如何在 C# 中創建和編輯 Excel 圖表
在 Excel 中,圖表是資料的圖形表示。它是一種視覺工具,用於以更易於理解和有意義的方式顯示和分析資料。Excel 提供各種圖表類型,例如條形圖、折線圖、圓餅圖等,每種圖表適用於不同類型的資料和分析。
IronXL 支援柱狀圖、散點圖、折線圖、圓餅圖、條形圖和區域圖,並具有可配置的系列名稱、圖例位置、圖表標題和圖表位置。
如何在 C# 中創建和編輯 Excel 圖表
- 下載 C# 程式庫以創建和編輯圖表
- 準備用於製作圖表的數據
- 使用
建立圖表
設置圖表類型和位置的方法 - 使用
新增系列
新增資料系列的方法 - 使用該繪製圖表
情節
方法
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronXL 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變Excel。
Install-Package IronXL.Excel
請考慮安裝 IronXL DLL 直接下載並手動安裝到您的專案或GAC表單: IronXL.zip
手動安裝到您的項目中
下載DLL建立圖表範例
IronXL 支援柱狀圖、散佈圖、折線圖、圓餅圖、條形圖和面積圖。要建立圖表,我們需要分別指定一些事項。
開始使用
CreateChart
方法來指定圖表類型和工作表中的位置。使用
AddSeries
方法添加系列資料。此方法也接受單列數據,因為這對於某些圖表類型已足夠。第一個參數代表水平方向的數值。第二個參數代表垂直方向的數值。可以選擇性地指定系列名稱、圖表名稱和圖例位置。
- 調用
Plot
方法來繪製圖表。該方法將使用所有添加的數據來繪製圖表。多次調用此方法會導致繪製多個圖表,而不是修改現有的圖表。
讓我們從數據中創建一些圖表 chart.xlsx Excel 文件。以下顯示數據預覽:
柱狀圖
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-column-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;
// Set the chart title
chart.SetTitle("Column Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("columnChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue
' Set the chart title
chart.SetTitle("Column Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("columnChart.xlsx")
折線圖
由於折線圖能夠代表與柱狀圖相同的信息,切換這兩者非常簡單。您只需要更改圖表類型。
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-line-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;
// Set the chart title
chart.SetTitle("Line Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("lineChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue
' Set the chart title
chart.SetTitle("Line Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("lineChart.xlsx")
圓餅圖
對於圓餅圖,僅需要一列數據。
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Set the chart title
chart.SetTitle("Pie Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Set the chart title
chart.SetTitle("Pie Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("pieChart.xlsx")
編輯圖表示例
您可以在現有圖表中編輯一些內容。您可以編輯圖例位置和圖表標題。要編輯圖表,您必須首先通過訪問 Charts 屬性來檢索圖表,然後選擇要編輯的目標圖表。從那裡,訪問圖表的屬性進行編輯。
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the chart
IChart chart = workSheet.Charts[0];
// Edit the legend position
chart.SetLegendPosition(LegendPosition.Top);
// Edit the chart title
chart.SetTitle("Edited Chart");
workBook.SaveAs("editedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the chart
Private chart As IChart = workSheet.Charts(0)
' Edit the legend position
chart.SetLegendPosition(LegendPosition.Top)
' Edit the chart title
chart.SetTitle("Edited Chart")
workBook.SaveAs("editedChart.xlsx")
之前
後
刪除圖表範例
要從試算表中刪除現有圖表,首先從工作表物件的 Charts 屬性中檢索圖表。您將從 Charts 屬性中獲取圖表列表。將目標圖表物件傳遞給 RemoveChart
方法來刪除它。
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
using System.Collections.Generic;
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the chart
List<IChart> chart = workSheet.Charts;
// Remove the chart
workSheet.RemoveChart(chart[0]);
workBook.SaveAs("removedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Imports System.Collections.Generic
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the chart
Private chart As List(Of IChart) = workSheet.Charts
' Remove the chart
workSheet.RemoveChart(chart(0))
workBook.SaveAs("removedChart.xlsx")