IronXL 操作指南 创建和编辑图表 How to Create and Edit Excel Charts in C# Chaknith Bin 已更新:六月 10, 2025 Download IronXL NuGet 下载 DLL 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English In Excel, a chart is a graphical representation of data. It is a visual tool used to display and analyze data in a more understandable and meaningful way. Excel provides a variety of chart types, such as bar charts, line charts, pie charts, and more, each of which is suited for different types of data and analysis. Quickstart: Create and Plot a Line Chart in Seconds With IronXL, you can install, load a workbook, call CreateChart, add your data series, set your title & legend position, and Plot—all in just a few lines. This example shows how quickly you can go from no chart to a polished visual using native C# methods—without any Interop overhead. Get started making PDFs with NuGet now: Install IronXL with NuGet Package Manager PM > Install-Package IronXL.Excel Copy and run this code snippet. var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot(); Deploy to test on your live environment Start using IronXL in your project today with a free trial Free 30 day Trial Minimal Workflow (5 steps) Download the C# library to create and edit charts Prepare the data for creating the chart Use the CreateChart method to set the chart type and position Use the AddSeries method to add data series Plot the chart using the Plot method Get started with IronXL 今天在您的项目中使用 IronXL,免费试用。 第一步: 免费开始 Create Charts Example IronXL supports column, scatter, line, pie, bar, and area charts. To create a chart, we have to specify a few things separately. Start by using the CreateChart method to specify the type of chart and its location in the worksheet. Add the series with the AddSeries method. This method also accepts a single column of data since that is sufficient for some chart types. The first parameter represents values for the horizontal axis. The second parameter represents values for the vertical axis. Optionally, specify the series name, chart name, and legend position. Invoke the Plot method to plot the chart. The method will plot the chart using all the data that was added. Multiple calls to this method lead to plotting multiple charts instead of modifying the existing chart. Let's create some charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below: Column Chart :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 Line Chart Since a line chart can represent as much information as a column chart, switching between the two is very simple. You only need to change the chart type. :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-line-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("Line Chart"); // Set the legend position chart.SetLegendPosition(LegendPosition.Bottom); // Plot the chart chart.Plot(); workBook.SaveAs("lineChart.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("Line Chart") ' Set the legend position chart.SetLegendPosition(LegendPosition.Bottom) ' Plot the chart chart.Plot() workBook.SaveAs("lineChart.xlsx") $vbLabelText $csharpLabel Pie Chart For a pie chart, only one column of data is needed. :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 Edit Chart Example There are a few things you can edit in an existing chart. You can edit the legend position and chart title. To edit the chart, you must first retrieve the chart by accessing the Charts property and select the targeted chart for editing. From there, access the properties of the chart to make your edits. :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 Before After Remove Chart Example To remove an existing chart from a spreadsheet, first retrieve the chart from the Charts property of the worksheet object. You will receive a list of charts from the Charts property. Pass the targeted chart object to the RemoveChart method to remove it. :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 常见问题解答 如何在C#中创建Excel图表而不使用Interop? 通过使用IronXL库,您可以在C#中创建Excel图表而不使用Interop。首先从NuGet下载该库,然后使用CreateChart方法指定图表类型及其在工作表中的位置。 使用C#自定义Excel图表涉及哪些步骤? 要使用C#自定义Excel图表,首先准备您的数据,然后使用AddSeries方法添加数据系列。通过访问工作表的Charts属性,定制图表属性,如标题和图例位置。 如何在C#中切换不同的图表类型? 在C#中切换不同的图表类型很简单。只需更改CreateChart方法中的图表类型参数,即可在柱状、散点、折线、饼图、条形或面积图之间切换。 在C#中编辑Excel图表后保存更改的方法是什么? 编辑Excel图表后,使用SaveAs方法保存对工作簿的更改,确保所有修改都被保存。 我能否使用C#从Excel工作表中删除现有图表? 是的,您可以通过工作表的Charts属性检索现有图表,然后使用RemoveChart方法将其删除。 在.NET应用程序中使用IronXL进行图表创建有哪些好处? IronXL允许在.NET应用程序中高效地创建和编辑图表,而无需依赖Excel Interop,从而提升性能并降低复杂性。它支持各种图表类型,并提供简单易用的自定义方法。 如何在C#中添加数据系列后绘制图表? 使用AddSeries方法添加数据系列后,您可以通过调用Plot方法来绘制图表,该方法使用提供的数据生成图表。 创建图表后,能否在C#中编辑其属性? 是的,您可以通过工作表的Charts属性访问图表并修改如图表标题、图例位置等属性来编辑图表的属性。 Chaknith Bin 立即与工程团队聊天 软件工程师 Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。 准备开始了吗? Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,686,155 查看许可证