spire.xls 组件为用户提供了多种图表类型选择,其中涵盖了 excel 自带的各种图表。这些图表类型被定义在枚举 spire.xls.excelcharttype 中,共有以下 74 种:
图表类型 |
描述 |
1. columnclustered | represents clustered column chart |
2. columnstacked | represents stacked column chart |
3. column100percentstacked | represents 100% stacked column chart |
4. column3dclustered | represents 3d clustered column chart |
5. column3dstacked | represents 3d stacked column chart |
6. column3d100percentstacked | represents 3d 100% stacked column chart |
7. column3d | represents 3d column chart |
8. barclustered | represents clustered bar chart |
9. barstacked | represents stacked bar chart |
10. bar100percentstacked | represents 100% stacked bar chart |
11. bar3dclustered | represents 3d clustered bar chart |
12. bar3dstacked | represents 3d stacked bar chart |
13. bar3d100percentstacked | represents 100% 3d stacked bar chart |
14. line | represents line chart |
15. linestacked | represents stacked line chart |
16. line100percentstacked | represents 100% stacked line chart |
17. linemarkers | represents markers line chart |
18. linemarkersstacked | represents stacked markers line chart |
19. linemarkers100percentstacked | represents 100% stacked markers line chart |
20. line3d | represents 3d line chart |
21. pie | represents pie chart |
22. pie3d = 21 | represents 3d pie chart |
23. pieofpie | represents pie of pie chart |
24. pieexploded | represents exploded pie chart |
25. pie3dexploded | represents 3d exploded pie chart |
26. piebar | represents bar pie chart |
27. scattermarkers | represents markers scatter chart |
28. scattersmoothedlinemarkers | represents scattersmoothedlinemarkers chart |
29. scattersmoothedline | represents scattersmoothedline chart |
30. scatterlinemarkers | represents scatterlinemarkers chart |
31. scatterline | represents scatterline chart |
32. area | represents area chart |
33. areastacked | represents areastacked chart |
34. area100percentstacked | represents area100percentstacked chart |
35. area3d | represents area3d chart |
36. area3dstacked | represents area3dstacked chart |
37. area3d100percentstacked | represents area3d100percentstacked chart |
38. doughnut | represents doughnut chart |
39. doughnutexploded | represents doughnutexploded chart |
40. radar | represents radar chart |
41. radarmarkers | represents radarmarkers chart |
42. radarfilled | represents radarfilled chart |
43. surface3d | represents surface3d chart |
44. surface3dnocolor | represents surface3dnocolor chart |
45. surfacecontour | represents surfacecontour chart |
46. surfacecontournocolor | represents surfacecontournocolor chart |
47. bubble | represents bubble chart |
48. bubble3d | represents bubble3d chart |
49. stockhighlowclose | represents stockhighlowclose chart |
50. stockopenhighlowclose | represents stockopenhighlowclose chart |
51. stockvolumehighlowclose | represents stockvolumehighlowclose chart |
52. stockvolumeopenhighlowclose | represents stockvolumeopenhighlowclose chart |
53. cylinderclustered | represents cylinderclustered chart |
54. cylinderstacked | represents cylinderstacked chart |
55. cylinder100percentstacked | represents cylinder100percentstacked chart |
56. cylinderbarclustered | represents cylinderbarclustered chart |
57. cylinderbarstacked | represents cylinderbarstacked chart |
58. cylinderbar100percentstacked | represents cylinderbar100percentstacked chart |
59. cylinder3dclustered | represents cylinder3dclustered chart |
60. coneclustered | represents coneclustered chart |
61. conestacked | represents conestacked chart |
62. cone100percentstacked | represents cone100percentstacked chart |
63. conebarclustered | represents conebarclustered chart |
64. conebarstacked | represents conebarstacked chart |
65. conebar100percentstacked | represents conebar100percentstacked chart |
66. cone3dclustered | represents cone3dclustered chart |
67. pyramidclustered | represents pyramidclustered chart |
68. pyramidstacked | represents pyramidstacked chart |
69. pyramid100percentstacked | represents pyramid100percentstacked chart |
70. pyramidbarclustered | represents pyramidbarclustered chart |
71. pyramidbarstacked | represents pyramidbarstacked chart |
72. pyramidbar100percentstacked | represents pyramidbar100percentstacked chart |
73. pyramid3dclustered | represents pyramid3dclustered chart |
74. combinationchart | represents combination chart |
该节中我们将从以上表格中选取三种常见图表来进行创建:clustered column chart(柱状图),pie chart (饼状图)及doughnut chart(环形图)。
clustered column chart 柱状图
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
//获取工作表
worksheet sheet = workbook.worksheets[0];
//设置工作表的名称
sheet.name = "柱状图";
sheet.gridlinesvisible = false;
//创建柱状图
chart chart = sheet.charts.add(excelcharttype.columnclustered);
//指定用于生成图表的数据区域
chart.datarange = sheet.range["a1:b5"];
chart.seriesdatafromrange = false;
//指定图表所在的位置
chart.leftcolumn = 5;
chart.toprow = 2;
chart.rightcolumn = 15;
chart.bottomrow = 20;
//设置图表的名称以及x、y轴的名称
chart.charttitle = "部门信息";
chart.charttitlearea.isbold = true;
chart.charttitlearea.size = 12;
chart.primarycategoryaxis.title = "部门";
chart.primarycategoryaxis.font.isbold = true;
chart.primarycategoryaxis.titlearea.isbold = true;
chart.primaryvalueaxis.title = "人数";
chart.primaryvalueaxis.hasmajorgridlines = false;
chart.primaryvalueaxis.titlearea.textrotationangle = 90;
chart.primaryvalueaxis.minvalue = 0;
chart.primaryvalueaxis.titlearea.isbold = true;
//5.设置图例的位置
chart.legend.position = legendpositiontype.right;
//保存工作薄
workbook.savetofile("columnchart.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
'获取工作表
dim sheet as worksheet = workbook.worksheets(0)
'设置工作表的名称
sheet.name = "柱状图"
sheet.gridlinesvisible = false
'创建柱状图
dim chart as chart = sheet.charts.add(excelcharttype.columnclustered)
'指定用于生成图表的数据区域
chart.datarange = sheet.range("a1:b5")
chart.seriesdatafromrange = false
'指定图表所在的位置
chart.leftcolumn = 5
chart.toprow = 2
chart.rightcolumn = 15
chart.bottomrow = 20
'设置图表的名称以及x、y轴的名称
chart.charttitle = "部门信息"
chart.charttitlearea.isbold = true
chart.charttitlearea.size = 12
chart.primarycategoryaxis.title = "部门"
chart.primarycategoryaxis.font.isbold = true
chart.primarycategoryaxis.titlearea.isbold = true
chart.primaryvalueaxis.title = "人数"
chart.primaryvalueaxis.hasmajorgridlines = false
chart.primaryvalueaxis.titlearea.textrotationangle = 90
chart.primaryvalueaxis.minvalue = 0
chart.primaryvalueaxis.titlearea.isbold = true
'5.设置图例的位置
chart.legend.position = legendpositiontype.right
'保存工作薄
workbook.savetofile("columnchart.xlsx", excelversion.version2013)
pie chart 饼状图
c#
//创建工作簿
workbook workbook = new workbook();
//获取工作表
worksheet sheet = workbook.worksheets[0];
//设置工作表的名称
sheet.name = "饼状图";
sheet.gridlinesvisible = false;
//向工作表添加数据
sheet.range["a1"].value = "年份";
sheet.range["a2"].value = "2002";
sheet.range["a3"].value = "2003";
sheet.range["a4"].value = "2004";
sheet.range["a5"].value = "2005";
sheet.range["b1"].value = "销售额";
sheet.range["b2"].numbervalue = 4000;
sheet.range["b3"].numbervalue = 6000;
sheet.range["b4"].numbervalue = 7000;
sheet.range["b5"].numbervalue = 8500;
sheet.range["a1:b1"].style.font.isbold = true;
sheet.range["a2:b2"].style.knowncolor = excelcolors.lightyellow;
sheet.range["a3:b3"].style.knowncolor = excelcolors.lightgreen1;
sheet.range["a4:b4"].style.knowncolor = excelcolors.lightorange;
sheet.range["a5:b5"].style.knowncolor = excelcolors.lightturquoise;
sheet.range["a1:b5"].style.borders[borderslinetype.edgetop].color = color.fromargb(0, 0, 128);
sheet.range["a1:b5"].style.borders[borderslinetype.edgetop].linestyle = linestyletype.thin;
sheet.range["a1:b5"].style.borders[borderslinetype.edgebottom].color = color.fromargb(0, 0, 128);
sheet.range["a1:b5"].style.borders[borderslinetype.edgebottom].linestyle = linestyletype.thin;
sheet.range["a1:b5"].style.borders[borderslinetype.edgeleft].color = color.fromargb(0, 0, 128);
sheet.range["a1:b5"].style.borders[borderslinetype.edgeleft].linestyle = linestyletype.thin;
sheet.range["a1:b5"].style.borders[borderslinetype.edgeright].color = color.fromargb(0, 0, 128);
sheet.range["a1:b5"].style.borders[borderslinetype.edgeright].linestyle = linestyletype.thin;
sheet.range["b2:c5"].style.numberformat = "\"$\"#,##0";
//创建饼状图表
chart chart = sheet.charts.add(excelcharttype.pie);
//指定用于生成图表的数据区域
chart.datarange = sheet.range["b2:b5"];
chart.seriesdatafromrange = false;
//指定图表所在位置
chart.leftcolumn = 1;
chart.toprow = 7;
chart.rightcolumn = 9;
chart.bottomrow = 26;
//设置图表的名称
chart.charttitle = "年销售额";
chart.charttitlearea.isbold = true;
chart.charttitlearea.size = 12;
//设置图表系列的值
spire.xls.charts.chartserie cs = chart.series[0];
cs.categorylabels = sheet.range["a2:a5"];
cs.values = sheet.range["b2:b5"];
cs.datapoints.defaultdatapoint.datalabels.hasvalue = true;
chart.plotarea.fill.visible = false;
//保存工作薄
workbook.savetofile("piechart.xls");
vb.net
'创建工作簿
dim workbook as new workbook()
'获取工作表
dim sheet as worksheet = workbook.worksheets(0)
'设置工作表的名称
sheet.name = "饼状图"
sheet.gridlinesvisible = false
'向工作表添加数据
sheet.range("a1").value = "年份"
sheet.range("a2").value = "2002"
sheet.range("a3").value = "2003"
sheet.range("a4").value = "2004"
sheet.range("a5").value = "2005"
sheet.range("b1").value = "销售额"
sheet.range("b2").numbervalue = 4000
sheet.range("b3").numbervalue = 6000
sheet.range("b4").numbervalue = 7000
sheet.range("b5").numbervalue = 8500
sheet.range("a1:b1").style.font.isbold = true
sheet.range("a2:b2").style.knowncolor = excelcolors.lightyellow
sheet.range("a3:b3").style.knowncolor = excelcolors.lightgreen1
sheet.range("a4:b4").style.knowncolor = excelcolors.lightorange
sheet.range("a5:b5").style.knowncolor = excelcolors.lightturquoise
sheet.range("a1:b5").style.borders(borderslinetype.edgetop).color = color.fromargb(0, 0, 128)
sheet.range("a1:b5").style.borders(borderslinetype.edgetop).linestyle = linestyletype.thin
sheet.range("a1:b5").style.borders(borderslinetype.edgebottom).color = color.fromargb(0, 0, 128)
sheet.range("a1:b5").style.borders(borderslinetype.edgebottom).linestyle = linestyletype.thin
sheet.range("a1:b5").style.borders(borderslinetype.edgeleft).color = color.fromargb(0, 0, 128)
sheet.range("a1:b5").style.borders(borderslinetype.edgeleft).linestyle = linestyletype.thin
sheet.range("a1:b5").style.borders(borderslinetype.edgeright).color = color.fromargb(0, 0, 128)
sheet.range("a1:b5").style.borders(borderslinetype.edgeright).linestyle = linestyletype.thin
sheet.range("b2:c5").style.numberformat = """$""#,##0"
'创建饼状图表
dim chart as chart = sheet.charts.add(excelcharttype.pie)
'指定用于生成图表的数据区域
chart.datarange = sheet.range("b2:b5")
chart.seriesdatafromrange = false
'指定图表所在位置
chart.leftcolumn = 1
chart.toprow = 7
chart.rightcolumn = 9
chart.bottomrow = 26
'设置图表的名称
chart.charttitle = "年销售额"
chart.charttitlearea.isbold = true
chart.charttitlearea.size = 12
'设置图表系列的值
dim cs as spire.xls.charts.chartserie = chart.series(0)
cs.categorylabels = sheet.range("a2:a5")
cs.values = sheet.range("b2:b5")
cs.datapoints.defaultdatapoint.datalabels.hasvalue = true
chart.plotarea.fill.visible = false
'保存工作薄
workbook.savetofile("piechart.xls")
doughnut chart 环形图
c#
//创建工作薄
workbook workbook = new workbook();
workbook.version = excelversion.version2013;
//获取工作表
worksheet sheet = workbook.worksheets[0];
sheet.gridlinesvisible = false;
//向工作表添加数据
sheet.range["a1"].value = "国家";
sheet.range["a1"].style.font.isbold = true;
sheet.range["a2"].value = "古巴";
sheet.range["a3"].value = "墨西哥";
sheet.range["a4"].value = "法国";
sheet.range["a5"].value = "德国";
sheet.range["b1"].value = "销售额";
sheet.range["b1"].style.font.isbold = true;
sheet.range["b2"].numbervalue = 6000;
sheet.range["b3"].numbervalue = 8000;
sheet.range["b4"].numbervalue = 9000;
sheet.range["b5"].numbervalue = 8500;
//创建环形图
chart chart = sheet.charts.add();
chart.charttype = excelcharttype.doughnut;
//指定用于生成图表的数据区域
chart.datarange = sheet.range["a1:b5"];
chart.seriesdatafromrange = false;
//指定图表的位置
chart.leftcolumn = 4;
chart.toprow = 2;
chart.rightcolumn = 12;
chart.bottomrow = 22;
//设置图表的名称
chart.charttitle = "销售额百分比";
chart.charttitlearea.isbold = true;
chart.charttitlearea.size = 10;
//设置图表系列的值
foreach (chartserie cs in chart.series)
{
cs.datapoints.defaultdatapoint.datalabels.haspercentage = true;
}
//设置图例的位置
chart.legend.position = legendpositiontype.top;
//保存工作薄
workbook.savetofile("doughnutchart.xlsx");
vb.net
'创建工作薄
dim workbook as new workbook()
workbook.version = excelversion.version2013
'获取工作表
dim sheet as worksheet = workbook.worksheets(0)
sheet.gridlinesvisible = false
'向工作表添加数据
sheet.range("a1").value = "国家"
sheet.range("a1").style.font.isbold = true
sheet.range("a2").value = "古巴"
sheet.range("a3").value = "墨西哥"
sheet.range("a4").value = "法国"
sheet.range("a5").value = "德国"
sheet.range("b1").value = "销售额"
sheet.range("b1").style.font.isbold = true
sheet.range("b2").numbervalue = 6000
sheet.range("b3").numbervalue = 8000
sheet.range("b4").numbervalue = 9000
sheet.range("b5").numbervalue = 8500
'创建环形图
dim chart as chart = sheet.charts.add()
chart.charttype = excelcharttype.doughnut
'指定用于生成图表的数据区域
chart.datarange = sheet.range("a1:b5")
chart.seriesdatafromrange = false
'指定图表的位置
chart.leftcolumn = 4
chart.toprow = 2
chart.rightcolumn = 12
chart.bottomrow = 22
'设置图表的名称
chart.charttitle = "销售额百分比"
chart.charttitlearea.isbold = true
chart.charttitlearea.size = 10
'设置图表系列的值
for each cs as chartserie in chart.series
cs.datapoints.defaultdatapoint.datalabels.haspercentage = true
next
'设置图例的位置
chart.legend.position = legendpositiontype.top
'保存工作薄
workbook.savetofile("doughnutchart.xlsx")