如何在 C# 中建立和編輯 Excel 圖表
IronXL 能讓 C# 開發人員使用簡單的 API 呼叫,以程式化的方式建立、編輯和移除 Excel 圖表。 您可以直接從資料產生柱狀圖、線狀圖、圓餅圖和其他圖表類型,而不需要依賴 Excel Interop。
在 Excel 中,圖表是用來直觀顯示和分析資訊的資料圖表。 Excel 提供各種圖表類型,例如柱狀圖、折線圖和圓餅圖,每種圖表適合不同的資料和分析需求。 在處理 IronXL comprehensive Excel library 時,您可以以程式化方式創建這些視覺化效果,以增強您的報告和儀表板。
快速入門:在幾秒內創建和繪製折線圖使用IronXL,您可以安裝、載入工作簿、呼叫CreateChart,新增您的資料系列、設定標題和圖例位置,並Plot - 所有這些只需幾行程式碼。 本範例說明如何使用原生 C# 方法建立圖表,而不會產生 Interop 開銷。
-
1使用 IronXL 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
// Load workbook and create a line chart, then add a data series IChart chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10); IChartSeries series = chart.AddSeries("A2:A10", "B2:B10"); series.Title = workSheet["B1"].StringValue; // Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart"); chart.SetLegendPosition(LegendPosition.Bottom); chart.Plot();' Load workbook and create a line chart, then add a data series Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10) Dim series As IChartSeries = chart.AddSeries("A2:A10", "B2:B10") series.Title = workSheet("B1").StringValue ' Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart") chart.SetLegendPosition(LegendPosition.Bottom) chart.Plot()' Load workbook and create a line chart, then add a data series Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10) Dim series As IChartSeries = chart.AddSeries("A2:A10", "B2:B10") series.Title = workSheet("B1").StringValue ' Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart") chart.SetLegendPosition(LegendPosition.Bottom) chart.Plot() -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronXL,並使用 免費試用
最小工作流程(5 個步驟)
- 下載C#程式庫以創建和編輯圖表
- 準備創建圖表所需的資料
- 使用
CreateChart方法設定圖表類型和位置 - 使用
AddSeries方法新增資料系列 - 使用
Plot方法繪製圖表
開始使用 IronXL
如何在Excel中創建圖表?
IronXL 支援長條圖、散佈圖、折線圖、圓餅圖、長條圖和麵積圖。 若要建立圖表,請指定下列元件。 這種靈活性允許您創建具有豐富視覺化效果的Excel試算表,以滿足您的數據展示需求。
- 使用
CreateChart指定圖表類型和工作表位置。 - 使用
AddSeries新增系列。 對於某些圖表類型,此方法接受單列。第一個參數是水平軸值。 第二是垂直軸值。 - 可選擇性地指定系列名稱、圖表名稱和圖例位置。
- 呼叫
Plot來渲染圖表。 多重呼叫可建立多個圖表。
讓我們從chart.xlsx Excel文件中的數據創建圖表。以下顯示數據預覽:

創建柱狀圖的過程是什麼?
柱狀圖是比較不同類別數值的理想工具。 當您載入試算表數據時,您可以使用柱狀圖有效地視覺化它,以突出數據點之間的差異。 以下範例示範使用動物數量資料建立多序列縱列圖表:
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");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")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")
如何創建折線圖?
折線圖最擅長顯示隨時間變化的趨勢。由於折線圖與柱狀圖顯示相同的資訊,因此在兩者之間切換只需變更圖表類型即可。 當在包含時間序列數據的 閱讀XLSX文件時,折線圖特別有用:
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.Line, 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");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.Line, 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
Dim workBook As WorkBook = WorkBook.Load("chart.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 5, 5, 20, 10)
Dim xAxis As String = "A2:A7"
' Add the series
Dim 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")Imports IronXL
Imports IronXL.Drawing.Charts
Dim workBook As WorkBook = WorkBook.Load("chart.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 5, 5, 20, 10)
Dim xAxis As String = "A2:A7"
' Add the series
Dim 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")
何時應該使用圓餅圖?
餅狀圖顯示整體的比例和百分比。 對於圓餅圖,只需要一列資料,因此實作起來較為簡單。 當您想將試算表數據轉換為市場份額、預算分配或類別分佈的可視化表示時,它們是有效的:
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");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")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")
如何編輯現有圖表?
使用現有 Excel 檔案時,可能需要修改已建立的圖表。 IronXL 提供編輯現有圖表的直接方法,讓您可以更新標題、重新定位圖例和刷新資料。 這在編輯含有現有視覺化效果的Excel文件時很有用。
您可以編輯現有圖表中的圖例位置和圖表標題。 若要編輯圖表,首先通過訪問Charts屬性檢索它並選擇目標圖表。 然後存取圖表的屬性來進行編輯:
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");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")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")
前

