IRONSOFTWAREHOME

How to Create and Edit Excel Charts in C#

Curtis Chau
Curtis Chau
Updated: 2026年4月22日

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 的額外負擔。

  1. 1Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

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

    // 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();
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronXL,透過免費試用
    arrow pointer

開始使用 IronXL


如何在 Excel 中建立圖表?

IronXL 支援柱狀圖、散點圖、線圖、圓餅圖、條形圖及面積圖。 要建立圖表,請指定以下元件。 This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs.

  1. 使用 CreateChart 來指定圖表型別和工作表位置。
  2. 使用 AddSeries 新增系列。 此方法對於某些圖表型別僅接受單一欄位。第一個參數為橫軸數值。 第二項是縱軸數值。
  3. 可選地指定系列名稱、圖表名稱及圖例位置。
  4. 呼叫 Plot 以渲染圖表。 多次呼叫會產生多個圖表。

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

包含樣本圖表資料的試算表,顯示 1 月至 6 月期間長頸鹿、大象和犀牛的每月數量

製作柱狀圖的流程為何?

柱狀圖非常適合用於比較不同類別之間的數值。 When you load spreadsheet data, you can visualize it effectively using column charts to highlight differences between data points. 以下範例展示如何使用動物族群資料建立多系列柱狀圖:

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");
Excel 試算表,顯示動物資料表及對應的分組柱狀圖,其中包含長頸鹿、大象和犀牛的每月數量

如何建立線圖?

線圖特別擅長呈現隨時間變化的趨勢。由於線圖與柱狀圖顯示相同資訊,因此只需變更圖表型別即可在兩者之間切換。 這使得線圖在讀取包含時間序列資料的 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");
Excel 試算表,顯示動物資料表及對應的線圖,其中包含長頸鹿、大象和犀牛的三條趨勢線

何時該使用圓餅圖?

圓形圖表用以顯示整體中的比例與百分比。 對於圓形圖,僅需一欄資料,因此更易於實作。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution:

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");
包含野生動物資料的試算表,以及顯示長頸鹿每月分佈的圓餅圖,其中 4 月以 89 隻長頸鹿(佔 21%)標示出來

如何編輯現有的圖表?

在處理現有 Excel 檔案時,您可能需要修改已建立的圖表。 IronXL 提供直觀的方法來編輯現有圖表,讓您能夠更新標題、重新定位圖例,以及刷新資料。 This is useful when editing Excel files that contain pre-existing visualizations.

您可以在現有圖表中編輯圖例位置和圖表標題。 若要編輯圖表,請先透過存取 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");
圓餅圖,顯示 1 月至 6 月的每月資料,以顏色區分各區段,並於下方附圖例
圓餅圖,顯示 1 月至 6 月的每月資料分布,並以顏色區分各區段及圖例

如何從 Excel 中移除圖表?

有時您需要清理 Excel 檔案,刪除過時或不必要的圖表。 This is common when managing worksheets containing multiple visualizations. 若要從試算表中移除現有圖表,請先從 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");

進階圖表自訂

除了基本的圖表建立功能外,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方法將資料新增到您的圖表中。該方法接受單元格範圍作為參數——第一個參數用於水平軸值,第二個參數用於垂直軸值。您可以新增多個系列以建立多系列圖表。

在Excel中使用C#建立折線圖的最快方法是什麼?

使用IronXL, 您可以在幾行程式碼中建立折線圖: 使用CreateChart(ChartType.Line)初始化圖表, AddSeries()以新增您的資料範圍, SetTitle() 用於圖表標題, 以及Plot() 將圖表渲染到工作表上。

我可以自定義圖表屬性,如標題和圖例位置嗎?

是的,IronXL允許完全自定義Excel圖表。您可以使用SetTitle()新增圖表標題,使用SetLegendPosition()放置圖例(上、下、左、右),並可選擇指定系列名稱以更好地識別資料。

我需要安裝Microsoft Excel來以編程方式建立圖表嗎?

不需要,IronXL可以獨立運作而不需要Microsoft Excel的安裝。它內部處理所有的Excel文件操作和圖表建立,非常適合不能安裝Excel的伺服器環境和應用程式。

How do I create a column chart using IronXL?

To create a column chart, load your workbook using IronXL, set the chart type with `CreateChart`, add data series using `AddSeries`, and call `Plot` to finalize and display the chart.

Can IronXL handle real-time data for chart creation?

Yes, IronXL integrates with .NET data structures like `DataTables` and `Lists`, making it ideal for creating charts from real-time data sources and generating automated reports.

Why should I consider using pie charts with IronXL?

Pie charts are useful for showing proportions and percentages of a whole, and with IronXL, you can easily implement them to visually represent market share, budget allocation, or category distribution.

Can I create Excel charts without Excel installed using IronXL?

Yes, IronXL operates independently of Excel, allowing you to create and manipulate Excel charts programmatically without needing Excel to be installed on your system.

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備好開始了嗎?

Nuget Downloads 2,237,574版本:2026.9剛剛發布

立即獲取免費

立即獲取 30天試用金鑰

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的免費30天試用金鑰
無需信用卡或帳戶建立
PDF的C# NuGet程式庫
NuGet安裝

版本: 2026.9

PM > Install-Package IronXL.Excel
nuget.org/packages/IronXL.Excel/
  1. 在方案資源管理器中,右鍵單擊參考,管理NuGet包
  2. 選擇瀏覽並搜尋“IronXL”
  3. 選擇包並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronXL到您的方案目錄中的比如~/Libs位置
  2. 在Visual Studio方案資源管理器中,右鍵單擊參考。選擇瀏覽,“IronXL.dll”

授權從$999

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲得無義務諮詢
填寫以下表格或電郵sales@ironsoftware.com
您的資料將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立