Create Excel Chart in C# and VB.NET

The following example shows how to create an Excel chart in C# and VB.NET and select data for it using the GemBox.Spreadsheet component.

Excel charts are supported in XLSX, HTML, PDF, XPS, and image formats. In HTML, charts are converted to images.

To convert an existing workbook with charts into another file format, use Convert example.

For more information about charts, see the charts help page.

Screenshot of Excel chart created with GemBox.Spreadsheet
Excel chart created with GemBox.Spreadsheet
using System;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Charts;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Chart");

        // Add data which will be used by the Excel chart.
        worksheet.Cells["A1"].Value = "Name";
        worksheet.Cells["A2"].Value = "John Doe";
        worksheet.Cells["A3"].Value = "Fred Nurk";
        worksheet.Cells["A4"].Value = "Hans Meier";
        worksheet.Cells["A5"].Value = "Ivan Horvat";

        worksheet.Cells["B1"].Value = "Salary";
        worksheet.Cells["B2"].Value = 3600;
        worksheet.Cells["B3"].Value = 2580;
        worksheet.Cells["B4"].Value = 3200;
        worksheet.Cells["B5"].Value = 4100;

        // Set header row and formatting.
        worksheet.Rows[0].Style.Font.Weight = ExcelFont.BoldWeight;
        worksheet.Columns[0].Width = (int)LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart);
        worksheet.Columns[1].Style.NumberFormat = "\"$\"#,##0";

        // Make entire sheet print on a single page.
        worksheet.PrintOptions.FitWorksheetWidthToPages = 1;
        worksheet.PrintOptions.FitWorksheetHeightToPages = 1;

        // Create Excel chart and select data for it.
        var chart = worksheet.Charts.Add(%ChartType%, "D2", "M25");
        chart.SelectData(worksheet.Cells.GetSubrangeAbsolute(0, 0, 4, 1), true);

        workbook.Save("Chart.%OutputFileType%");
    }
}
Imports System
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Charts

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = New ExcelFile
        Dim worksheet = workbook.Worksheets.Add("Chart")

        ' Add data which is used by the Excel chart.
        worksheet.Cells("A1").Value = "Name"
        worksheet.Cells("A2").Value = "John Doe"
        worksheet.Cells("A3").Value = "Fred Nurk"
        worksheet.Cells("A4").Value = "Hans Meier"
        worksheet.Cells("A5").Value = "Ivan Horvat"

        worksheet.Cells("B1").Value = "Salary"
        worksheet.Cells("B2").Value = 3600
        worksheet.Cells("B3").Value = 2580
        worksheet.Cells("B4").Value = 3200
        worksheet.Cells("B5").Value = 4100

        ' Set header row and formatting.
        worksheet.Rows(0).Style.Font.Weight = ExcelFont.BoldWeight
        worksheet.Columns(0).Width = CInt(LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart))
        worksheet.Columns(1).Style.NumberFormat = """$""#,##0"

        ' Make entire sheet print on a single page.
        worksheet.PrintOptions.FitWorksheetWidthToPages = 1
        worksheet.PrintOptions.FitWorksheetHeightToPages = 1

        ' Create Excel chart and select data for it.
        Dim chart = worksheet.Charts.Add(%ChartType%, "D2", "M25")
        chart.SelectData(worksheet.Cells.GetSubrangeAbsolute(0, 0, 4, 1), True)

        workbook.Save("Chart.%OutputFileType%")
    End Sub
End Module

Excel combo chart

An Excel combo (combination) chart is a special chart type that combines two or more chart types in a single chart.

The following example shows how to create an Excel combo chart in C# and VB.NET and select data for it using the GemBox.Spreadsheet component.

As all other chart types, combo chart is also supported in XLSX, PDF, XPS, and image formats.

Screenshot of Excel combo chart created with GemBox.Spreadsheet
Excel combo chart created with GemBox.Spreadsheet
using System;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Charts;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Chart");

        // Add data which will be used by the Excel chart.
        worksheet.Cells["A1"].Value = "Name";
        worksheet.Cells["A2"].Value = "John Doe";
        worksheet.Cells["A3"].Value = "Fred Nurk";
        worksheet.Cells["A4"].Value = "Hans Meier";
        worksheet.Cells["A5"].Value = "Ivan Horvat";

        worksheet.Cells["B1"].Value = "Salary";
        worksheet.Cells["B2"].Value = 4023;
        worksheet.Cells["B3"].Value = 3263;
        worksheet.Cells["B4"].Value = 2851;
        worksheet.Cells["B5"].Value = 4694;

        worksheet.Cells["C1"].Value = "Max";
        worksheet.Cells["C2"].Value = 4500;
        worksheet.Cells["C3"].Value = 4300;
        worksheet.Cells["C4"].Value = 4000;
        worksheet.Cells["C5"].Value = 4900;

        worksheet.Cells["D1"].Value = "Min";
        worksheet.Cells["D2"].Value = 3000;
        worksheet.Cells["D3"].Value = 2800;
        worksheet.Cells["D4"].Value = 2500;
        worksheet.Cells["D5"].Value = 3400;

        // Set header row and formatting.
        worksheet.Rows[0].Style.Font.Weight = ExcelFont.BoldWeight;
        worksheet.Columns[0].Width = (int)LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart);

        // Set value cells number formatting
        foreach (var cell in worksheet.Cells.GetSubrange("B2", "D5"))
            cell.Style.NumberFormat = "\"$\"#,##0";

        // Make entire sheet print on a single page.
        worksheet.PrintOptions.FitWorksheetWidthToPages = 1;
        worksheet.PrintOptions.FitWorksheetHeightToPages = 1;

        // Create Excel combo chart and set category labels reference.
        var comboChart = worksheet.Charts.Add<ComboChart>("F2", "O25");
        comboChart.CategoryLabelsReference = "Chart!A2:A5";

        // Make chart legend visible
        comboChart.Legend.IsVisible = true;
        comboChart.Legend.Position = ChartLegendPosition.Top;

        // Add column chart for displaying salary series
        var salaryChart = comboChart.Add(%SalaryChartType%);
        salaryChart.Series.Add("=Chart!B1", "Chart!B2:B5");

        // Add line chart for displaying min and max series
        var minMaxChart = comboChart.Add(%MinMaxChartType%);
        minMaxChart.Series.Add("=Chart!C1", "Chart!C2:C5");
        minMaxChart.Series.Add("=Chart!D1", "Chart!D2:D5");

        workbook.Save("Combo Chart.%OutputFileType%");
    }
}
Imports System
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Charts

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = New ExcelFile
        Dim worksheet = workbook.Worksheets.Add("Chart")

        ' Add data which is used by the Excel chart.
        worksheet.Cells("A1").Value = "Name"
        worksheet.Cells("A2").Value = "John Doe"
        worksheet.Cells("A3").Value = "Fred Nurk"
        worksheet.Cells("A4").Value = "Hans Meier"
        worksheet.Cells("A5").Value = "Ivan Horvat"

        worksheet.Cells("B1").Value = "Salary"
        worksheet.Cells("B2").Value = 4023
        worksheet.Cells("B3").Value = 3263
        worksheet.Cells("B4").Value = 2851
        worksheet.Cells("B5").Value = 4694

        worksheet.Cells("C1").Value = "Max"
        worksheet.Cells("C2").Value = 4500
        worksheet.Cells("C3").Value = 4300
        worksheet.Cells("C4").Value = 4000
        worksheet.Cells("C5").Value = 4900

        worksheet.Cells("D1").Value = "Min"
        worksheet.Cells("D2").Value = 3000
        worksheet.Cells("D3").Value = 2800
        worksheet.Cells("D4").Value = 2500
        worksheet.Cells("D5").Value = 3400

        ' Set header row And formatting.
        worksheet.Rows(0).Style.Font.Weight = ExcelFont.BoldWeight
        worksheet.Columns(0).Width = CInt(LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart))

        ' Set value cells number formatting
        For Each cell In worksheet.Cells.GetSubrange("B2", "D5")
            cell.Style.NumberFormat = """$""#,##0"
        Next

        ' Make entire sheet print on a single page.
        worksheet.PrintOptions.FitWorksheetWidthToPages = 1
        worksheet.PrintOptions.FitWorksheetHeightToPages = 1

        ' Create Excel combo chart And set category labels reference.
        Dim comboChart = worksheet.Charts.Add(Of ComboChart)("F2", "O25")
        comboChart.CategoryLabelsReference = "Chart!A2:A5"

        ' Make chart legend visible
        comboChart.Legend.IsVisible = True
        comboChart.Legend.Position = ChartLegendPosition.Top

        ' Add column chart for displaying salary series
        Dim salaryChart = comboChart.Add(%SalaryChartType%)
        salaryChart.Series.Add("=Chart!B1", "Chart!B2:B5")

        ' Add line chart for displaying min And max series
        Dim minMaxChart = comboChart.Add(%MinMaxChartType%)
        minMaxChart.Series.Add("=Chart!C1", "Chart!C2:C5")
        minMaxChart.Series.Add("=Chart!D1", "Chart!D2:D5")

        workbook.Save("Combo Chart.%OutputFileType%")
    End Sub
End Module

Check next example or download examples from GitHub.