如何使用 C# 创建 Excel 图表
通过调用CreateChart()方法,使用IronXL在C#中创建Excel图表,添加数据系列使用AddSeries(),配置标题和图例,然后使用Plot()方法绘制—只需几行代码即可完成Excel可视化。
在 IronXL 中创建柱状图:使用单一工作表方法创建图表、添加数据序列、设置标题和图例、绘制图表并保存。 要全面了解IronXL的功能,请访问我们的入门概览页面。
-
1使用 IronXL 安装 NuGet Package Manager
-
2复制并运行这段代码。
IChart chart = worksheet.CreateChart(ChartType.Column, 5, 5, 20, 10); chart.AddSeries("A2:A7", "B2:B7"); chart.SetTitle("Sales Overview").SetLegendPosition(LegendPosition.Bottom).Plot(); workbook.SaveAs("SalesChart.xlsx");Dim chart As IChart = worksheet.CreateChart(ChartType.Column, 5, 5, 20, 10) chart.AddSeries("A2:A7", "B2:B7") chart.SetTitle("Sales Overview").SetLegendPosition(LegendPosition.Bottom).Plot() workbook.SaveAs("SalesChart.xlsx")Dim chart As IChart = worksheet.CreateChart(ChartType.Column, 5, 5, 20, 10) chart.AddSeries("A2:A7", "B2:B7") chart.SetTitle("Sales Overview").SetLegendPosition(LegendPosition.Bottom).Plot() workbook.SaveAs("SalesChart.xlsx") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronXL
最小化工作流程(6步)
- 安装 Excel 库以创建 Excel 图表。
- 将现有Excel文件加载到
Workbook对象中。 - 使用
CreateChart创建图表。 - 设置图表的标题和图例。
- 调用
Plot方法。 - 将
Workbook保存到Excel文件中。
使用 .NET 以编程方式创建 Excel 图表
- 以编程方式创建 Excel 图表
- 添加系列标题和图例
步骤1
如何安装IronXL以创建Excel图表?
使用 Visual Studio 中的 NuGet 包管理器安装 IronXL:
- 选择“项目”菜单
- 管理 NuGet 程序包
- 搜索
IronXL.Excel - 安装
此安装提供了所有需要的工具,用于以编程方式创建Excel图表。 IronXL 支持各种图表类型,包括柱状图、线图、饼图、条形图、面积图和散点图。
为何使用NuGet包管理器?
您也可以在开发人员命令提示符中输入以下命令:
有哪些替代安装方法?
或者从这里下载:https://ironsoftware.com/csharp/excel/packages/IronXL.zip
安装后,IronXL允许您从头开始创建新的Excel文件或处理现有的电子表格,适合新项目和遗留系统集成。
如何使用教程
如何在.NET中以编程方式创建Excel图表?
将以下数据添加到 Excel 电子表格中:
在创建图表之前,请确保您的数据组织得当。 IronXL可以加载现有电子表格,或者您可以以编程方式创建它们。 该库支持各种 Excel 文件格式,包括 XLS、XLSX、CSV 和 TSV。
Excel图表需要哪些名称空间?
添加必要的命名空间,以便在 IronXL.Excel 中使用 Excel 图表:
using IronXL;
using IronXL.Drawing.Charts;using IronXL;
using IronXL.Drawing.Charts;Imports IronXL
Imports IronXL.Drawing.ChartsImports IronXL
Imports IronXL.Drawing.Charts这些命名空间可以访问所有与图表相关的功能。 IronXL命名空间包含核心工作簿和工作表操作,而IronXL.Drawing.Charts负责图表创建和自定义功能。
如何向我的图表添加多个数据系列?
添加代码,使用 IronXL 以编程方式创建 Excel 图表:
using IronXL;
using IronXL.Drawing.Charts;
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("Chart_Ex.xlsx");
// Use the default worksheet from the workbook
WorkSheet ws = wb.DefaultWorkSheet;
// Create a column chart at the specified range of cells
var chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20);
// Define the range for the x-axis data
const string xAxis = "A2:A7";
// Add a series for the chart using data in the range and give it a title from the first row
var series = chart.AddSeries(xAxis, "B2:B7");
series.Title = ws["B1"].StringValue;
// Add another series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = ws["C1"].StringValue;
// Add a third series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = ws["D1"].StringValue;
// Set the chart title
chart.SetTitle("Column Chart");
// Position the legend at the bottom of the chart
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart with the provided data
chart.Plot();
// Save the workbook with the newly added chart
wb.SaveAs("Exported_Column_Chart.xlsx");using IronXL;
using IronXL.Drawing.Charts;
// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("Chart_Ex.xlsx");
// Use the default worksheet from the workbook
WorkSheet ws = wb.DefaultWorkSheet;
// Create a column chart at the specified range of cells
var chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20);
// Define the range for the x-axis data
const string xAxis = "A2:A7";
// Add a series for the chart using data in the range and give it a title from the first row
var series = chart.AddSeries(xAxis, "B2:B7");
series.Title = ws["B1"].StringValue;
// Add another series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = ws["C1"].StringValue;
// Add a third series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = ws["D1"].StringValue;
// Set the chart title
chart.SetTitle("Column Chart");
// Position the legend at the bottom of the chart
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart with the provided data
chart.Plot();
// Save the workbook with the newly added chart
wb.SaveAs("Exported_Column_Chart.xlsx");Imports IronXL
Imports IronXL.Drawing.Charts
' Load the existing Excel workbook
Dim wb As WorkBook = WorkBook.Load("Chart_Ex.xlsx")
' Use the default worksheet from the workbook
Dim ws As WorkSheet = wb.DefaultWorkSheet
' Create a column chart at the specified range of cells
Dim chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20)
' Define the range for the x-axis data
Const xAxis As String = "A2:A7"
' Add a series for the chart using data in the range and give it a title from the first row
Dim series = chart.AddSeries(xAxis, "B2:B7")
series.Title = ws("B1").StringValue
' Add another series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = ws("C1").StringValue
' Add a third series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = ws("D1").StringValue
' Set the chart title
chart.SetTitle("Column Chart")
' Position the legend at the bottom of the chart
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart with the provided data
chart.Plot()
' Save the workbook with the newly added chart
wb.SaveAs("Exported_Column_Chart.xlsx")Imports IronXL
Imports IronXL.Drawing.Charts
' Load the existing Excel workbook
Dim wb As WorkBook = WorkBook.Load("Chart_Ex.xlsx")
' Use the default worksheet from the workbook
Dim ws As WorkSheet = wb.DefaultWorkSheet
' Create a column chart at the specified range of cells
Dim chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20)
' Define the range for the x-axis data
Const xAxis As String = "A2:A7"
' Add a series for the chart using data in the range and give it a title from the first row
Dim series = chart.AddSeries(xAxis, "B2:B7")
series.Title = ws("B1").StringValue
' Add another series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = ws("C1").StringValue
' Add a third series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = ws("D1").StringValue
' Set the chart title
chart.SetTitle("Column Chart")
' Position the legend at the bottom of the chart
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart with the provided data
chart.Plot()
' Save the workbook with the newly added chart
wb.SaveAs("Exported_Column_Chart.xlsx")CreateChart方法接受参数用于图表类型和位置(左列、顶行、右列、底行)。 该定位使用单元格坐标将图表精确地放置在工作表中。 您可以在一张工作表上创建多个图表,也可以将它们分散到不同的工作表中。
在处理数据系列时,IronXL.Excel 允许您使用标准 Excel 符号引用单元格区域。 这样,熟悉 Excel 的开发人员就可以直观地过渡到编程式图表创建。 此外,您还可以将图表与Excel公式结合起来创建动态可视化,当数据变化时自动更新。
如何创建不同类型的图表?
IronXL 支持柱状图以外的各种图表类型。 下面是一个创建带有自定义样式的折线图的示例:
// Create a line chart for trend analysis
var lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32);
// Add data series with smooth lines
var trendSeries = lineChart.AddSeries("A2:A7", "B2:B7");
trendSeries.Title = "Q1 Sales Trend";
// Customize chart appearance
lineChart.SetTitle("Quarterly Sales Trend Analysis");
lineChart.SetLegendPosition(LegendPosition.Right);
// You can also create a column chart with multiple series
var comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25);
comboChart.AddSeries("A2:A7", "B2:B7"); // First column series
comboChart.AddSeries("A2:A7", "C2:C7"); // Second column series
comboChart.Plot();// Create a line chart for trend analysis
var lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32);
// Add data series with smooth lines
var trendSeries = lineChart.AddSeries("A2:A7", "B2:B7");
trendSeries.Title = "Q1 Sales Trend";
// Customize chart appearance
lineChart.SetTitle("Quarterly Sales Trend Analysis");
lineChart.SetLegendPosition(LegendPosition.Right);
// You can also create a column chart with multiple series
var comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25);
comboChart.AddSeries("A2:A7", "B2:B7"); // First column series
comboChart.AddSeries("A2:A7", "C2:C7"); // Second column series
comboChart.Plot();' Create a line chart for trend analysis
Dim lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32)
' Add data series with smooth lines
Dim trendSeries = lineChart.AddSeries("A2:A7", "B2:B7")
trendSeries.Title = "Q1 Sales Trend"
' Customize chart appearance
lineChart.SetTitle("Quarterly Sales Trend Analysis")
lineChart.SetLegendPosition(LegendPosition.Right)
' You can also create a column chart with multiple series
Dim comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25)
comboChart.AddSeries("A2:A7", "B2:B7") ' First column series
comboChart.AddSeries("A2:A7", "C2:C7") ' Second column series
comboChart.Plot()' Create a line chart for trend analysis
Dim lineChart = ws.CreateChart(ChartType.Line, 10, 22, 25, 32)
' Add data series with smooth lines
Dim trendSeries = lineChart.AddSeries("A2:A7", "B2:B7")
trendSeries.Title = "Q1 Sales Trend"
' Customize chart appearance
lineChart.SetTitle("Quarterly Sales Trend Analysis")
lineChart.SetLegendPosition(LegendPosition.Right)
' You can also create a column chart with multiple series
Dim comboChart = ws.CreateChart(ChartType.Column, 27, 15, 42, 25)
comboChart.AddSeries("A2:A7", "B2:B7") ' First column series
comboChart.AddSeries("A2:A7", "C2:C7") ' Second column series
comboChart.Plot()最终的图表输出是什么样的?
创建了一个Workbook对象和一个Worksheet对象。 调用Worksheet对象的CreateChart方法来指定图表类型和图表位置。 图表的序列会添加标题和图例。

