DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

How to Use C# to Create Excel Charts

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

C#:

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");
}
Enter fullscreen mode Exit fullscreen mode

VB:

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
Enter fullscreen mode Exit fullscreen mode

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:

Image 1

Figure 1 - Data to be used for charting

Add the Namespaces necessary to work with Excel charts in IronXL.

C#:

using IronXL;
using IronXL.Drawing.Charts;
Enter fullscreen mode Exit fullscreen mode

VB:

Imports IronXL
Imports IronXL.Drawing.Charts
Enter fullscreen mode Exit fullscreen mode

Add code to create the Excel graph programmatically with IronXL:

C#:

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");
}
Enter fullscreen mode Exit fullscreen mode

VB:

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
Enter fullscreen mode Exit fullscreen mode

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.

Image 2

Figure 2 - Chart output


Top comments (0)