後
如何從Excel中移除圖表?
有時您需要移除過時或不必要的圖表來清理 Excel 檔案。 這在管理含有多個視覺化效果的工作表時很常見。 要從試算表中移除現有的圖表,首先從Charts屬性檢索圖表。 您將收到一份圖表清單。 將目標圖表對象傳遞給RemoveChart:
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");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")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")高級圖表自定義
除了基本的圖表建立之外,IronXL 還支援進階的客製化選項。 在創建複雜的報告或儀表板時,您可以將圖表與其他Excel功能結合使用,如條件格式設定,以創建全面的數據可視化。
對於商業應用程式,圖表通常需要從資料庫查詢或即時資料來源動態產生。 IronXL與.NET資料結構整合,讓您可以從DataTables、Lists或任何可列舉的集合中建立圖表。 這使得它非常適合產生包含視覺元素的自動化報告。
總結
IronXL 提供在 C# 應用程式中使用 Excel 圖表的完整解決方案。 無論是建立新的視覺化、修改現有的視覺化,或是移除過時的圖表,圖庫都提供了不需要 Excel Interop 的直覺方法。透過將圖表功能與 IronXL 的其他功能(如資料處理與格式化)相結合,您可以建立複雜的 Excel 自動化解決方案,強化 .NET 應用程式中的資料呈現與分析功能。
常見問題
使用IronXL,我可以創建哪些類型的Excel圖表?
IronXL支持創建各種Excel圖表,例如欄形圖、折線圖、圓餅圖、長條圖、散佈圖和面積圖,並且無需Excel Interop就可以直接在C#應用程式中使用。
如何使用IronXL創建折線圖?
要使用IronXL創建折線圖,請安裝該程式庫,載入您的工作簿,使用 CreateChart 方法將圖表類型設置為折線,使用 AddSeries 添加您的數據序列,然後調用 Plot 來繪製圖表。
IronXL能幫助編輯現有的Excel圖表嗎?
是的,IronXL提供了編輯現有Excel圖表的方法,讓您能輕鬆更新圖表標題、變更圖例位置,並刷新數據。
使用IronXL是否可以刪除Excel工作表中的圖表?
是的,IronXL允許您通過訪問 Charts 屬性並在所需圖表上調用 RemoveChart,從Excel工作表中刪除圖表。
如何使用IronXL進一步自定義Excel圖表?
除了基本的圖表創建外,IronXL還支持進階的自定義功能,例如從資料庫查詢動態生成圖表,以及整合.NET資料結構以進行全面的數據可視化。
使用IronXL創建圖表相比於Excel Interop有何一個優勢?
IronXL能在C#中創建和編輯圖表,而不依賴Excel Interop,提供了一種更簡單且高效的程式化處理Excel文件的方法。
如何使用IronXL創建欄形圖?
要創建欄形圖,使用IronXL載入您的工作簿,使用 CreateChart 設置圖表類型,使用 AddSeries 添加數據序列,並調用 Plot 來最終顯示圖表。
IronXL是否可以處理即時數據來創建圖表?
是的,IronXL與 DataTables 和 Lists 等.NET資料結構整合,這使得它能創建來自即時數據來源的圖表,並生成自動報告。
為何我應該考慮使用IronXL的圓餅圖?
圓餅圖有助於顯示整體的比例和百分比,透過IronXL,您可以輕鬆實現它們,以視覺化地展示市場份額、預算分配或類別分佈。
使用IronXL是否可以在不安裝Excel的情況下創建Excel圖表?
是的,IronXL獨立於Excel運行,允許您程式化地創建和操作Excel圖表,而不需要在您的系統上安裝Excel。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。