创建excel图表C#

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 中创建图表的步骤。

以下代码示例展示了如何使用 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# 中创建柱形图

使用 C# 在 Excel 中创建折线图

为了插入折线图,您只需要在 Worksheet.Charts.Add() 方法中指定 ChartType.Line 类型。其余步骤将与上一节中提到的相同。

以下代码示例展示了如何使用 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");
Excel C#中的折线图

使用 C# 在 Excel 中创建金字塔图

要添加金字塔图,只需在将图表添加到工作表时传递 ChartType.Pyramid 类型。以下是使用 C# 在 Excel 中添加金字塔图的步骤。

以下代码示例演示如何使用 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 中创建金字塔图

要了解有关使用 Excel 图表的更多信息,请阅读 this 文档文章。

获取免费 API 许可证

您可以通过申请 临时许可证 来试用 Aspose.Cells for .NET,而不受评估限制。

结论

在本文中,您了解了如何使用 C# 在 Excel 工作表中创建图表。特别是,您学习了如何在 Excel 中创建柱形图、折线图和金字塔图。此外,您还可以使用 Aspose.Cells for .NET 无缝创建其他类型的图表。有关更多详细信息,请访问 API 的文档。如果您有任何疑问,请通过我们的 论坛 联系我们。

也可以看看