创建Excel图表
IronXL 支持在现代 XLSX 文件格式中创建和编辑 Excel 文档的图表。
此示例展示如何创建折线图。 其他图表类型也得到支持。
如何在 C&num 中创建 Excel 折线图;
- 安装 Excel 库,创建 Excel 折线图。
- 加载 Excel 工作簿并创建图表,使用
创建图表
. - 在
工作手册
根据需要 - 设置图表标题和图例,并调用
情节
. - 保存
工作手册
数据到 Excel 文件中。
using IronXL; using IronXL.Drawing.Charts; WorkBook workBook = WorkBook.Load("test.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Set the chart type and it's position on the worksheet. var chart = workSheet.CreateChart(ChartType.Line, 10, 10, 18, 20); // Add the series to the chart // The first parameter represents the address of the range for horizontal(category) axis. // The second parameter represents the address of the range for vertical(value) axis. var series = chart.AddSeries("B3:B8", "A3:A8"); // Set the chart title. series.Title = "Line Chart"; // Set the legend position. // Can be removed by setting it to None. chart.SetLegendPosition(LegendPosition.Bottom); // We can change the position of the chart. chart.Position.LeftColumnIndex = 2; chart.Position.RightColumnIndex = chart.Position.LeftColumnIndex + 3; // Plot all the data that was added to the chart before. // Multiple call of this method leads to plotting multiple charts instead of modifying the existing chart. // Yet there is no possibility to remove chart or edit it's series/position. // We can just create new one. chart.Plot(); workBook.SaveAs("CreateLineChart.xlsx");
Imports IronXL Imports IronXL.Drawing.Charts Private workBook As WorkBook = WorkBook.Load("test.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Set the chart type and it's position on the worksheet. Private chart = workSheet.CreateChart(ChartType.Line, 10, 10, 18, 20) ' Add the series to the chart ' The first parameter represents the address of the range for horizontal(category) axis. ' The second parameter represents the address of the range for vertical(value) axis. Private series = chart.AddSeries("B3:B8", "A3:A8") ' Set the chart title. series.Title = "Line Chart" ' Set the legend position. ' Can be removed by setting it to None. chart.SetLegendPosition(LegendPosition.Bottom) ' We can change the position of the chart. chart.Position.LeftColumnIndex = 2 chart.Position.RightColumnIndex = chart.Position.LeftColumnIndex + 3 ' Plot all the data that was added to the chart before. ' Multiple call of this method leads to plotting multiple charts instead of modifying the existing chart. ' Yet there is no possibility to remove chart or edit it's series/position. ' We can just create new one. chart.Plot() workBook.SaveAs("CreateLineChart.xlsx")
Install-Package IronXL.Excel
IronXL 支持在现代 XLSX 文件格式中创建和编辑 Excel 文档的图表。
此示例展示如何创建折线图。 其他图表类型也得到支持。
创建图表
.工作手册
根据需要情节
.工作手册
数据到 Excel 文件中。