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

如何使用 C# 建立 Excel 圖表

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

以下操作指南將指導您如何使用 IronXL 在 C# 中以程式設計方式建立 Excel 圖表。

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

本範例向您展示了在 IronXL 中建立長條圖是多麼容易:使用單一工作表方法建立圖表,新增資料系列,設定標題和圖例,繪製圖表,然後儲存-幾分鐘內即可獲得有意義的視覺效果,而不是幾個小時。

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

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

    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

as-heading:3(最小工作流程(6 個步驟))

  1. 安裝 Excel 庫以建立 Excel 圖表。
  2. 將現有的 Excel 檔案載入到Workbook物件中。
  3. 使用CreateChart建立圖表。
  4. 設定圖表標題和圖例
  5. 呼叫Plot方法。
  6. Workbook儲存為 Excel 檔案。

<div class="learnn-how-section"> <div class="row"> <div class="col-sm-6"> <h2>使用 .NET 以程式設計方式建立 Excel 圖表</h2> <ul class="list-unstyled"> <li>以程式設計方式建立 Excel 圖表</li> <li>新增系列標題和圖例</li> </ul> </div> <div class="col-sm-6"> <div class="download-card"> <img style="box-shadow: none; width: 308px; height: 320px;" src="/img/faq/excel/how-to-work.svg" class="img-responsive learn-how-to-img replaceable-img"> </div> </div> </div> </div>

<hr class="separator">

<p class="main-content__segment-title">步驟 1</p>

1. 安裝 IronXL

首先,安裝 IronXL 最簡單的方法是使用 Visual Studio 中的 NuGet 套件管理器:

  • 選擇"項目"選單
  • 管理 NuGet 套件 搜尋 IronXL.Excel
  • 安裝

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

Install-Package IronXL.Excel

Or Download from here: <a class="js-modal-open" href="/csharp/excel/packages/IronXL.zip" data-modal-id="trial-license-after-download">https://ironsoftware.com/csharp/excel/packages/IronXL.zip</a>

<hr class="separator">

<p class="main-content__segment-title">操作指南</p>

2. 為 .NET 建立 Excel 圖表

現在開始專案!

請將以下資訊加入Excel表格中。 如下圖所示:

<div class="content-img-align-center"> <div class="center-image-wrapper"> <a href="/img/faq/excel/csharp-create-excel-chart-programmatically/data-to-be-used-for-charting.png" target="_blank" > <img class="img-responsive" src="/img/faq/excel/csharp-create-excel-chart-programmatically/data-to-be-used-for-charting.png" alt="用於圖表的資料" > </a> <p><strong>圖 1</strong> –<em>用於繪製圖表的數據</em></p> </div> </div>

在 IronXL 中新增處理 Excel 圖表所需的命名空間。

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

新增程式碼,使用 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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

建立了一個 Workbook 物件和一個 Worksheet 物件。 呼叫 Worksheet 物件的CreateChart方法來指定圖表類型和圖表位置。 圖表系列將被添加,包括其標題和圖例。 如下圖所示。

<div class="content-img-align-center"> <div class="center-image-wrapper"> <img class="img-responsive" src="/img/faq/excel/csharp-create-excel-chart-programmatically/chart-output.png" alt="圖表輸出" > <p><strong>圖 2</strong> –<em>圖表輸出</em></p> </div> </div>

<hr class="separator">

<p class="main-content__segment-title">圖書館快速訪問</p>

<div class="tutorial-section"> <div class="row"> <div class="col-sm-8"> <h3>IronXL API 參考文檔</h3> <p>使用便利的 IronXL API 參考文檔,了解更多並分享如何在 Excel 電子表格中合併、取消合併和處理儲存格。</p> <a class="doc-link" href="/csharp/excel/object-reference/api/" target="_blank"> IronXL API 參考文檔 <i class="fa fa-chevron-right"></i></a> </div> <div class="col-sm-4"> <div class="tutorial-image"> <img style="max-width: 110px; width: 100px; height: 140px;" alt="" class="img-responsive add-shadow" src="/img/svgs/documentation.svg" width="100" height="140"> </div> </div> </div> </div>

常見問題解答

如何使用 C# 程式化建立 Excel 圖表?

您可以利用 IronXL 函式庫使用 C# 程式化建立 Excel 圖表。首先,透過 NuGet 封裝管理器在 Visual Studio 中安裝 IronXL,將現有的 Excel 文件載入到 Workbook 物件中,並使用 CreateChart 方法定義圖表類型和位置。添加數據系列、設定標題,並將 Workbook 儲存到 Excel 文件。

程式化建立 Excel 圖表需遵循哪些步驟?

程式化建立 Excel 圖表的步驟包括:安裝 IronXL,將現有的 Excel 文件載入 Workbook 物件中,使用 CreateChart 方法建立圖表,添加數據系列,設定圖表的標題和圖例,並儲存工作簿。

IronXL 支持哪些類型的圖表創建?

IronXL 支持程式化創建各種類型的圖表,例如柱狀圖、條形圖、折線圖和圓餅圖。

如何使用 C# 向 Excel 圖表添加數據系列?

要使用 C# 向 Excel 圖表添加數據系列,利用 IronXL 的 AddSeries 方法。指定 x 軸和 y 軸數據的範圍,並可選擇性地為系列設定標題。

如何程式化自訂 Excel 圖表的圖例位置?

您可以利用 IronXL 的 SetLegendPosition 方法程式化自訂 Excel 圖表的圖例位置。指定其位置,例如底部、頂部、左側或右側。

處理 IronXL 中 Excel 圖表需要哪些命名空間?

若要使用 IronXL 處理 Excel 圖表,請包含必要的命名空間:IronXLIronXL.Drawing.Charts

程式化添加圖表後,如何儲存 Excel 文件?

在程式化添加圖表後,使用 IronXL 的 SaveAs 方法儲存 Excel 文件。指定所需的文件路徑和名字以儲存。

IronXL 可以用於修改現有的 Excel 文件嗎?

是的,IronXL 可以用於載入、編輯和儲存現有的 Excel 文件。允許你程式化修改數據、添加圖表及執行其他操作。

在哪裡可以找到使用 IronXL 配合 Excel 圖表的文件?

您可以在他們的網站上查看 IronXL API 參考文件,有關其功能和如何與 Excel 圖表配合使用的詳細資訊。

Curtis Chau
技術作家

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

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

準備好開始了嗎?
Nuget 下載 1,738,553 | Version: 2025.11 剛發表