文檔首頁>>Spire.XLS 系列教程>>Spire.XLS 圖表系列教程:C# 給 Excel 圖表添加數(shù)據(jù)表
Spire.XLS 圖表系列教程:C# 給 Excel 圖表添加數(shù)據(jù)表
查看冰藍更多產品教程:Spire.Doc系列教程 | Spire.PDF系列教程
數(shù)據(jù)表也稱為模擬運算表,是Excel圖表的一種元素,它可以在圖表的下方顯示數(shù)據(jù)源中的數(shù)據(jù)。本文將介紹如何使用Spire.XLS給Excel圖表添加數(shù)據(jù)表。
//創(chuàng)建Workbook實例 Workbook workbook = new Workbook(); //獲取第一張工作表 Worksheet sheet = workbook.Worksheets[0]; //向工作表中添加數(shù)據(jù) 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"]; //顯示數(shù)據(jù)表 chart.HasDataTable = true; //保存文件 workbook.SaveToFile("AddDataTable.xlsx", ExcelVersion.Version2010);
效果圖:
如果你有任何問題或意見,請在下方評論區(qū)留言~