图2 - 图表输出
最终的图表可以清晰直观地展示您的数据。 IronXL.Excel 可在幕后处理所有复杂的 Excel XML 格式,让您专注于业务逻辑。 有关更多图表创建示例,请查看我们的Excel图表示例页面。
高级图表自定义
除了基本的图表创建外,IronXL 还提供广泛的自定义选项。 您可以修改颜色、添加数据标签、调整坐标轴比例并应用各种格式化选项。 库的保存与导出功能确保您的图表在不同的Excel格式和版本中正确保存。
在处理大型数据集时,请考虑在创建图表前实施分页或数据过滤。 这种方法可确保最佳性能,并防止可视化效果杂乱无章。 IronXL.Excel 的高效内存管理使您可以处理大量 Excel 文件而不会降低性能。
库快速访问
常见问题
如何通过编程方式使用C#创建Excel图表?
您可以通过在C#中使用IronXL库以编程方式创建Excel图表。首先,将您的Excel文件加载到一个Workbook对象中。然后,使用工作表的CreateChart方法指定图表类型和位置。通过AddSeries方法添加数据系列,然后在调用Plot以渲染图表之前设置图表的标题和图例。
在IronXL中,哪些方法用于向Excel图表添加数据系列?
在IronXL中,您可以使用AddSeries方法向Excel图表添加数据。此方法需要指定x轴和y轴数据的单元格范围。您还可以使用工作表中的值设置数据系列标题。
如何安装IronXL来创建Excel图表?
IronXL可以使用Visual Studio中的NuGet包管理器进行安装。只需进入项目菜单,选择管理NuGet包,搜索IronXL.Excel并安装即可。这将提供所有必要的工具,以编程方式创建Excel图表。
IronXL支持哪些类型的图表?
IronXL支持多种图表类型,包括柱状图、折线图、饼图、条形图、面积图和散点图。您可以使用IronXL提供的方法以编程方式在C#中创建和自定义这些图表。
如何使用IronXL向单个图表添加多个数据系列?
要在IronXL中向单个图表添加多个数据系列,请将AddSeries方法与CreateChart函数一起使用。为每个系列指定数据范围,并使用工作表单元格值设置单独的标题。您可以根据需要为图表添加任意数量的数据系列。
IronXL可以处理大型Excel文件而不影响性能吗?
可以,IronXL旨在有效管理内存,使其能够处理大型Excel文件而不会显著降低性能。为了获得最佳性能,请在创建图表之前考虑使用分页或数据过滤。
使用IronXL创建Excel图表需要哪些命名空间?
使用IronXL创建Excel图表时,所需的命名空间是IronXL和IronXL.Drawing.Charts。IronXL命名空间促进核心工作簿和工作表操作,而IronXL.Drawing.Charts则支持图表创建和自定义。
IronXL支持哪些Excel图表文件格式?
IronXL支持多种Excel文件格式,包括XLS、XLSX、CSV和TSV。这种灵活性使您能够以不同格式创建图表,与不同的Excel版本和用例兼容。
如何在IronXL中自定义图表外观?
IronXL为图表提供了广泛的自定义选项,包括修改颜色、添加数据标签、调整坐标轴尺度和应用不同的格式样式。这使您能够创建视觉上吸引人且信息丰富的Excel图表。
使用IronXL库创建Excel图表有什么优势?
IronXL简化了通过编程方式在C#中创建和管理Excel图表的过程。它处理复杂的Excel XML格式,支持多种图表类型,提供广泛的自定义选项,并为大型文件提供高效的内存管理,使开发者能够专注于业务逻辑。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
