Create Excel Chart in C# and VB.NET

The following sample 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 sample.

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

Screenshot

Chart Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using GemBox.Spreadsheet;
3using GemBox.Spreadsheet.Charts;
4
5class Sample
6{
7    [STAThread]
8    static void Main(string[] args)
9    {
10        // If using Professional version, put your serial key below.
11        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
12
13        ExcelFile ef = new ExcelFile();
14        ExcelWorksheet ws = ef.Worksheets.Add("Chart");
15
16        int numberOfEmployees = 4;
17
18        // Create chart and select data for it.
19        var chart = ws.Charts.Add(ChartType.Bar, "D2", "M25");
20        chart.SelectData(ws.Cells.GetSubrangeAbsolute(0, 0, numberOfEmployees, 1), true);
21
22        // Add data which is used by the chart.
23        var names = new string[] { "John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat" };
24        var random = new Random();
25        for (int i = 0; i < numberOfEmployees; i++)
26        {
27            ws.Cells[i + 1, 0].Value = names[i % names.Length] + (i < names.Length ? string.Empty : ' ' + (i / names.Length + 1).ToString());
28            ws.Cells[i + 1, 1].SetValue(random.Next(1000, 5000));
29        }
30
31        // Set header row and formatting.
32        ws.Cells[0, 0].Value = "Name";
33        ws.Cells[0, 1].Value = "Salary";
34        ws.Cells[0, 0].Style.Font.Weight = ws.Cells[0, 1].Style.Font.Weight = ExcelFont.BoldWeight;
35        ws.Columns[0].Width = (int)LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart);
36        ws.Columns[1].Style.NumberFormat = "\"$\"#,##0";
37
38        // Make entire sheet print on a single page.
39        ws.PrintOptions.FitWorksheetWidthToPages = ws.PrintOptions.FitWorksheetHeightToPages = 1;
40
41        ef.Save("Chart.xlsx");
42    }
43}
1Imports System
2Imports GemBox.Spreadsheet
3Imports GemBox.Spreadsheet.Charts
4
5Module Samples
6
7    Sub Main()
8
9        ' If using Professional version, put your serial key below.
10        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
11
12        Dim ef As ExcelFile = New ExcelFile
13        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Chart")
14
15        Dim numberOfEmployees As Integer = 4
16
17        ' Create chart and select data for it.
18        Dim chart = ws.Charts.Add(ChartType.Bar, "D2", "M25")
19        chart.SelectData(ws.Cells.GetSubrangeAbsolute(0, 0, numberOfEmployees, 1), True)
20
21        ' Add data which is used by the chart.
22        Dim names = New String() {"John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat"}
23        Dim random = New Random()
24        For i As Integer = 0 To numberOfEmployees - 1
25            ws.Cells(i + 1, 0).Value = names(i Mod names.Length) & (If(i < names.Length, String.Empty, " "c & (i \ names.Length + 1).ToString()))
26            ws.Cells(i + 1, 1).SetValue(random.Next(1000, 5000))
27        Next
28
29        ' Set header row and formatting.
30        ws.Cells(0, 0).Value = "Name"
31        ws.Cells(0, 1).Value = "Salary"
32        ws.Cells(0, 0).Style.Font.Weight = ExcelFont.BoldWeight
33        ws.Cells(0, 1).Style.Font.Weight = ExcelFont.BoldWeight
34        ws.Columns(0).Width = CInt(LengthUnitConverter.Convert(3, LengthUnit.Centimeter, LengthUnit.ZeroCharacterWidth256thPart))
35        ws.Columns(1).Style.NumberFormat = """$""#,##0"
36
37        ' Make entire sheet print on a single page.
38        ws.PrintOptions.FitWorksheetWidthToPages = 1
39        ws.PrintOptions.FitWorksheetHeightToPages = 1
40
41        ef.Save("Chart.xlsx")
42
43    End Sub
44
45End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.