如何使用 C# 建立 Excel 圖表
透過呼叫 CreateChart() 方法、使用 AddSeries() 新增資料序列、設定標題和圖例,然後再使用 Plot() 方法進行繪圖,即可在 C# 中使用 IronXL 製作 Excel 圖表 - 只需 5 行代碼即可完成完整的 Excel 可視化。
快速入門:使用 C# 以程式設計方式產生 Excel 圖表
在 IronXL 中建立柱狀圖:使用單一工作表方法建立圖表、新增資料序列、設定標題和圖例、繪製圖表並儲存。 如需 IronXL.Excel 功能的全面概述,請造訪我們的 Get Started Overview 頁面。
立即開始使用 NuGet 建立 PDF 檔案:
使用 NuGet 套件管理器安裝 IronXL
複製並運行這段程式碼。
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");部署到您的生產環境進行測試
最低限度的工作流程(6 個步驟)
- 安裝 Excel 庫以建立 Excel 圖表。
- 將現有的 Excel 檔案載入到
Workbook物件中。 - 使用
CreateChart建立圖表。 4.設定圖表的標題和圖例。 - 呼叫
Plot方法。 - 將
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>
<h2>如何安裝 IronXL.Excel 圖表製作?
使用 Visual Studio 中的 NuGet Package Manager 安裝 IronXL:
- 選擇"項目"選單
- 管理 NuGet 套件 搜尋 IronXL.Excel
- 安裝
本安裝提供 以程式化方式建立 Excel 圖表所需的所有工具。 IronXL 支援多種圖表類型,包括柱狀圖、線圖、圓餅圖、條狀圖、區域圖和散點圖。
<h3>為什麼要使用 NuGet 套件管理程式?
您也可以在 Developer Command Prompt 中輸入以下指令:
Install-Package IronXL.Excel
<h3>有哪些替代安裝方法?
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>
IronXL.Excel 安裝完成後,您可以 從頭建立新的 Excel 檔案,或使用現有的試算表,適合新專案和舊系統整合。
<hr class="separator">
<p class="main-content__segment-title">操作指南</p>
<h2>如何在 .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="Excel 電子表格,顯示 A 列至 D 列的銷售資料,標題為產品、Q1 銷售額、Q2 銷售額和 Q3 銷售額" > </a> <p><strong>圖 1</strong> –<em>用於繪製圖表的數據</em></p> </div> </div>
在建立圖表之前,請確保您的資料已妥善組織。 IronXL.Excel 可以 載入現有的試算表,您也可以程式化地建立試算表。 資料庫支援各種 Excel 檔案格式,包括 XLS、XLSX、CSV 和 TSV。
<h3>Excel 圖表需要哪些命名空間?
在 IronXL.Excel 中加入使用 Excel 圖表所需的命名空間:
using IronXL;
using IronXL.Drawing.Charts;using IronXL;
using IronXL.Drawing.Charts;Imports IronXL
Imports IronXL.Drawing.Charts這些命名空間提供所有圖表相關功能的存取權限。 IronXL 命名空間包含核心工作簿和工作表操作,而 IronXL.Drawing.Charts 則處理圖表建立和自訂功能。
<h3>如何在我的圖表中加入多個資料序列?
新增程式碼,使用 IronXL 以程式設計方式建立 Excel 圖表:
:path=/static-assets/excel/content-code-examples/how-to/csharp-create-excel-chart-programmatically-example.csusing 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.comCreateChart 方法需要圖表類型和位置 (左列、頂行、右列、底行) 的參數。 此定位使用儲存格座標,將您的圖表精確地放置在工作表中。 您可以在單一工作表上建立多個圖表,也可以將圖表分佈在不同的工作表上。
在處理資料序列時,IronXL.Excel 允許您使用標準 Excel 符號參照儲存格範圍。 這可讓熟悉 Excel 的開發人員直覺地過渡到程式化圖表製作。 此外,您可以將圖表與 Excel 公式結合,以建立資料變更時會自動更新的動態可視化。
<h3>如何建立不同的圖表類型?
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();IRON VB CONVERTER ERROR developers@ironsoftware.com<h3>最終的圖表輸出是什麼樣子?
建立一個 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="Excel 縱列圖表顯示季度銷售資料,包含三個序列,圖例位於底部,標題為 '縱列圖表" > <p><strong>圖 2</strong> –<em>圖表輸出</em></p> </div> </div>
由此產生的圖表可清楚直觀地呈現您的資料。 IronXL.Excel 會在幕後處理所有複雜的 Excel XML 格式,讓您專注於業務邏輯。 如需更多圖表製作範例,請查看我們的 Excel 圖表範例頁面。
進階圖表客製化
除了基本的圖表建立之外,IronXL 還提供廣泛的客製化選項。 您可以修改顏色、新增資料標籤、調整軸線比例,以及套用各種格式化選項。 圖庫的 Save & Export 功能可確保您的圖表在不同的 Excel 格式和版本中都能正確保存。
在處理大型資料集時,請考慮在建立圖表之前先實作分頁或資料篩選。 此方法可確保最佳效能,並避免雜亂無章的可視化。 IronXL.Excel 的高效記憶體管理可讓您處理大量 Excel 檔案而不會降低效能。
<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>Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API 參考文檔.</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.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"),可新增來自這些範圍值的序列。






