How to Use C# to Create Excel Charts

The following FAQ enables you to create an Excel chart programmatically in C# using IronXL.

Programmatically Create Excel Charts in .NET


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:

PM> Install-Package IronPdf

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 a 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
VB   C#

Add code to create the Excel graph programmatically with IronXL:

private void button1_Click(object sender, EventArgs e)
{
    WorkBook wb = WorkBook.Load("Chart_Ex.xlsx");
    WorkSheet ws = wb.DefaultWorkSheet;

    var chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20);

    const string xAxis = "A2:A7";

    var series = chart.AddSeries(xAxis, "B2:B7");
    series.Title = ws["B1"].StringValue;

    series = chart.AddSeries(xAxis, "C2:C7");
    series.Title = ws["C1"].StringValue;

    series = chart.AddSeries(xAxis, "D2:D7");
    series.Title = ws["D1"].StringValue;

    chart.SetTitle("Column Chart");
    chart.SetLegendPosition(LegendPosition.Bottom);
    chart.Plot();
    wb.SaveAs("Exported_Column_Chart.xlsx");
}
private void button1_Click(object sender, EventArgs e)
{
    WorkBook wb = WorkBook.Load("Chart_Ex.xlsx");
    WorkSheet ws = wb.DefaultWorkSheet;

    var chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20);

    const string xAxis = "A2:A7";

    var series = chart.AddSeries(xAxis, "B2:B7");
    series.Title = ws["B1"].StringValue;

    series = chart.AddSeries(xAxis, "C2:C7");
    series.Title = ws["C1"].StringValue;

    series = chart.AddSeries(xAxis, "D2:D7");
    series.Title = ws["D1"].StringValue;

    chart.SetTitle("Column Chart");
    chart.SetLegendPosition(LegendPosition.Bottom);
    chart.Plot();
    wb.SaveAs("Exported_Column_Chart.xlsx");
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim wb As WorkBook = WorkBook.Load("Chart_Ex.xlsx")
	Dim ws As WorkSheet = wb.DefaultWorkSheet

	Dim chart = ws.CreateChart(ChartType.Column, 10, 15, 25, 20)

	Const xAxis As String = "A2:A7"

	Dim series = chart.AddSeries(xAxis, "B2:B7")
	series.Title = ws("B1").StringValue

	series = chart.AddSeries(xAxis, "C2:C7")
	series.Title = ws("C1").StringValue

	series = chart.AddSeries(xAxis, "D2:D7")
	series.Title = ws("D1").StringValue

	chart.SetTitle("Column Chart")
	chart.SetLegendPosition(LegendPosition.Bottom)
	chart.Plot()
	wb.SaveAs("Exported_Column_Chart.xlsx")
End Sub
VB   C#

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 gets 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