数据表也称为模拟运算表,是excel图表的一种元素,它可以在图表的下方显示数据源中的数据。本文将介绍如何使用spire.xls给excel图表添加数据表。
c#
//创建workbook实例
workbook workbook = new workbook();
//获取第一张工作表
worksheet sheet = workbook.worksheets[0];
//向工作表中添加数据
sheet.range["a1"].text = "月份";
sheet.range["a2"].text = "7月销量";
sheet.range["a3"].text = "8月销量";
sheet.range["a4"].text = "9月销量";
sheet.range["b1"].text = "海尔";
sheet.range["b2"].numbervalue = 950;
sheet.range["b3"].numbervalue = 500;
sheet.range["b4"].numbervalue = 900;
sheet.range["c1"].text = "美的";
sheet.range["c2"].numbervalue = 680;
sheet.range["c3"].numbervalue = 720;
sheet.range["c4"].numbervalue = 890;
sheet.range["d1"].text = "格力";
sheet.range["d2"].numbervalue = 980;
sheet.range["d3"].numbervalue = 1070;
sheet.range["d4"].numbervalue = 1200;
//插入图表
chart chart = sheet.charts.add(excelcharttype.column3dclustered);
chart.datarange = sheet.range["b1:d4"];
chart.seriesdatafromrange = false;
chart.toprow = 7;
chart.bottomrow = 28;
chart.leftcolumn = 3;
chart.rightcolumn = 11;
chart.charttitle = "第三季度产品销量分析";
chart.charttitlearea.isbold = true;
chart.charttitlearea.size = 12;
spire.xls.charts.chartserie cs1 = chart.series[0];
cs1.categorylabels = sheet.range["a2:a4"];
//显示数据表
chart.hasdatatable = true;
//保存文件
workbook.savetofile("adddatatable.xlsx", excelversion.version2010);
vb.net
'创建workbook实例
dim workbook as new workbook()
'获取第一张工作表
dim sheet as worksheet = workbook.worksheets(0)
'向工作表中添加数据
sheet.range("a1").text = "月份"
sheet.range("a2").text = "7月销量"
sheet.range("a3").text = "8月销量"
sheet.range("a4").text = "9月销量"
sheet.range("b1").text = "海尔"
sheet.range("b2").numbervalue = 950
sheet.range("b3").numbervalue = 500
sheet.range("b4").numbervalue = 900
sheet.range("c1").text = "美的"
sheet.range("c2").numbervalue = 680
sheet.range("c3").numbervalue = 720
sheet.range("c4").numbervalue = 890
sheet.range("d1").text = "格力"
sheet.range("d2").numbervalue = 980
sheet.range("d3").numbervalue = 1070
sheet.range("d4").numbervalue = 1200
'插入图表
dim chart as chart = sheet.charts.add(excelcharttype.column3dclustered)
chart.datarange = sheet.range("b1:d4")
chart.seriesdatafromrange = false
chart.toprow = 7
chart.bottomrow = 28
chart.leftcolumn = 3
chart.rightcolumn = 11
chart.charttitle = "第三季度产品销量分析"
chart.charttitlearea.isbold = true
chart.charttitlearea.size = 12
dim cs1 as spire.xls.charts.chartserie = chart.series(0)
cs1.categorylabels = sheet.range("a2:a4")
'显示数据表
chart.hasdatatable = true
'保存文件
workbook.savetofile("adddatatable.xlsx", excelversion.version2010)
效果图: