如何使用 C# 建立 Excel 圖表
在C#中使用IronXL通過調用CreateChart()方法創建Excel圖表,添加數據系列使用AddSeries(),配置標題和圖例,然後使用Plot()方法進行繪製 —— 僅需幾行代碼即可完成Excel視覺化。
在 IronXL 中建立柱狀圖:使用單一工作表方法建立圖表、新增資料序列、設定標題和圖例、繪製圖表並儲存。 若要全面了解IronXL的功能,請造訪我們的入門概覽頁面。
-
1使用 IronXL 安裝 NuGet Package Manager
-
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");Dim chart As IChart = 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")Dim chart As IChart = 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,並使用 免費試用
最小工作流程(6步)
- 安裝 Excel 庫以建立 Excel 圖表。
- 將現有的Excel文件加載到
Workbook對象中。 - 使用
CreateChart創建圖表。 - 設置圖表的標題和圖例。
- 調用
Plot方法。 - 將
Workbook保存到Excel文件中。
使用 .NET 以程式設計方式建立 Excel 圖表
- 以程式設計方式建立 Excel 圖表
- 新增系列標題和圖例
步驟 1
如何安裝IronXL以創建Excel圖表?
使用 Visual Studio 中的 NuGet Package Manager 安裝 IronXL:
- 選擇「項目」選單
- 管理 NuGet 套件
- 搜索
IronXL.Excel - 安裝
此安裝提供了以編程方式創建Excel圖表所需的所有工具。 IronXL 支援多種圖表類型,包括柱狀圖、線圖、圓餅圖、條狀圖、區域圖和散點圖。
為什麼我應該使用NuGet包管理器?
您也可以在 Developer Command Prompt 中輸入以下指令:
有哪些替代安裝方法?
或者從這裡下載:https://ironsoftware.com/csharp/excel/packages/IronXL.zip
安裝後,IronXL允許您從頭創建新的Excel文件或使用現有的電子表格,適合新專案和舊系統整合。
操作指南
如何在.NET中以編程方式創建Excel圖表?
將下列資料加入 Excel 試算表中:
在建立圖表之前,請確保您的資料已妥善組織。 IronXL可以加載現有的電子表格,或者您可以以編程方式創建它們。 資料庫支援各種 Excel 檔案格式,包括 XLS、XLSX、CSV 和 TSV。
創建Excel圖表需要哪些命名空間?
在 IronXL.Excel 中加入使用 Excel 圖表所需的命名空間:
using IronXL;
using IronXL.Drawing.Charts;using IronXL;
using IronXL.Drawing.Charts;Imports IronXL
Imports IronXL.Drawing.ChartsImports IronXL
Imports IronXL.Drawing.Charts這些命名空間提供所有圖表相關功能的存取權限。 IronXL命名空間包含核心工作簿和工作表操作,而IronXL.Drawing.Charts處理圖表的創建和自定義功能。
如何向我的圖表添加多個數據系列?
新增程式碼,使用 IronXL 以程式設計方式建立 Excel 圖表:
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");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");Imports IronXL
Imports IronXL.Drawing.Charts
' Load the existing Excel workbook
Dim wb As WorkBook = WorkBook.Load("Chart_Ex.xlsx")
' Use the default worksheet from the workbook
Dim ws As WorkSheet = wb.DefaultWorkSheet
' Create a column chart at the specified range of cells
Dim chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20)
' Define the range for the x-axis data
Const xAxis As String = "A2:A7"
' Add a series for the chart using data in the range and give it a title from the first row
Dim 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")Imports IronXL
Imports IronXL.Drawing.Charts
' Load the existing Excel workbook
Dim wb As WorkBook = WorkBook.Load("Chart_Ex.xlsx")
' Use the default worksheet from the workbook
Dim ws As WorkSheet = wb.DefaultWorkSheet
' Create a column chart at the specified range of cells
Dim chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20)
' Define the range for the x-axis data
Const xAxis As String = "A2:A7"
' Add a series for the chart using data in the range and give it a title from the first row
Dim 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")CreateChart方法接受圖表類型和位置的參數(左列、頂行、右列、底行)。 此定位使用儲存格座標,將您的圖表精確地放置在工作表中。 您可以在單一工作表上建立多個圖表,也可以將圖表分佈在不同的工作表上。
在處理資料序列時,IronXL.Excel 允許您使用標準 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 a column chart with multiple series
var comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25);
comboChart.AddSeries("A2:A7", "B2:B7"); // First column series
comboChart.AddSeries("A2:A7", "C2:C7"); // Second column series
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 a column chart with multiple series
var comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25);
comboChart.AddSeries("A2:A7", "B2:B7"); // First column series
comboChart.AddSeries("A2:A7", "C2:C7"); // Second column series
comboChart.Plot();' Create a line chart for trend analysis
Dim lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32)
' Add data series with smooth lines
Dim 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 a column chart with multiple series
Dim comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25)
comboChart.AddSeries("A2:A7", "B2:B7") ' First column series
comboChart.AddSeries("A2:A7", "C2:C7") ' Second column series
comboChart.Plot()' Create a line chart for trend analysis
Dim lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32)
' Add data series with smooth lines
Dim 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 a column chart with multiple series
Dim comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25)
comboChart.AddSeries("A2:A7", "B2:B7") ' First column series
comboChart.AddSeries("A2:A7", "C2:C7") ' Second column series
comboChart.Plot()最終的圖表輸出是什麼樣子?
創建了Workbook物件和Worksheet物件。 調用了Worksheet物件的CreateChart方法以指定圖表類型和圖表位置。 圖表的序列會加上標題和圖例。

圖2 - 圖表輸出
由此產生的圖表可清楚直觀地呈現您的資料。 IronXL.Excel 會在幕後處理所有複雜的 Excel XML 格式,讓您專注於業務邏輯。 欲了解更多圖表創建例子,請查閱我們的Excel圖表範例頁面。
高級圖表自定義
除了基本的圖表建立之外,IronXL 還提供廣泛的客製化選項。 您可以修改顏色、新增資料標籤、調整軸線比例,以及套用各種格式化選項。 程式庫的保存和導出功能確保您的圖表在不同Excel格式和版本之間正確保留。
在處理大型資料集時,請考慮在建立圖表之前先實作分頁或資料篩選。 此方法可確保最佳效能,並避免雜亂無章的可視化。 IronXL.Excel 的高效記憶體管理可讓您處理大量 Excel 檔案而不會降低效能。
圖書館快速訪問
常見問題
如何使用C#以程式方式建立Excel圖表?
您可以使用IronXL程式庫以程式方式在C#中建立Excel圖表。首先,將您的Excel文件載入到Workbook物件中。然後,使用工作表的CreateChart方法指定圖表類型和位置。使用AddSeries方法添加資料系列,然後在呼叫Plot來呈現您的圖表之前設置圖表的標題和圖例。
在IronXL中,使用哪種方法將資料系列添加到Excel圖表中?
在IronXL中,您可以使用AddSeries方法將資料添加到Excel圖表中。此方法需要指定用於x軸和y軸資料的儲存格範圍。您也可以使用工作表中的值設置資料系列的標題。
如何安裝IronXL來創建Excel圖表?
可以使用Visual Studio中的NuGet套件管理器來安裝IronXL。只需前往專案選單,選擇管理NuGet套件,搜尋IronXL.Excel並安裝。這將提供所有必要的工具以程式方式創建Excel圖表。
IronXL支援哪些圖表類型?
IronXL支援各種圖表類型,包括柱狀圖、折線圖、餅圖、條形圖、區域圖和散佈圖。您可以使用IronXL提供的方法在C#中以程式方式創建和自訂這些圖表。
如何使用IronXL將多個資料系列添加到單個圖表中?
若要在IronXL中將多個資料系列添加到一個圖表中,可以使用AddSeries方法與CreateChart功能。為每個系列指定資料範圍,並使用工作表中的儲存格值設置個別標題。您可以根據需要向圖表添加多個資料系列。
IronXL能否處理大型Excel文件而不出現性能問題?
是的,IronXL被設計為能有效地管理記憶體,使其能夠在不顯著降低性能的情況下處理大型Excel文件。為了獲得最佳性能,考慮在創建圖表之前使用分頁或資料篩選。
創建IronXL Excel圖表需要哪些命名空間?
使用IronXL創建Excel圖表時,需要的命名空間為IronXL和IronXL.Drawing.Charts。IronXL命名空間促進核心的工作簿和工作表操作,而IronXL.Drawing.Charts則使得圖表創建和自訂得以實現。
IronXL支援哪些Excel圖表文件格式?
IronXL支援多種Excel文件格式,包括XLS、XLSX、CSV和TSV。這種靈活性允許您在各種格式下創建圖表,使其與不同的Excel版本和用案例兼容。
如何在IronXL中自訂圖表外觀?
IronXL為圖表提供了廣泛的自訂選項,包括修改顏色、添加資料標籤、調整坐標軸比例以及應用不同的格式樣式。這使您能夠創建視覺上吸引人且富有信息的Excel圖表。
使用IronXL程式庫創建Excel圖表的優勢是什麼?
IronXL簡化了在C#中以程式方式創建和管理Excel圖表的過程。它處理複雜的Excel XML格式,支援各種圖表類型,提供了廣泛的自訂選項,並能有效地管理大型文件的記憶體,讓開發者能專注於商業邏輯。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。
