如何在 C# 中创建和编辑 Excel 图表
IronXL.Excel 使 C# 开发人员能够使用简单的 API 调用,以编程方式创建、编辑和删除 Excel 图表。 您可以直接从数据生成柱状图、线图、饼图和其他类型的图表,而无需依赖 Excel Interop。
在 Excel 中,图表是数据的图形表示法,用于直观地显示和分析信息。 Excel 提供多种图表类型,如柱形图、折线图和饼图,每种类型都适合不同的数据和分析需求。 使用IronXL的全面Excel库,您可以以编程方式创建这些可视化内容,以增强报告和仪表盘。
快速入门:在几秒钟内创建并绘制折线图使用IronXL,您可以安装、加载工作簿、调用CreateChart、添加数据系列、设置标题和图例位置,并且Plot,只需几行代码即可完成。 本示例展示了如何使用本地 C# 方法创建图表,而无需 Interop 开销。
-
1使用 IronXL 安装 NuGet Package Manager
-
2复制并运行这段代码。
// Load workbook and create a line chart, then add a data series IChart chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10); IChartSeries series = chart.AddSeries("A2:A10", "B2:B10"); series.Title = workSheet["B1"].StringValue; // Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart"); chart.SetLegendPosition(LegendPosition.Bottom); chart.Plot();' Load workbook and create a line chart, then add a data series Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10) Dim series As IChartSeries = chart.AddSeries("A2:A10", "B2:B10") series.Title = workSheet("B1").StringValue ' Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart") chart.SetLegendPosition(LegendPosition.Bottom) chart.Plot()' Load workbook and create a line chart, then add a data series Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10) Dim series As IChartSeries = chart.AddSeries("A2:A10", "B2:B10") series.Title = workSheet("B1").StringValue ' Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart") chart.SetLegendPosition(LegendPosition.Bottom) chart.Plot() -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronXL
最小工作流程(5 个步骤)
- 下载C#库以创建和编辑图表
- 准备创建图表的数据
- 使用
CreateChart方法设置图表类型和位置 - 使用
AddSeries方法添加数据系列 - 使用
Plot方法绘制图表
开始使用 IronXL
如何在Excel中创建图表?
IronXL 支持柱状图、散点图、折线图、饼图、条形图和面积图。 要创建图表,请指定以下组件。 这种灵活性使您可以根据数据展示需求创建包含丰富可视化的Excel电子表格。
- 使用
CreateChart指定图表类型和工作表位置。 - 使用
AddSeries添加系列。 对于某些图表类型,该方法只接受一列。第一个参数是横轴值。 二是纵轴值。 - 可选地指定系列名称、图表名称和图例位置。
- 调用
Plot以渲染图表。 多次调用可创建多个图表。
让我们从chart.xlsx Excel文件中的数据创建图表。数据预览如下所示:

创建柱状图的流程是什么?
柱状图是比较不同类别数值的理想工具。 当您加载电子表格数据时,可以有效地使用柱状图来突出数据点之间的差异。 下面的示例演示了使用动物种群数据创建多序列柱状图:
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");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")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文件时特别有用:
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.Line, 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");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.Line, 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
Dim workBook As WorkBook = WorkBook.Load("chart.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 5, 5, 20, 10)
Dim xAxis As String = "A2:A7"
' Add the series
Dim 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")Imports IronXL
Imports IronXL.Drawing.Charts
Dim workBook As WorkBook = WorkBook.Load("chart.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 5, 5, 20, 10)
Dim xAxis As String = "A2:A7"
' Add the series
Dim 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")
什么时候应该使用饼图?
饼图显示整体的比例和百分比。 对于饼图,只需要一列数据,因此实施起来比较简单。 当您想要将电子表格数据转换为市场份额、预算分配或类别分布的可视化表示时,它们非常有效:
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");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")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属性并选择目标图表来检索它。 然后访问图表属性进行编辑:
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");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")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:
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");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")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数据结构集成,允许您从DataTables、Lists或任何可枚举集合中创建图表。 因此,它非常适合生成包含可视化元素的自动报告。
总结
IronXL.Excel 为在 C# 应用程序中使用 Excel 图表提供了完整的解决方案。 无论是创建新的可视化图表、修改现有图表还是删除过时的图表,该库都能提供无需 Excel Interop 的直观方法。通过将图表功能与 IronXL 的其他功能(如数据操作和格式化)相结合,您可以构建复杂的 Excel 自动化解决方案,从而增强.NET 应用程序中的数据展示和分析功能。
常见问题
使用IronXL可以创建哪些类型的Excel图表?
IronXL支持在C#应用程序中直接创建各种Excel图表,包括柱状图、折线图、饼图、条形图、散点图和面积图,无需使用Excel Interop。
如何使用IronXL创建折线图?
要使用IronXL创建折线图,首先安装库,加载您的工作簿,使用CreateChart方法将图表类型设置为折线图,使用AddSeries添加数据系列,然后调用Plot来渲染图表。
IronXL可以帮助编辑现有的Excel图表吗?
可以,IronXL提供编辑现有Excel图表的方法,使您可以轻松更新图表标题、更改图例位置和刷新数据。
可以用IronXL从Excel工作表中删除图表吗?
可以,IronXL允许您通过访问Charts属性并对所需图表调用RemoveChart来删除Excel工作表中的图表。
如何使用IronXL进一步自定义Excel图表?
除了基本的图表创建功能外,IronXL还支持高级自定义功能,如从数据库查询动态生成,以及与.NET数据结构集成以实现全面的数据可视化。
与Excel Interop相比,使用IronXL创建图表有什么优势?
IronXL在C#中创建和编辑图表无需依赖Excel Interop,提供了一种更简单高效的方法来编程处理Excel文件。
如何使用IronXL创建柱状图?
要创建柱状图,使用IronXL加载您的工作簿,使用CreateChart设置图表类型,使用AddSeries添加数据系列,然后调用Plot以完成并显示图表。
IronXL能处理实时数据以创建图表吗?
可以,IronXL可以与.NET的数据结构如DataTables和Lists集成,使其非常适合从实时数据源创建图表和生成自动化报告。
为什么我应该考虑使用IronXL的饼图?
饼图对于显示整体的比例和百分比非常有用,使用IronXL,您可以轻松实现它们,以直观地表示市场份额、预算分配或类别分布。
可以不安装Excel而使用IronXL创建Excel图表吗?
可以,IronXL独立于Excel运行,允许您在无需安装Excel的情况下,以编程方式创建和操控Excel图表。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。