如何在 C# 中創建和編輯 Excel 圖表

如何在 C# 中建立和編輯 Excel 圖表

This article was translated from English: Does it need improvement?
Translated
View the article in English

在Excel中,圖表是資料的圖形化表示。 它是一種視覺化工具,用於以更易於理解和更有意義的方式顯示和分析數據。 Excel 提供了多種圖表類型,例如長條圖、折線圖、圓餅圖等等,每種圖表都適用於不同類型的資料和分析。

快速入門:幾秒鐘內創建並繪製折線圖

使用 IronXL,您只需幾行程式碼即可完成安裝、載入工作簿、呼叫 CreateChart、新增資料系列、設定標題和圖例位置以及繪製圖表等操作。 這個例子展示瞭如何使用原生 C# 方法快速地從沒有圖表到製作精美的可視化圖表——而無需任何互通開銷。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronXL

    PM > Install-Package IronXL.Excel

  2. 複製並運行這段程式碼。

    var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot();
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronXL,免費試用!
    arrow pointer


開始使用 IronXL

立即開始在您的項目中使用 IronXL 並免費試用。

第一步:
green arrow pointer


建立圖表範例

IronXL 支援長條圖、散佈圖、折線圖、圓餅圖、長條圖和麵積圖。 要建立圖表,我們需要分別指定一些內容。

  1. 首先使用CreateChart方法指定圖表類型及其在工作表中的位置。
  2. 使用AddSeries方法新增序列。 此方法也接受單列數據,因為對於某些圖表類型來說,單列數據就足夠了。第一個參數表示水平軸的值。 第二個參數表示縱軸的值。
  3. (可選)指定係列名稱、圖表名稱和圖例位置。
  4. 呼叫Plot方法繪製圖表。 此方法將使用所有已新增的資料繪製圖表。 多次呼叫此方法會導致繪製多個圖表,而不是修改現有圖表。

Let's create some charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:

数据

長條圖

: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")
$vbLabelText   $csharpLabel
長條圖

折線圖

由於折線圖和長條圖都能表示大量的訊息,因此在兩者之間切換非常簡單。 您只需要更改圖表類型即可。

: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")
$vbLabelText   $csharpLabel
折線圖

圓餅圖

餅圖只需要一列資料。

: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")
$vbLabelText   $csharpLabel
圓餅圖

編輯圖表範例

您可以對現有圖表進行一些編輯。 您可以編輯圖例位置和圖表標題。 若要編輯圖表,必須先透過造訪"圖表"屬性來擷取圖表,然後選擇要編輯的目標圖表。 從那裡,訪問圖表的屬性進行編輯。

: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")
$vbLabelText   $csharpLabel
前
後

移除圖表範例

若要從電子表格中刪除現有圖表,首先從工作表物件的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")
$vbLabelText   $csharpLabel

常見問題解答

如何在 C# 中創建不需要 Interop 的 Excel 圖表?

您可以使用 IronXL 庫在 C# 中創建不需 Interop 的 Excel 圖表。首先,從 NuGet 下載該庫,然後使用像 CreateChart 這樣的方法指定工作表中圖表的類型和位置。

使用 C# 自訂 Excel 圖表涉及哪些步驟?

要使用 C# 自訂 Excel 圖表,首先準備數據,然後使用 AddSeries 方法添加數據系列。透過訪問工作表的 Charts 屬性來自訂圖表屬性,例如標題和圖例的位置。

我如何在 C# 中切換不同的圖表類型?

在 C# 中切換不同的圖表類型很簡單。只需更改 CreateChart 方法中的圖表類型參數即可在柱形圖、散點圖、折線圖、圓餅圖、條形圖或面積圖間切換。

在 C# 中編輯完 Excel 圖表後保存更改的方法是什麼?

編輯完 Excel 圖表後,使用 SaveAs 方法將更改保存到工作簿,確保所有修改保留下來。

我可以使用 C# 刪除 Excel 工作表中的現有圖表嗎?

可以,您可以透過工作表的 Charts 屬性檢索現有圖表,然後使用 RemoveChart 方法刪除它。

使用 IronXL 在 .NET 應用程式中創建圖表有哪些好處?

IronXL 允許在 .NET 應用程式中有效地創建和編輯圖表,而不依賴於 Excel Interop,這提高了性能並降低了複雜性。它支持各種圖表類型,並提供易於自訂的方法。

在使用 AddSeries 方法添加數據系列後,如何在 C# 中繪製圖表?

使用 AddSeries 方法添加數據系列後,可以通過調用 Plot 方法繪製圖表,該方法基於提供的數據生成圖表。

在 C# 中創建圖表後,是否可以編輯圖表屬性?

是的,您可以透過工作表的 Charts 屬性訪問圖表,在 C# 中編輯其屬性,如圖表標題、圖例位置等。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 1,738,553 | Version: 2025.11 剛發表