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

如何使用 C# 建立 Excel 圖表

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

使用IronXL在 C# 中建立 Excel 圖表,透過呼叫 CreateChart() 方法,使用 AddSeries() 新增資料系列,配置標題和圖例,然後使用 Plot() 方法繪製圖表 - 只需 5 行程式碼即可完成 Excel 視覺化。

快速入門:使用 C# 以程式設計方式產生 Excel 圖表

在IronXL中建立長條圖:使用單一工作表方法建立圖表,新增資料系列,設定標題和圖例,繪製圖表,然後儲存。 如需全面了解 IronXL 的功能,請造訪我們的"入門概覽"頁面。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronXl.Excel

    PM > Install-Package IronXl.Excel
  2. 複製並運行這段程式碼。

    IChart chart = worksheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
    chart.AddSeries("A2:A7", "B2:B7");
    chart.SetTitle("Sales Overview").SetLegendPosition(LegendPosition.Bottom).Plot();
    workbook.SaveAs("SalesChart.xlsx");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronXL

    arrow pointer

最簡工作流程(6 個步驟)

  1. 安裝 Excel 庫以建立 Excel 圖表。
  2. 將現有的 Excel 檔案載入到 Workbook 物件中。
  3. 建立一個包含 CreateChart 的圖表。
  4. 設定圖表標題和圖例。
  5. 呼叫 Plot 方法。
  6. Workbook 儲存到 Excel 檔案。
## 使用.NET以程式設計方式建立 Excel 圖表
  • 透過程式設計方式建立 Excel 圖表
  • 新增系列標題和圖例
How To Work related to 最簡工作流程(6 個步驟)

步驟 1

如何安裝IronXL以建立 Excel 圖表?

在 Visual Studio 中使用NuGet套件管理器安裝IronXL :

  • 選擇"項目"選單
  • 管理NuGet程式包
  • 搜尋IronXl.Excel
  • 安裝

此安裝程式提供了 以程式設計方式建立 Excel 圖表所需的所有工具。 IronXL支援多種圖表類型,包括長條圖、折線圖、圓餅圖、長條圖、面積圖和散佈圖。

我為什麼要使用NuGet套件管理器?

您也可以在開發者命令提示字元中輸入以下命令:

Install-Package IronXl.Excel

還有哪些安裝方法?

Or download from here: https://ironsoftware.com/csharp/excel/packages/IronXL.zip

安裝完成後, IronXL允許您從頭開始建立新的 Excel 文件,或處理現有的電子表格,適用於新專案和舊系統整合。


操作指南

如何在.NET中以程式設計方式建立Excel圖表?

將以下資料新增至 Excel 表格:

Excel電子表格,A列至D列顯示銷售數據,標題分別為產品、第一季銷售額、第二季銷售額及第三季銷售額

**圖 1** –*用於繪製圖表的數據*

在建立圖表之前,請確保您的資料已正確整理。 IronXL可以載入現有的電子表格,或者您也可以透過程式設計方式建立電子表格。 該庫支援多種 Excel 檔案格式,包括 XLS、XLSX、CSV 和 TSV。

Excel圖表需要哪些命名空間?

新增在IronXL中使用 Excel 圖表所需的命名空間:

using IronXL;
using IronXl.Drawing.Charts;
using IronXL;
using IronXl.Drawing.Charts;
$vbLabelText   $csharpLabel

這些命名空間提供了對所有圖表相關功能的存取權。 IronXL 命名空間包含核心工作簿和工作表操作,而 IronXl.Drawing.Charts 處理圖表建立和自訂功能。

如何在圖表中新增多個資料系列?

新增程式碼,使用IronXL以程式設計方式建立 Excel 圖表:

:path=/static-assets/excel/content-code-examples/how-to/csharp-create-excel-chart-programmatically-example.cs
using IronXL;
using IronXL.Drawing.Charts;

// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("Chart_Ex.xlsx");
// Use the default worksheet from the workbook
WorkSheet ws = wb.DefaultWorkSheet;

// Create a column chart at the specified range of cells
var chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20);

// Define the range for the x-axis data
const string xAxis = "A2:A7";

// Add a series for the chart using data in the range and give it a title from the first row
var series = chart.AddSeries(xAxis, "B2:B7");
series.Title = ws["B1"].StringValue;

// Add another series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = ws["C1"].StringValue;

// Add a third series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = ws["D1"].StringValue;

// Set the chart title
chart.SetTitle("Column Chart");
// Position the legend at the bottom of the chart
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart with the provided data
chart.Plot();
// Save the workbook with the newly added chart
wb.SaveAs("Exported_Column_Chart.xlsx");
$vbLabelText   $csharpLabel

CreateChart 方法接受圖表類型和位置(左列、頂行、右列、底行)的參數。 此定位方式使用儲存格座標將圖表精確放置在工作表中。 您可以在同一工作表中建立多個圖表,也可以將它們分佈在不同的表格中。

處理資料序列時, IronXL允許您使用標準的 Excel 表示法來引用儲存格區域。 這使得熟悉 Excel 的開發人員能夠直觀地過渡到程式化圖表建立。 此外,您也可以將圖表與Excel 公式結合使用,建立動態視覺化效果,以便在資料變更時自動更新。

如何建立不同類型的圖表?

IronXL除了長條圖之外,還支援多種其他圖表類型。 以下是一個建立自訂樣式折線圖的範例:

// Create a line chart for trend analysis
var lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32);

