IronXL 操作指南 创建和编辑图表 如何在 C# 中创建和编辑 Excel 图表 Chaknith Bin 已更新:六月 10, 2025 下载 IronXL NuGet 下载 DLL 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English 在Excel中,图表是数据的图形化表示。 它是一种可视化工具,用于以更易于理解和更有意义的方式显示和分析数据。 Excel 提供了多种图表类型,例如条形图、折线图、饼图等等,每种图表都适用于不同类型的数据和分析。 快速入门:几秒钟内创建并绘制折线图 使用 IronXL,您只需几行代码即可完成安装、加载工作簿、调用 CreateChart、添加数据系列、设置标题和图例位置以及绘制图表等操作。 这个例子展示了如何使用原生 C# 方法快速地从没有图表到制作精美的可视化图表——而无需任何互操作开销。 立即开始使用 NuGet 创建 PDF 文件: 使用 NuGet 包管理器安装 IronXL PM > Install-Package IronXL.Excel 复制并运行这段代码。 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(); 部署到您的生产环境中进行测试 立即开始在您的项目中使用 IronXL,免费试用! 免费试用30天 最小工作流程(5 个步骤) 下载用于创建和编辑图表的 C# 库。 准备创建图表的数据 使用CreateChart方法设置图表类型和位置 使用AddSeries方法添加数据系列 使用Plot方法绘制图表 开始使用 IronXL 今天在您的项目中使用 IronXL,免费试用。 第一步: 免费开始 创建图表示例 IronXL 支持柱状图、散点图、折线图、饼图、条形图和面积图。 要创建图表,我们需要分别指定一些内容。 首先使用CreateChart方法指定图表类型及其在工作表中的位置。 使用AddSeries方法添加序列。 此方法也接受单列数据,因为对于某些图表类型来说,单列数据就足够了。第一个参数表示水平轴的值。 第二个参数表示纵轴的值。 (可选)指定系列名称、图表名称和图例位置。 调用Plot方法绘制图表。 该方法将使用所有已添加的数据绘制图表。 多次调用此方法会导致绘制多个图表,而不是修改现有图表。 Let's create some charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below: 柱状图 :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 折线图 由于折线图和柱状图都能表示大量的信息,因此在两者之间切换非常简单。 您只需要更改图表类型即可。 :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 饼图 饼图只需要一列数据。 :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 编辑图表示例 您可以对现有图表进行一些编辑。 您可以编辑图例位置和图表标题。 要编辑图表,必须先通过访问"图表"属性检索图表,然后选择要编辑的目标图表。 从那里,访问图表的属性进行编辑。 :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 前 后 移除图表示例 要从电子表格中删除现有图表,首先从工作表对象的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 常见问题解答 如何在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,738,553 | Version: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,738,553 查看许可证