How to Use C# to Create Excel Charts

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

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

How to Create Excel Chart in C#

  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.

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;

// This method is triggered by a button click event
private void button1_Click(object sender, EventArgs e)
{
    // 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");
}
$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

Frequently Asked Questions

How can I programmatically create an Excel chart using C#?

You can programmatically create an Excel chart using C# by utilizing the IronXL library. First, install IronXL via NuGet Package Manager in Visual Studio, load an existing Excel file into a Workbook object, and use the CreateChart method to define the chart type and location. Add data series, set titles, and save the Workbook to the Excel file.

What steps are involved in creating an Excel chart programmatically?

The steps to create an Excel chart programmatically include: installing IronXL, loading an existing Excel file into a Workbook object, using the CreateChart method to create the chart, adding data series, setting the chart's title and legend, and saving the workbook.

What types of charts can be created with IronXL?

IronXL supports creating various chart types such as Column, Bar, Line, and Pie charts programmatically.

How can I add a data series to an Excel chart using C#?

To add a data series to an Excel chart using C#, utilize IronXL's AddSeries method. Specify the range for the x-axis and y-axis data, and optionally set a title for the series.

How do I customize the legend position of an Excel chart programmatically?

You can customize the legend position of an Excel chart using IronXL by utilizing the SetLegendPosition method. Specify positions such as Bottom, Top, Left, or Right.

What namespaces are necessary for working with Excel charts in IronXL?

To work with Excel charts using IronXL, include the necessary namespaces: IronXL and IronXL.Drawing.Charts.

How can I save an Excel file after adding a chart programmatically?

After adding a chart programmatically, save the Excel file using IronXL's SaveAs method of the Workbook object. Specify the desired file path and name for saving.

Can IronXL be used to modify existing Excel files?

Yes, IronXL can be used to load, edit, and save existing Excel files. It allows you to modify data, add charts, and perform other operations programmatically.

Where can I find documentation for using IronXL with Excel charts?

You can access the IronXL API Reference Documentation on their website for more information about its features and how to use them with Excel charts.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.