How to Use C# to Create Excel Charts

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

Code Examples

Create the Excel Graph
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);
    
    var series = chart.AddSeries("A2", "B2:D2");
    chart.AddSeries("A3", "B3:D3");
    chart.AddSeries("A4", "B4:D4");
    chart.AddSeries("A5", "B5:D5");
    chart.AddSeries("A6", "B6:D6");
    chart.AddSeries("A7", "B7:D7");

    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)

	Dim series = chart.AddSeries("A2", "B2:D2")
	chart.AddSeries("A3", "B3:D3")
	chart.AddSeries("A4", "B4:D4")
	chart.AddSeries("A5", "B5:D5")
	chart.AddSeries("A6", "B6:D6")
	chart.AddSeries("A7", "B7:D7")

	chart.SetTitle("Column Chart")

	chart.SetLegendPosition(LegendPosition.Bottom)

	chart.Plot()

	wb.SaveAs("Exported_Column_Chart.xlsx")
End Sub
Jump to Article
Try IronXL free for development

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);

    var series = chart.AddSeries("A2", "B2:D2");
    chart.AddSeries("A3", "B3:D3");
    chart.AddSeries("A4", "B4:D4");
    chart.AddSeries("A5", "B5:D5");
    chart.AddSeries("A6", "B6:D6");
    chart.AddSeries("A7", "B7:D7");

    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);

    var series = chart.AddSeries("A2", "B2:D2");
    chart.AddSeries("A3", "B3:D3");
    chart.AddSeries("A4", "B4:D4");
    chart.AddSeries("A5", "B5:D5");
    chart.AddSeries("A6", "B6:D6");
    chart.AddSeries("A7", "B7:D7");

    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)

	Dim series = chart.AddSeries("A2", "B2:D2")
	chart.AddSeries("A3", "B3:D3")
	chart.AddSeries("A4", "B4:D4")
	chart.AddSeries("A5", "B5:D5")
	chart.AddSeries("A6", "B6:D6")
	chart.AddSeries("A7", "B7:D7")

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