How to Create and Edit Excel Charts in C# | IronXL

How to Use C# to Create Excel Charts

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. 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");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

as-heading:3(Minimal Workflow (6 steps)

  1. Install Excel library to create Excel charts.
  2. Load the existing Excel file into a Workbook object.
  3. Create a chart with CreateChart.
  4. Set the chart's title and legend
  5. Call the Plot method.
  6. Save the Workbook to the Excel file.

Programmatically Create Excel Charts in .NET

  • Create Excel graphs programmatically
  • Add series with title and legend
How To Work related to How to Use C# to Create Excel Charts

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:

Data to be used for charting

Figure 1 Data to be used for charting

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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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.

Chart output

Figure 2Chart 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
Documentation related to 2. Create Excel Chart for .NET

常见问题解答

如何使用 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 图表,请包括必要的命名空间:IronXLIronXL.Drawing.Charts

如何在以编程方式添加图表后保存 Excel 文件?

在以编程方式添加图表后,使用 IronXL 的 SaveAs 方法保存 Excel 文件。指定保存所需的文件路径和名称。

IronXL 可以用于修改现有 Excel 文件吗?

是的,IronXL 可以用来加载、编辑和保存现有的 Excel 文件。它允许您以编程方式修改数据、添加图表和执行其他操作。

在哪里可以找到使用 IronXL 和 Excel 图表的文档?

您可以在其网站上访问 IronXL API 参考文档,以获取有关其功能及如何在 Excel 图表中使用它们的更多信息。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布