How to Use C# to Create Excel Charts
The following How-To enables you to create an Excel chart programmatically in C# using IronXL.
Quickstart: Programmatically Generate an Excel Chart in C#)
This example shows you how easy it is to spin up a column chart in IronXL: use a single worksheet method to create the chart, add a data series, set a title and legend, plot it, and save—get meaningful visuals in minutes, not hours.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
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");Deploy to test on your live environment
as-heading:3(Minimal Workflow (6 steps)
- Install Excel library to create Excel charts.
- Load the existing Excel file into a
Workbookobject. - Create a chart with
CreateChart. - Set the chart's title and legend
- Call the
Plotmethod. - Save the
Workbookto the Excel file.
Programmatically Create Excel Charts in .NET
- Create Excel graphs programmatically
- Add series with title and legend
Step 1
1. Install IronXL
First, the easiest way to install IronXL is to make use of the NuGet Package manager in Visual Studio:
- Select the Project menu
- Manage NuGet Packages
- Search for IronXL.Excel
- Install
You could also enter the following command into the Developer Command Prompt:
Install-Package IronXL.Excel
Or Download from here: https://ironsoftware.com/csharp/excel/packages/IronXL.zip
How to Tutorial
2. Create Excel Chart for .NET
Now for the project!
Add the following details into an Excel Spreadsheet. This is shown below:
Add the Namespaces necessary to work with Excel charts in IronXL.
using IronXL;
using IronXL.Drawing.Charts;
using IronXL;
using IronXL.Drawing.Charts;
Imports IronXL
Imports IronXL.Drawing.Charts
Add code to create the Excel graph programmatically with IronXL:
:path=/static-assets/excel/content-code-examples/how-to/csharp-create-excel-chart-programmatically-example.cs
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
A Workbook object and a Worksheet object are created. The CreateChart method of the Worksheet object gets called to specify the chart type and chart location. The chart’s series get added, with its Title and the Legend. This is shown below.
Figure 2 – Chart output
Library Quick Access
IronXL API Reference Documentation
Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.
IronXL API Reference Documentation常见问题解答
如何使用 C# 以编程方式创建 Excel 图表?
您可以通过利用 IronXL 库使用 C# 以编程方式创建 Excel 图表。首先,通过 Visual Studio 中的 NuGet 包管理器安装 IronXL,将现有的 Excel 文件加载到 Workbook 对象中,并使用 CreateChart 方法定义图表类型和位置。添加数据系列,设置标题,并将 Workbook 保存到 Excel 文件中。
创建 Excel 图表以编程方式涉及哪些步骤?
程序化创建 Excel 图表的步骤包括:安装 IronXL,将现有 Excel 文件加载到 Workbook 对象中,使用 CreateChart 方法创建图表,添加数据系列,设置图表的标题和图例,并保存工作簿。
可以使用 IronXL 创建哪些类型的图表?
IronXL 支持以编程方式创建各种图表类型,例如柱形图、条形图、折线图和饼图。
如何使用 C# 向 Excel 图表添加数据系列?
若要使用 C# 向 Excel 图表添加数据系列,请使用 IronXL 的 AddSeries 方法。指定 x 轴和 y 轴数据的范围,并可选为系列设置标题。
如何以编程方式自定义 Excel 图表的图例位置?
您可以使用 IronXL 的 SetLegendPosition 方法自定义 Excel 图表的图例位置。指定位置,例如底部、顶部、左侧或右侧。
处理 IronXL 中的 Excel 图表需要哪些命名空间?
要使用 IronXL 处理 Excel 图表,请包括必要的命名空间:IronXL 和 IronXL.Drawing.Charts。
如何在以编程方式添加图表后保存 Excel 文件?
在以编程方式添加图表后,使用 IronXL 的 SaveAs 方法保存 Excel 文件。指定保存所需的文件路径和名称。
IronXL 可以用于修改现有 Excel 文件吗?
是的,IronXL 可以用来加载、编辑和保存现有的 Excel 文件。它允许您以编程方式修改数据、添加图表和执行其他操作。
在哪里可以找到使用 IronXL 和 Excel 图表的文档?
您可以在其网站上访问 IronXL API 参考文档,以获取有关其功能及如何在 Excel 图表中使用它们的更多信息。

