How to Create and Edit Excel Charts in C
IronXL.Excel 使 C# 开发人员能够使用简单的 API 调用,以编程方式创建、编辑和删除 Excel 图表。 您可以直接从数据生成柱状图、线图、饼图和其他类型的图表,而无需依赖 Excel Interop。
在 Excel 中,图表是数据的图形表示法,用于直观地显示和分析信息。 Excel 提供多种图表类型,如柱形图、折线图和饼图,每种类型都适合不同的数据和分析需求。 在使用 IronXL.Excel综合库时,您可以通过编程创建这些可视化内容,以增强您的报告和仪表板。
快速入门:在几秒钟内创建并绘制折线图
using IronXL,您可以安装、加载工作簿,调用 CreateChart,添加您的数据系列,设置您的标题和图例位置,并且 Plot—仅需几行代码。 本示例展示了如何使用本地 C# 方法创建图表,而无需 Interop 开销。
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/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
最小工作流程(5 个步骤)
- 下载用于创建和编辑图表的 C# 库。
- 准备创建图表的数据
- 使用
CreateChart方法设置图表类型和位置 - 使用
AddSeries方法添加数据系列 - 使用
Plot方法绘制图表
开始使用 IronXL
如何在 Excel 中创建图表?
IronXL 支持柱状图、散点图、折线图、饼图、条形图和面积图。 要创建图表,请指定以下组件。 这种灵活性使您可以 创建 Excel 电子表格,并根据您的数据展示需求量身定制丰富的可视化内容。
- 使用
CreateChart指定图表类型和工作表位置。 - 使用
AddSeries添加系列。 对于某些图表类型,该方法只接受一列。第一个参数是横轴值。 二是纵轴值。 3.可选择指定系列名称、图表名称和图例位置。 - 调用
Plot渲染图表。 多次调用可创建多个图表。
让我们根据 chart.xlsx Excel 文件中的数据创建图表。数据预览如下:
创建柱状图的过程是怎样的?
柱状图是比较不同类别数值的理想工具。 当您加载电子表格数据时,您可以使用列图有效地将其可视化,以突出显示数据点之间的差异。 下面的示例演示了使用动物种群数据创建多序列柱状图:
: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")
如何创建折线图?
折线图擅长显示随时间变化的趋势。由于折线图显示的信息与柱状图相同,因此在两者之间切换只需更改图表类型。 这使得折线图在读取包含时间序列数据的 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")
何时应该使用饼图?
饼图显示整体的比例和百分比。 对于饼图,只需要一列数据,因此实施起来比较简单。 当您想将电子表格数据转换为市场份额、预算分配或类别分布的可视化表示时,它们就会很有效:
: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")
如何编辑现有图表?
在使用现有 Excel 文件时,您可能需要修改已创建的图表。 IronXL 提供了编辑现有图表的直接方法,允许您更新标题、重新定位图例和刷新数据。 这在编辑包含已有可视化内容的 Excel 文件时非常有用。
您可以编辑现有图表中的图例位置和图表标题。 要编辑图表,首先通过访问 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")
前
后
如何从 Excel 中删除图表?
有时您需要清理 Excel 文件,删除过时或不必要的图表。 这在管理包含多个可视化的工作表时很常见。 要从电子表格中删除现有图表,首先从 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")
高级图表定制
除了基本的图表创建,IronXL 还支持高级定制选项。 在创建复杂的报告或仪表板时,您可以将图表与其他 Excel 功能(如条件格式化)结合起来,创建全面的数据可视化。
对于商业应用程序,图表通常需要从数据库查询或实时数据源动态生成。 IronXL 与.NET数据结构无缝集成,允许您从 Lists 或任何可枚举集合中创建图表。 因此,它非常适合生成包含可视化元素的自动报告。
摘要
IronXL.Excel 为在 C# 应用程序中使用 Excel 图表提供了完整的解决方案。 无论是创建新的可视化图表、修改现有图表还是删除过时的图表,该库都能提供无需 Excel Interop 的直观方法。通过将图表功能与 IronXL 的其他功能(如数据操作和格式化)相结合,您可以构建复杂的 Excel 自动化解决方案,从而增强.NET 应用程序中的数据展示和分析功能。
常见问题解答
如何在不使用 Interop 的情况下用 C# 编程创建 Excel 图表?
IronXL.Excel 提供了一个简单的 API,可在 C# 中创建 Excel 图表,而无需依赖 Interop。您可以使用 CreateChart 方法指定图表类型和位置,使用 AddSeries 方法添加数据,使用 Plot 方法渲染图表--所有这些都可以通过本地 C# 代码实现。
using C# 可以在 Excel 电子表格中创建哪些类型的图表?
IronXL 支持创建各种图表类型,包括柱状图、散点图、线图、饼图、条形图和面积图。您可以在调用 CreateChart 方法时指定图表类型,并使用标题、图例和数据序列自定义每个图表。
如何以编程方式在 Excel 图表中添加数据序列?
using IronXL 的 AddSeries 方法为图表添加数据。该方法接受单元格范围作为参数--第一个参数用于水平轴值,第二个参数用于垂直轴值。您可以添加多个序列来创建多序列图表。
using C# 在 Excel 中创建折线图的最快方法是什么?
using IronXL,您只需几行代码就能创建一个折线图:使用 CreateChart(ChartType.Line) 来初始化图表,使用 AddSeries() 来添加数据范围,使用 SetTitle() 来创建图表标题,使用 Plot() 来在工作表上渲染图表。
能否自定义标题和图例位置等图表属性?
是的,IronXL.Excel 允许完全自定义 Excel 图表。您可以使用 SetTitle() 添加图表标题,使用 SetLegendPosition() 放置图例(顶部、底部、左侧、右侧),还可以选择指定系列名称,以便更好地识别数据。
我需要安装 Microsoft Excel 才能以编程方式创建图表吗?
不,IronXL.Excel 可独立运行,无需安装 Microsoft Excel。它可以在内部处理所有 Excel 文件操作和图表创建,因此非常适合无法安装 Excel 的服务器环境和应用程序。

