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

How to Use C# to Create Excel Charts

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

以下操作指南幫助你使用IronXL在C#中編程創建Excel圖表。

快速入門:使用C#編程生成Excel圖表)

此範例展示了如何在IronXL中輕松構建柱形圖:使用單一的工作表方法創建圖表、添加數據系列、設置標題和圖例、繪製並保存——在幾分鐘內獲得有意義的可視化,而不是幾個小時。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    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. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

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

  1. 安裝Excel庫以創建Excel圖表。
  2. 將現有Excel文件加載到Workbook對象中。
  3. 使用CreateChart創建圖表。
  4. 設置圖表的標題和圖例。
  5. 調用Plot方法。
  6. Workbook保存到Excel文件中。
class="learnn-how-section">
class="row">
class="col-sm-6">

在.NET中編程創建Excel圖表

  • 通過編程創建Excel圖形
  • 添加具有標題和圖例的系列
class="col-sm-6">
class="download-card">How To Work related to How to Use C# to Create Excel Charts

class="main-content__segment-title">步驟 1

1. 安裝IronXL

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

  • 選擇項目菜單
  • 管理NuGet包
  • 搜索IronXL.Excel
  • 安裝

您還可以在開發者命令提示符中輸入以下命令:

Install-Package IronXL.Excel

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


class="main-content__segment-title">教程指南

2. 為.NET創建Excel圖表

現在開始項目!

將以下詳細信息添加到Excel電子表格中。 如下所示:

class="content-img-align-center">
class="center-image-wrapper">Data to be used for charting

圖1 用於製圖的數據

添加所需的命名空間以在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方法來指定圖表的類型和位置。 圖表的系列已添加,並包含其標題和圖例。 如下所示。

class="content-img-align-center">
class="center-image-wrapper">Chart output

圖2圖表輸出


class="main-content__segment-title">庫快速訪問

class="tutorial-section">
class="row">
class="col-sm-8">

IronXL API 參考文檔

了解更多並分享如何合併、取消合併以及使用 Excel 試算表中的單元格的便捷 IronXL API 參考文檔。

IronXL API 參考文檔
class="col-sm-4">
class="tutorial-image"> Documentation related to 2. 為.NET創建Excel圖表

常見問題解答

如何使用 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,686,155 | 版本: 2025.11 剛剛發布