Excel 圖表用於可視化電子表格中的數據。 MS Excel 支持折線圖、條形圖、餅圖、圓環圖、金字塔圖、氣泡圖等多種圖表。在本文中,您將學習如何使用 C# 在 Excel 文件中創建圖表。
用於在 Excel 中創建圖表的 C# API
為了使用 Excel 圖表,我們將使用 Aspose.Cells for .NET。它是一個功能強大的 API,可讓您在 .NET 應用程序中實現 Excel 自動化。此外,它還允許您無縫地創建各種圖表。要使用 API,您可以 下載 DLL 或使用 NuGet 安裝它。
Install-Package Aspose.Cells
支持的 Excel 圖表類型
Aspose.Cells for .NET 提供了一套完整的標準圖表類型。該列表包括但不限於:
- 柱子
- 柱子Stacked
- 柱子100PercentStacked
- 柱子3DClustered
- 柱子3DStacked
- 柱子3D100PercentStacked
- 柱子3D
- 酒吧
- 酒吧Stacked
- 酒吧100PercentStacked
- 酒吧3DClustered
- 酒吧3DStacked
- 酒吧3D100PercentStacked
- 線
- 線Stacked
- 線100PercentStacked
- 線WithDataMarkers
- 線StackedWithDataMarkers
- 線100PercentStackedWithDataMarkers
- 線3D
- 餡餅
- 餡餅3D
- 餡餅Pie
- 餡餅Exploded
- 餡餅3DExploded
- 餡餅Bar
- 分散
- 分散ConnectedByCurvesWithDataMarker
如需受支持的 Excel 圖表的完整列表,請訪問本文。
使用 C# 在 Excel 中創建圖表
以下是使用 C# 在 Excel 中創建圖表的步驟。
- 首先,創建一個新的 Excel 工作簿或使用 Workbook 類加載現有工作簿。
- 將所需的工作表訪問到 Worksheet 對像中。
- 將數據插入工作表(如果工作表為空)。
- 使用 Worksheet.Charts.Add(ChartType type, int upperLeftRow, int upperLeftColumn, int lowerRightRow, int lowerRightColumn) 方法在工作表中創建圖表。
- 通過索引訪問圖表到 Chart 對象。
- 使用 Chart.SetChartDataRange(“A1:C4”, true) 方法為圖表設置數據源。
- 最後,使用 Workbook.Save(string) 方法保存工作簿。
以下代碼示例展示瞭如何使用 C# 創建 Excel 圖表。
// 實例化工作簿對象
Workbook workbook = new Workbook();
// 獲取第一個工作表的引用
Worksheet worksheet = workbook.Worksheets[0];
// 將示例值添加到單元格
worksheet.Cells["A2"].PutValue("Category1");
worksheet.Cells["A3"].PutValue("Category2");
worksheet.Cells["A4"].PutValue("Category3");
worksheet.Cells["B1"].PutValue("Column1");
worksheet.Cells["B2"].PutValue(4);
worksheet.Cells["B3"].PutValue(20);
worksheet.Cells["B4"].PutValue(50);
worksheet.Cells["C1"].PutValue("Column2");
worksheet.Cells["C2"].PutValue(50);
worksheet.Cells["C3"].PutValue(100);
worksheet.Cells["C4"].PutValue(150);
// 向工作表添加圖表
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5);
// 訪問新添加圖表的實例
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// 設置圖表數據源為範圍“A1:C4”
chart.SetChartDataRange("A1:C4", true);
// 保存 Excel 文件
workbook.Save("Column-Chart.xls");
使用 C# 在 Excel 中創建折線圖
為了插入折線圖,您只需要在 Worksheet.Charts.Add() 方法中指定 ChartType.Line 類型。其餘步驟將與上一節中提到的相同。
- 首先,使用 Workbook 類創建一個 Excel 工作簿。
- 將所需的工作表訪問到 Worksheet 對像中。
- 將數據插入工作表。
- 使用 Worksheet.Charts.Add() 方法創建折線圖。
- 通過索引訪問圖表到 Chart 對象。
- 使用 Chart.SetChartDataRange(“A1:C4”, true) 方法為圖表設置數據源。
- 最後,使用 Workbook.Save(string) 方法保存工作簿。
以下代碼示例展示瞭如何使用 C# 在 Excel 中創建折線圖。
// 實例化工作簿對象
Workbook workbook = new Workbook();
// 獲取第一個工作表的引用
Worksheet worksheet = workbook.Worksheets[0];
// 將示例值添加到單元格
worksheet.Cells["A2"].PutValue("Category1");
worksheet.Cells["A3"].PutValue("Category2");
worksheet.Cells["A4"].PutValue("Category3");
worksheet.Cells["B1"].PutValue("Column1");
worksheet.Cells["B2"].PutValue(4);
worksheet.Cells["B3"].PutValue(20);
worksheet.Cells["B4"].PutValue(50);
worksheet.Cells["C1"].PutValue("Column2");
worksheet.Cells["C2"].PutValue(50);
worksheet.Cells["C3"].PutValue(100);
worksheet.Cells["C4"].PutValue(150);
// 向工作表添加圖表
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 5, 0, 15, 5);
// 訪問新添加圖表的實例
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// 設置圖表數據源為範圍“A1:C4”
chart.SetChartDataRange("A1:C4", true);
// 保存 Excel 文件
workbook.Save("Line-Chart.xls");
使用 C# 在 Excel 中創建金字塔圖
要添加金字塔圖,只需在將圖表添加到工作表時傳遞 ChartType.Pyramid 類型即可。以下是使用 C# 在 Excel 中添加金字塔圖的步驟。
- 首先,使用 Workbook 類創建一個 Excel 工作簿。
- 將所需的工作表訪問到 Worksheet 對像中。
- 將數據插入工作表。
- 使用 Worksheet.Charts.Add() 方法創建金字塔圖。
- 通過索引訪問圖表到 Chart 對象。
- 使用 Chart.SetChartDataRange(“A1:C4”, true) 方法為圖表設置數據源。
- 最後,使用 Workbook.Save(string) 方法保存工作簿。
以下代碼示例演示如何使用 C# 在 Excel 工作表中插入金字塔圖。
// 實例化工作簿對象
Workbook workbook = new Workbook();
// 獲取第一個工作表的引用
Worksheet worksheet = workbook.Worksheets[0];
// 將示例值添加到單元格
worksheet.Cells["A2"].PutValue("Category1");
worksheet.Cells["A3"].PutValue("Category2");
worksheet.Cells["A4"].PutValue("Category3");
worksheet.Cells["B1"].PutValue("Column1");
worksheet.Cells["B2"].PutValue(4);
worksheet.Cells["B3"].PutValue(20);
worksheet.Cells["B4"].PutValue(50);
worksheet.Cells["C1"].PutValue("Column2");
worksheet.Cells["C2"].PutValue(50);
worksheet.Cells["C3"].PutValue(100);
worksheet.Cells["C4"].PutValue(150);
// 向工作表添加圖表
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5);
// 訪問新添加圖表的實例
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// 設置圖表數據源為範圍“A1:C4”
chart.SetChartDataRange("A1:C4", true);
// 保存 Excel 文件
workbook.Save("Pyramid-Chart.xls");
要了解有關使用 Excel 圖表的更多信息,請閱讀 this 文檔文章。
獲取免費的 API 許可證
您可以通過申請 臨時許可證 來試用 Aspose.Cells for .NET,而沒有評估限制。
結論
在本文中,您了解瞭如何使用 C# 在 Excel 工作表中創建圖表。特別是,您學習瞭如何在 Excel 中創建柱形圖、折線圖和金字塔圖。此外,您還可以使用 Aspose.Cells for .NET 無縫創建其他類型的圖表。有關詳細信息,請訪問 API 的 文檔。如果您有任何疑問,請通過我們的 論壇 聯繫我們。