借助Aapose.Cells 在 Excel 中创建龙卷风图表

发布于:2024-07-09 ⋅ 阅读:(41) ⋅ 点赞:(0)

龙卷风图是一种强大的工具,可用于比较数据点和可视化潜在变化。在这篇博文中,我们将学习如何使用 C# 在 Excel 中创建龙卷风图。本文将教您如何制作可直接添加到应用程序中的炫酷图表和图形,并附带简单易懂的说明!

Aspose.Cells 是Excel电子表格编程API,可加快电子表格的管理和处理任务,支持构建能够生成,修改,转换,呈现和打印电子表格的跨平台应用程序。同时不依赖于Microsoft Excel或任何Microsoft Office Interop组件,Aspose API 支持旗下产品覆盖文档、图表、PDF、条码、OCR、CAD、HTML、电子邮件等各个文档管理领域,为全球.NET 、Java、C ++等10余种平台开发人员提供丰富的开发选择。

Aspose.Cells for .Net 最新下载(qun:666790229)icon-default.png?t=N7T8https://www.evget.com/product/4115/download

使用 C# API 在 Excel 中创建龙卷风图表

Aspose.Cells for .NET是一款功能强大的 API,它使开发人员无需 Microsoft Excel 即可创建、操作和转换 Excel 文件。除了众多功能外,它还提供全面的图表功能,用于在电子表格中生成和管理图表。

要利用 API,请使用以下命令下载 DLL或从NuGet安装它:

PM> Install-Package Aspose.Cells
使用 C# 在 Excel 中创建龙卷风图

现在,我们可以按照以下步骤在 Excel 工作表中轻松地创建龙卷风图:

  1. 使用Workbook类加载 Excel 文件。
  2. 将所需的工作表放入Worksheet类对象中。
  3. (可选)将数据插入工作表。
  4. 使用Charts.Add()方法添加堆积条形图。
  5. 通过图表类对象中的索引访问图表。
  6. 使用SetChartDataRange()方法设置图表的数据源。
  7. 设置图表所需的属性。
  8. 最后,使用Save()方法保存文档

以下代码示例展示如何使用 C# 在 Excel 中创建龙卷风图

// Load en existing Excel file with data
Workbook wb = new Workbook("D:\\Files\\sample.xlsx");

// Access the worksheet
Worksheet sheet = wb.Worksheets[0];

// Get charts
ChartCollection charts = sheet.Charts;

// Add bar chart
int index = charts.Add(ChartType.BarStacked, 8, 1, 24, 8);
Chart chart = charts[index];

// Set data for bar chart
chart.SetChartDataRange("A1:C7", true);

// Set properties for bar chart
chart.Title.Text = "Tornado chart";
chart.Style = 2;
chart.PlotArea.Area.ForegroundColor = Color.White;
chart.PlotArea.Border.Color = Color.White;
chart.Legend.Position = LegendPositionType.Bottom;

chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.Low;
chart.CategoryAxis.IsPlotOrderReversed = true;

chart.GapWidth = 10;

Axis valueAxis = chart.ValueAxis;
valueAxis.TickLabels.NumberFormat = "#,##0;#,##0";

// Save the file
wb.Save("D:\\Files\\out.xlsx");

使用 C# 在 Excel 中创建龙卷风图

在 Excel 中插入数据并创建龙卷风图

在上一节中,我们使用预先填充了数据的工作表创建了龙卷风图。我们还可以使用Cells类的PutValue() 方法 将数据插入 Excel 工作表。创建龙卷风图的其余过程将保持不变。

以下代码示例显示如何使用 C# 在 Excel 中插入数据然后创建龙卷风图

// Create a new Excel Workbook
Workbook wb = new Workbook();
Worksheet worksheet = wb.Worksheets[0];

// Add sample values to cells
worksheet.Cells["A1"].PutValue("Products");
worksheet.Cells["A2"].PutValue("Product A");
worksheet.Cells["A3"].PutValue("Product B");
worksheet.Cells["A4"].PutValue("Product C");
worksheet.Cells["A5"].PutValue("Product D");
worksheet.Cells["A6"].PutValue("Product E");

worksheet.Cells["B1"].PutValue("2021-2022");
worksheet.Cells["B2"].PutValue(-100);
worksheet.Cells["B3"].PutValue(-80);
worksheet.Cells["B4"].PutValue(-75);
worksheet.Cells["B5"].PutValue(-60);
worksheet.Cells["B6"].PutValue(-48);

worksheet.Cells["C1"].PutValue("2023-2024");
worksheet.Cells["C2"].PutValue(95);
worksheet.Cells["C3"].PutValue(80);
worksheet.Cells["C4"].PutValue(72);
worksheet.Cells["C5"].PutValue(65);
worksheet.Cells["C6"].PutValue(45);

ChartCollection charts = worksheet.Charts;
// Add bar chart
int index = charts.Add(ChartType.BarStacked, 8, 1, 24, 8);
Chart chart = charts[index];

// Set data for bar chart
chart.SetChartDataRange("A1:C6", true);

// Set properties for bar chart
chart.Title.Text = "Tornado chart";
chart.Style = 2;
chart.PlotArea.Area.ForegroundColor = Color.White;
chart.PlotArea.Border.Color = Color.White;
chart.Legend.Position = LegendPositionType.Bottom;

chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.Low;
chart.CategoryAxis.IsPlotOrderReversed = true;

chart.GapWidth = 50;

// Show data labels
DataLabels datalabels;
for (int i = 0; i < chart.NSeries.Count; i++)
{
datalabels = chart.NSeries[i].DataLabels;
//Set the position of DataLabels
datalabels.Position = LabelPositionType.Center;

//Show the value in the DataLabels
datalabels.ShowValue = true;

datalabels.Font.Color = Color.White;
datalabels.NumberFormat = "#,##0;#,##0";

}

Axis valueAxis = chart.ValueAxis;
valueAxis.TickLabels.NumberFormat = "#,##0;#,##0";

// Save the file
wb.Save("D:\\Files\\chart_out.xlsx");

在 Excel 中插入数据并创建龙卷风图

结论

在本文中,我们学习了如何使用 C# 在 Excel 中创建龙卷风图。从在 Excel 工作表中插入数据到创建图表和设置图表属性,Aspose.Cells for .NET API 提供了一套全面的数据可视化功能。按照本文概述的步骤,您可以轻松地将龙卷风图集成到您的应用程序中。


网站公告

今日签到

点亮在社区的每一天
去签到