// Add data series with smooth lines
var trendSeries = lineChart.AddSeries("A2:A7", "B2:B7");
trendSeries.Title = "Q1 Sales Trend";

// Customize chart appearance
lineChart.SetTitle("Quarterly Sales Trend Analysis");
lineChart.SetLegendPosition(LegendPosition.Right);

// You can also create combination charts
var comboChart = ws.CreateChart(ChartType.ColumnClustered, 27, 15, 42, 25);
comboChart.AddSeries("A2:A7", "B2:B7"); // Column series
comboChart.AddSeries("A2:A7", "C2:C7"); // Can be styled as line

comboChart.Plot();
// Create a line chart for trend analysis
var lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32);

// Add data series with smooth lines
var trendSeries = lineChart.AddSeries("A2:A7", "B2:B7");
trendSeries.Title = "Q1 Sales Trend";

// Customize chart appearance
lineChart.SetTitle("Quarterly Sales Trend Analysis");
lineChart.SetLegendPosition(LegendPosition.Right);

// You can also create combination charts
var comboChart = ws.CreateChart(ChartType.ColumnClustered, 27, 15, 42, 25);
comboChart.AddSeries("A2:A7", "B2:B7"); // Column series
comboChart.AddSeries("A2:A7", "C2:C7"); // Can be styled as line

comboChart.Plot();
$vbLabelText   $csharpLabel

最終圖表輸出是什麼樣子的?

建立了 Workbook 物件和 Worksheet 物件。 呼叫 Worksheet 物件的 CreateChart 方法來指定圖表類型和圖表位置。 圖表中的系列會新增標題和圖例。

Excel長條圖,顯示季度銷售數據,包含三個系列,底部有圖例,標題為

**圖 2** –*圖表輸出*

產生的圖表可以清晰地直觀地呈現您的數據。 IronXL在後台處理所有複雜的 Excel XML 格式設置,讓您可以專注於業務邏輯。 如需查看更多圖表建立範例,請造訪我們的Excel 圖表範例頁面。

進階圖表自訂

除了基本的圖表建立功能外, IronXL還提供豐富的自訂選項。 您可以修改顏色、新增資料標籤、調整座標軸刻度,並套用各種格式設定選項。 該庫的儲存和匯出功能可確保您的圖表在不同的 Excel 格式和版本中正確儲存。

處理大型資料集時,請考慮在建立圖表之前實現分頁或資料篩選。 這種方法可確保最佳性能並防止可視化效果雜亂無章。 IronXL 的高效能記憶體管理功能可讓您處理大型 Excel 檔案而不會降低效能。


圖書館快速訪問

### IronXL API 參考文檔

Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy [IronXL API 參考文檔](https://ironsoftware.com/csharp/excel/object-reference/api/).

IronXL API 參考文檔
Documentation related to 進階圖表自訂

常見問題解答

如何以 C# 程式化方式建立 Excel 圖表?

您可以使用 IronXl.Excel 在 C# 中建立 Excel 圖表,方法是呼叫 CreateChart()、使用 AddSeries()加入資料序列、配置標題和圖例,然後再使用 Plot()方法進行繪圖。整個過程只需 5 行代碼即可完成。

我可以在 Excel 中以程式化方式建立哪些圖表類型?

IronXL 支援在 C# 應用程式中以程式化方式建立各種圖表類型,包括柱狀圖、折線圖、圓餅圖、條狀圖、區域圖和散點圖。

如何安裝用於在 C# 中建立 Excel 圖表的庫?

使用 Visual Studio 中的 NuGet 套件管理器安裝 IronXL,方法是選擇專案功能表 > 管理 NuGet 套件 > 搜尋 IronXl.Excel > 安裝。這提供了以程式化方式建立 Excel 圖表所需的所有工具。

我可以在程式化建立的 Excel 圖表中加入標題和圖例嗎?

是的,IronXL 允許您使用 SetTitle() 方法設置圖表標題,並使用 SetLegendPosition() 方法配置圖例位置,讓您完全控制圖表的外觀和佈局。

Excel 圖表製作支援哪些檔案格式?

IronXL 支援多種 Excel 檔案格式,包括 XLS、XLSX、CSV 和 TSV,讓您在以程式化方式建立圖表時,同時處理現代和傳統 Excel 檔案。

我可以從現有的 Excel 資料建立圖表,還是需要建立新的試算表?

IronXL 既可以載入現有的試算表,也可以從頭編程建立試算表,因此在產生 Excel 圖表時,既適用於新專案,也適用於舊系統整合。

在 C# 中建立 Excel 圖表的基本工作流程是什麼?

IronXL 的最小工作流程包括 6 個步驟:安裝 Excel 函式庫、將 Excel 檔案載入 Workbook 物件、使用 CreateChart 建立圖表、設定圖表的標題與圖例、呼叫 Plot 方法,並將 Workbook 儲存為 Excel 檔案。

如何在 Excel 圖表中加入資料序列?

使用 IronXL 中的 AddSeries() 方法為圖表新增資料序列,方法是指定資料的儲存格範圍,例如 chart.AddSeries("A2:A7", "B2:B7"),可新增來自這些範圍值的序列。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/ready_to_started_202509.php
Line: 12
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 489
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/ready_to_started_202509.php
Line: 19
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 489
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 1,890,100 | 版本: 2026.3 剛剛發布

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/still_scrolling_202512.php
Line: 17
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 71
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/still_scrolling_202512.php
Line: 24
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 71
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronXl.Excel
執行範例 觀看您的資料變成試算表。