IronXL 操作指南 創建和編輯圖表 如何在 C# 中建立和編輯 Excel 圖表 Curtis Chau 更新:2026年1月10日 下載 IronXL NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronXL 能讓 C# 開發人員使用簡單的 API 呼叫,以程式化的方式建立、編輯和移除 Excel 圖表。 您可以直接從資料產生柱狀圖、線狀圖、圓餅圖和其他圖表類型,而不需要依賴 Excel Interop。 在 Excel 中,圖表是用來直觀顯示和分析資訊的資料圖表。 Excel 提供各種圖表類型,例如柱狀圖、折線圖和圓餅圖,每種圖表適合不同的資料和分析需求。 When working with IronXL's comprehensive Excel library, you can programmatically create these visualizations to enhance your reports and dashboards. 快速入門:幾秒鐘內創建並繪製折線圖 有了 IronXL,您可以安裝、載入工作簿、呼叫 CreateChart 、新增您的資料序列、設定標題和圖例位置,以及 Plot - 全部只需幾行即可完成。 本範例說明如何使用原生 C# 方法建立圖表,而不會產生 Interop 開銷。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronXL PM > Install-Package IronXL.Excel 複製並運行這段程式碼。 // Load workbook and create a line chart with data series var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; // Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot(); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronXL,免費試用! 免費試用30天 ### 最小工作流程(5 個步驟) 下載用於建立和編輯圖表的 C# 庫。 準備創建圖表所需的數據 使用CreateChart方法設定圖表類型和位置 使用AddSeries方法新增資料系列 使用Plot方法繪製圖表 開始使用 IronXL 如何在 Excel 中建立圖表? IronXL 支援長條圖、散佈圖、折線圖、圓餅圖、長條圖和麵積圖。 若要建立圖表,請指定下列元件。 This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs. 1.使用 CreateChart 指定圖表類型和工作表位置。 2.使用 AddSeries 新增系列。 對於某些圖表類型,此方法接受單列。第一個參數是水平軸值。 第二是垂直軸值。 3.可選擇指定系列名稱、圖表名稱和圖例位置。 4.呼叫 Plot 以渲染圖表。 多重呼叫可建立多個圖表。 Let's create charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below: 建立縱列圖的流程是什麼? 柱狀圖是比較不同類別數值的理想工具。 When you load spreadsheet data, you can visualize it effectively using column charts to highlight differences between data points. 以下範例示範使用動物數量資料建立多序列縱列圖表: :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-column-chart.cs using IronXL; using IronXL.Drawing.Charts; WorkBook workBook = WorkBook.Load("chart.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Set the chart type and position IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10); string xAxis = "A2:A7"; // Add the series IChartSeries series = chart.AddSeries(xAxis, "B2:B7"); series.Title = workSheet["B1"].StringValue; // Add the series series = chart.AddSeries(xAxis, "C2:C7"); series.Title = workSheet["C1"].StringValue; // Add the series series = chart.AddSeries(xAxis, "D2:D7"); series.Title = workSheet["D1"].StringValue; // Set the chart title chart.SetTitle("Column Chart"); // Set the legend position chart.SetLegendPosition(LegendPosition.Bottom); // Plot the chart chart.Plot(); workBook.SaveAs("columnChart.xlsx"); Imports IronXL Imports IronXL.Drawing.Charts Private workBook As WorkBook = WorkBook.Load("chart.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Set the chart type and position Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10) Private xAxis As String = "A2:A7" ' Add the series Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7") series.Title = workSheet("B1").StringValue ' Add the series series = chart.AddSeries(xAxis, "C2:C7") series.Title = workSheet("C1").StringValue ' Add the series series = chart.AddSeries(xAxis, "D2:D7") series.Title = workSheet("D1").StringValue ' Set the chart title chart.SetTitle("Column Chart") ' Set the legend position chart.SetLegendPosition(LegendPosition.Bottom) ' Plot the chart chart.Plot() workBook.SaveAs("columnChart.xlsx") $vbLabelText $csharpLabel 如何建立折線圖? 折線圖最擅長顯示隨時間變化的趨勢。由於折線圖與柱狀圖顯示相同的資訊,因此在兩者之間切換只需變更圖表類型即可。 當 讀取包含時間序列資料的 XLSX 檔案時,這使得折線圖特別有用: :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs using IronXL; using IronXL.Drawing.Charts; WorkBook workBook = WorkBook.Load("chart.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Set the chart type and position IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10); string xAxis = "A2:A7"; // Add the series IChartSeries series = chart.AddSeries(xAxis, "B2:B7"); series.Title = workSheet["B1"].StringValue; // Set the chart title chart.SetTitle("Pie Chart"); // Set the legend position chart.SetLegendPosition(LegendPosition.Bottom); // Plot the chart chart.Plot(); workBook.SaveAs("pieChart.xlsx"); Imports IronXL Imports IronXL.Drawing.Charts Private workBook As WorkBook = WorkBook.Load("chart.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Set the chart type and position Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10) Private xAxis As String = "A2:A7" ' Add the series Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7") series.Title = workSheet("B1").StringValue ' Set the chart title chart.SetTitle("Pie Chart") ' Set the legend position chart.SetLegendPosition(LegendPosition.Bottom) ' Plot the chart chart.Plot() workBook.SaveAs("pieChart.xlsx") $vbLabelText $csharpLabel 何時應該使用圓餅圖? 餅狀圖顯示整體的比例和百分比。 對於圓餅圖,只需要一列資料,因此實作起來較為簡單。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution: :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs using IronXL; using IronXL.Drawing.Charts; WorkBook workBook = WorkBook.Load("chart.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Set the chart type and position IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10); string xAxis = "A2:A7"; // Add the series IChartSeries series = chart.AddSeries(xAxis, "B2:B7"); series.Title = workSheet["B1"].StringValue; // Set the chart title chart.SetTitle("Pie Chart"); // Set the legend position chart.SetLegendPosition(LegendPosition.Bottom); // Plot the chart chart.Plot(); workBook.SaveAs("pieChart.xlsx"); Imports IronXL Imports IronXL.Drawing.Charts Private workBook As WorkBook = WorkBook.Load("chart.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Set the chart type and position Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10) Private xAxis As String = "A2:A7" ' Add the series Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7") series.Title = workSheet("B1").StringValue ' Set the chart title chart.SetTitle("Pie Chart") ' Set the legend position chart.SetLegendPosition(LegendPosition.Bottom) ' Plot the chart chart.Plot() workBook.SaveAs("pieChart.xlsx") $vbLabelText $csharpLabel 如何編輯現有的圖表? 使用現有 Excel 檔案時,可能需要修改已建立的圖表。 IronXL 提供編輯現有圖表的直接方法,讓您可以更新標題、重新定位圖例和刷新資料。 This is useful when editing Excel files that contain pre-existing visualizations. 您可以編輯現有圖表中的圖例位置和圖表標題。 若要編輯圖表,請先存取 Charts 屬性並選擇目標圖表,以擷取該圖表。 然後存取圖表的屬性來進行編輯: :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs using IronXL; using IronXL.Drawing.Charts; WorkBook workBook = WorkBook.Load("pieChart.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Retrieve the chart IChart chart = workSheet.Charts[0]; // Edit the legend position chart.SetLegendPosition(LegendPosition.Top); // Edit the chart title chart.SetTitle("Edited Chart"); workBook.SaveAs("editedChart.xlsx"); Imports IronXL Imports IronXL.Drawing.Charts Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Retrieve the chart Private chart As IChart = workSheet.Charts(0) ' Edit the legend position chart.SetLegendPosition(LegendPosition.Top) ' Edit the chart title chart.SetTitle("Edited Chart") workBook.SaveAs("editedChart.xlsx") $vbLabelText $csharpLabel 前 後 如何從 Excel 中移除圖表? 有時您需要移除過時或不必要的圖表來清理 Excel 檔案。 This is common when managing worksheets containing multiple visualizations. 若要從試算表移除現有的圖表,請先從 Charts 屬性擷取圖表。 您將收到一份圖表清單。 將目標圖表物件傳給 RemoveChart : :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs using IronXL; using IronXL.Drawing.Charts; using System.Collections.Generic; WorkBook workBook = WorkBook.Load("pieChart.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Retrieve the chart List<IChart> chart = workSheet.Charts; // Remove the chart workSheet.RemoveChart(chart[0]); workBook.SaveAs("removedChart.xlsx"); Imports IronXL Imports IronXL.Drawing.Charts Imports System.Collections.Generic Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Retrieve the chart Private chart As List(Of IChart) = workSheet.Charts ' Remove the chart workSheet.RemoveChart(chart(0)) workBook.SaveAs("removedChart.xlsx") $vbLabelText $csharpLabel 進階圖表客製化 除了基本的圖表建立之外,IronXL 還支援進階的客製化選項。 When creating complex reports or dashboards, you can combine charts with other Excel features like conditional formatting to create comprehensive data visualizations. 對於商業應用程式,圖表通常需要從資料庫查詢或即時資料來源動態產生。 IronXL 與 .NET 資料結構無縫整合,可讓您從 DataTables, Lists 或任何可枚舉的集合中建立圖表。 這使得它非常適合產生包含視覺元素的自動化報告。 摘要 IronXL 提供在 C# 應用程式中使用 Excel 圖表的完整解決方案。 無論是建立新的視覺化、修改現有的視覺化,或是移除過時的圖表,圖庫都提供了不需要 Excel Interop 的直覺方法。透過將圖表功能與 IronXL 的其他功能(如資料處理與格式化)相結合,您可以建立複雜的 Excel 自動化解決方案,強化 .NET 應用程式中的資料呈現與分析功能。 常見問題解答 如何在不使用 Interop 的情況下,以 C# 程式化的方式建立 Excel 圖表? IronXL.Excel 提供了一個簡單的 API,可以在 C# 中建立 Excel 圖表,而不需要 Interop 依賴。您可以使用 CreateChart 方法來指定圖表類型和位置,使用 AddSeries 來新增資料,以及使用 Plot 來渲染圖表 - 所有這些都可以透過 C# 原始程式碼來完成。 使用 C# 可以在 Excel 試算表中建立哪些類型的圖表? IronXL 支援建立各種圖表類型,包括柱狀圖、散點圖、線圖、圓餅圖、條狀圖和面積圖。您可以在呼叫 CreateChart 方法時指定圖表類型,並使用標題、圖例和資料系列自訂每個圖表。 如何以程式化方式在 Excel 圖表中加入資料序列? 使用 IronXL 的 AddSeries 方法將資料新增至您的圖表。此方法接受儲存格範圍作為參數 - 第一個參數為水平軸值,第二個參數為垂直軸值。您可以添加多個序列來創建多序列圖表。 使用 C# 在 Excel 中建立折線圖的最快方法是什麼? 使用 IronXL,您只需幾行代碼即可創建線形圖:使用 CreateChart(ChartType.Line) 初始化圖表,AddSeries() 添加您的資料範圍,SetTitle() 設定圖表標題,Plot() 在工作表上渲染圖表。 我可以自訂標題和圖例位置等圖表屬性嗎? 是的,IronXL.Excel 允許完全自訂 Excel 圖表。您可以使用 SetTitle() 添加圖表標題,使用 SetLegendPosition() 放置圖例(頂部、底部、左側、右側),還可以選擇性地指定系列名稱,以便更好地識別數據。 我需要安裝 Microsoft Excel 才能以程式化方式建立圖表嗎? 不,IronXL 可獨立工作,無需安裝 Microsoft Excel。它在內部處理所有 Excel 檔案作業和圖表建立,因此非常適合伺服器環境和無法安裝 Excel 的應用程式。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 準備好開始了嗎? Nuget 下載 1,846,091 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:1,846,091 查看許可證