How to Create and Edit Excel Charts in C
IronXL 讓 C# 開發人員能夠透過簡單的 API 呼叫,以程式化方式建立、編輯及移除 Excel 圖表。 您可直接從資料生成柱狀圖、折線圖、圓餅圖及其他圖表類型,無需依賴 Excel Interop。
在 Excel 中,圖表是資料的圖形化呈現,用於以視覺化方式顯示和分析資訊。 Excel 提供多種圖表類型,例如條形圖、線圖和圓餅圖,每種圖表皆適用於不同的資料與分析需求。 When working with IronXL's comprehensive Excel library, you can programmatically create these visualizations to enhance your reports and dashboards.
快速入門:幾秒鐘內建立並繪製線圖
透過 IronXL,您只需幾行程式碼,即可完成安裝、載入工作簿、呼叫 CreateChart、新增資料系列、設定標題與圖例位置,以及 Plot 等操作。 此範例展示如何使用原生 C# 方法建立圖表,無需承受 Interop 的額外負擔。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronXL.Excel
PM > Install-Package IronXL.Excel -
請複製並執行此程式碼片段。
// Load workbook and create a line chart with data series var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; // Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot(); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronXL
簡化工作流程(5 個步驟)
- 下載 C# 函式庫以建立和編輯圖表
- 準備製作圖表所需的資料
- 請使用
CreateChart方法來設定圖表類型與位置 - 請使用
AddSeries方法來新增資料系列 - 使用
Plot方法繪製圖表
開始使用 IronXL
如何在 Excel 中建立圖表?
IronXL 支援柱狀圖、散點圖、線圖、圓餅圖、條形圖及面積圖。 要建立圖表,請指定以下元件。 This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs.
- 使用
CreateChart來指定圖表類型和工作表位置。 - 使用
AddSeries添加系列。 此方法對於某些圖表類型僅接受單一欄位。第一個參數為橫軸數值。 第二項是縱軸數值。 - 可選地指定系列名稱、圖表名稱及圖例位置。
- 呼叫
Plot以渲染圖表。 多次呼叫會產生多個圖表。
Let's create charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:
製作柱狀圖的流程為何?
柱狀圖非常適合用於比較不同類別之間的數值。 When you load spreadsheet data, you can visualize it effectively using column charts to highlight differences between data points. 以下範例展示如何使用動物族群數據建立多系列柱狀圖:
: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")
如何建立線圖?
線圖特別擅長呈現隨時間變化的趨勢。由於線圖與柱狀圖顯示相同資訊,因此只需變更圖表類型即可在兩者之間切換。 這使得線圖在讀取包含時間序列資料的 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")
何時該使用圓餅圖?
圓形圖表用以顯示整體中的比例與百分比。 對於圓形圖,僅需一欄數據,因此更易於實作。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution:
: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")
如何編輯現有的圖表?
在處理現有 Excel 檔案時,您可能需要修改已建立的圖表。 IronXL 提供直觀的方法來編輯現有圖表,讓您能夠更新標題、重新定位圖例,以及刷新資料。 This is useful when editing Excel files that contain pre-existing visualizations.
您可以在現有圖表中編輯圖例位置和圖表標題。 若要編輯圖表,請先透過存取 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")
前言
之後
如何從 Excel 中移除圖表?
有時您需要清理 Excel 檔案,刪除過時或不必要的圖表。 This is common when managing worksheets containing multiple visualizations. 若要從試算表中移除現有圖表,請先從 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")
進階圖表自訂
除了基本的圖表建立功能外,IronXL 還支援進階自訂選項。 When creating complex reports or dashboards, you can combine charts with other Excel features like conditional formatting to create comprehensive data visualizations.
在商業應用中,圖表通常需要透過資料庫查詢或即時資料來源進行動態生成。 IronXL 可與 .NET 資料結構無縫整合,讓您能從 Lists 或任何可枚舉集合中建立圖表。 這使其非常適合用於生成包含視覺元素的自動化報告。
摘要
IronXL 提供了一套完整的解決方案,用於在 C# 應用程式中處理 Excel 圖表。 無論是建立新的視覺化圖表、修改現有圖表,還是移除過時的圖表,此函式庫皆提供直覺的方法,且無需使用 Excel Interop。透過將圖表功能與 IronXL 的其他功能(如資料處理和格式設定)結合,您可以建立精緻的 Excel 自動化解決方案,藉此提升 .NET 應用程式中的資料呈現與分析能力。
常見問題
如何在不使用 Interop 的情況下,透過 C# 程式碼建立 Excel 圖表?
IronXL 提供一個簡單的 API,讓您能在 C# 中建立 Excel 圖表,且無需依賴 Interop。您可以使用 CreateChart 方法指定圖表類型與位置,透過 AddSeries 添加資料,並使用 Plot 渲染圖表——所有操作皆透過原生 C# 程式碼完成。
我可以在 Excel 試算表中使用 C# 建立哪些類型的圖表?
IronXL 支援建立多種圖表類型,包括柱狀圖、散點圖、線圖、圓餅圖、條形圖及面積圖。您可在呼叫 CreateChart 方法時指定圖表類型,並透過標題、圖例及資料系列來自訂每個圖表。
如何透過程式碼將資料系列新增至 Excel 圖表?
請使用 IronXL 的 AddSeries 方法將資料新增至圖表。此方法接受儲存格範圍作為參數——第一個參數用於水平軸值,第二個參數用於垂直軸值。您可以新增多個系列以建立多系列圖表。
using C# 在 Excel 中建立折線圖的最快方法是什麼?
透過 IronXL,您只需幾行程式碼即可建立折線圖:使用 CreateChart(ChartType.Line) 初始化圖表,透過 AddSeries() 新增資料範圍,使用 SetTitle() 設定圖表標題,並透過 Plot() 將其繪製於工作表上。
我可以自訂圖表的屬性,例如標題和圖例的位置嗎?
是的,IronXL 允許完全自訂 Excel 圖表。您可以使用 SetTitle() 新增圖表標題,使用 SetLegendPosition() 設定圖例位置(頂部、底部、左側、右側),並可選擇指定系列名稱以利識別資料。
我需要安裝 Microsoft Excel 才能透過程式碼建立圖表嗎?
不,IronXL 可獨立運作,無需安裝 Microsoft Excel。它內部處理所有 Excel 檔案操作與圖表建立,因此非常適合無法安裝 Excel 的伺服器環境與應用程式。

