数据表也称为模拟运算表,是Excel图表的一种元素,它可以在图表的下方显示数据源中的数据。本文将介绍如何使用Spire.XLS给Excel图表添加数据表。
//创建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);
效果图:
如果你有任何问题或意见,请在下方评论区留言~
点击“了解更多”下载产品最新试用版
↓↓↓