How to Export Charts to Excel files from your .NET applications?

Following example shows how to export preserved chart to Excel file using GemBox.Spreadsheet component.

GemBox.Spreadsheet is a C# / VB.NET component which provides simple and efficient way to read, write, convert and print native Microsoft Excel (XLSX, XLS) and other spreadsheet (ODS, CSV) formats and various document formats (HTML, PDF, XPS) from .NET applications without the need for Microsoft Excel on either the developer or client machines.
GemBox.Spreadsheet Free is free of charge while GemBox.Spreadsheet Professional is a commercial version licensed per developer.
For more information, see GemBox.Spreadsheet features and why GemBox.Spreadsheet outperforms Excel automation.

Template workbook (with worksheet that contains preserved chart, named ranges used by the chart and no data) is used. Example reads template workbook, updates named ranges, fills worksheet with data and writes the new file.

C# code:

// Load template workbook that contains chart.
var workbook = ExcelFile.Load("templateChart.xlsx");

// Select the worksheet with chart.
var ws = workbook.Worksheets[0];

// Update named ranges 'Names' and 'Worktime' which are used by preserved chart.
ws.NamedRanges["Names"].Range = ws.Cells.GetSubrangeAbsolute(0, 0, 2, 0);
ws.NamedRanges["Worktime"].Range = ws.Cells.GetSubrangeAbsolute(0, 1, 2, 1);

// Add data which is used by preserved chart
// through named ranges 'Names' and 'Worktime'.
ws.Cells[0, 0].Value = "John Doe";
ws.Cells[0, 1].Value = 8;
ws.Cells[1, 0].Value = "Hans Meier";
ws.Cells[1, 1].Value = 9;
ws.Cells[2, 0].Value = "Jean Dupont";
ws.Cells[2, 1].Value = 12;

// Save the workbook.
workbook.Save("newChart.xlsx");

Visual Basic .NET code:

' Load template workbook that contains chart.
Dim workbook = ExcelFile.Load("templateChart.xlsx")

' Select the worksheet with chart.
Dim ws = workbook.Worksheets(0)

' Update named ranges 'Names' and 'Worktime' which are used by preserved chart.
ws.NamedRanges("Names").Range = ws.Cells.GetSubrangeAbsolute(0, 0, 2, 0)
ws.NamedRanges("Worktime").Range = ws.Cells.GetSubrangeAbsolute(0, 1, 2, 1)

' Add data which is used by preserved chart
' through named ranges 'Names' and 'Worktime'.
ws.Cells(0, 0).Value = "John Doe"
ws.Cells(0, 1).Value = 8
ws.Cells(1, 0).Value = "Hans Meier"
ws.Cells(1, 1).Value = 9
ws.Cells(2, 0).Value = "Jean Dupont"
ws.Cells(2, 1).Value = 12

' Save the workbook.
workbook.Save("newChart.xlsx")

Template file had no chart data and looked like this:

New file was created by using the code above and looks like this: