IronXL支援在現代XLSX檔案格式的Excel文件中建立和編輯圖表。
此範例顯示如何建立一個LineChart。 其他圖表型別也受到支援。
如何在C#中建立Excel折線圖
- 安裝Excel程式庫來建立Excel折線圖。
- 載入Excel工作簿並使用
CreateChart建立圖表。 - 根據需要向
Workbook新增更多值。 - 設定圖表的標題和圖例,並調用
Plot方法來顯示圖表。 - 將
Workbook資料儲存到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
Dim workBook As WorkBook = WorkBook.Load("test.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and its position on the worksheet.
Dim 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.
Dim 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 calls of this method lead to plotting multiple charts instead of modifying the existing chart.
' Yet there is no possibility to remove chart or edit its series/position.
' We can just create a new one.
chart.Plot()
workBook.SaveAs("CreateLineChart.xlsx")IronXL支援在現代XLSX檔案格式的Excel文件中建立和編輯圖表。
此範例顯示如何建立一個LineChart。 其他圖表型別也受到支援。
CreateChart建立圖表。Workbook新增更多值。Plot方法來顯示圖表。Workbook資料儲存到Excel檔案中。Nuget Downloads 2,237,574|版本:2026.9剛剛發布