GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Excel Chart Preservation

Charts which are not supported by GemBox.Spreadsheet (for example, 3D charts) are preserved so they will still be contained in output XLSX file.

Preserved charts can reflect changes made to data when opened in MS Excel application.

One way to accomplish this is to use named ranges as source of data for chart. Chart will reflect changes made to named ranges when opened in MS Excel application as following example shows.

This technique is also known as dynamic chart data and, in order to work, template file which contains chart must also have defined named ranges which are used by the chart and changed in code.

For more information about chart preservation, see charts preservation section from help documentation.

Screenshot
Preservation Screenshot

See the full code below, use Run Example to execute.

Upload your file(Drag files here)

Download a sample file

using System;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Charts;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        ExcelFile ef = ExcelFile.Load("ChartTemplate.xlsx");

        int numberOfEmployees = 4;

        var ws = ef.Worksheets[0];

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

        // Add data which is used by preserved chart through named ranges 'Names' and 'Salaries'.
        var names = new string[] { "John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat" };
        var random = new Random();
        for (int i = 0; i < numberOfEmployees; i++)
        {
            ws.Cells[i + 1, 0].Value = names[i % names.Length] + (i < names.Length ? string.Empty : ' ' + (i / names.Length + 1).ToString());
            ws.Cells[i + 1, 1].SetValue(random.Next(1000, 5000));
        }

        ef.Save("Preservation.xlsx");
    }
}
Imports System
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Charts

Module Samples

    Sub Main()

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

        Dim ef As ExcelFile = ExcelFile.Load("ChartTemplate.xlsx")

        Dim numberOfEmployees As Integer = 4

        Dim ws = ef.Worksheets(0)

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

        ' Add data which is used by preserved chart through named ranges 'Names' and 'Salaries'.
        Dim names = New String() {"John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat"}
        Dim random = New Random()
        For i As Integer = 0 To numberOfEmployees - 1
            ws.Cells(i + 1, 0).Value = names(i Mod names.Length) & (If(i < names.Length, String.Empty, " "c & (i \ names.Length + 1).ToString()))
            ws.Cells(i + 1, 1).SetValue(random.Next(1000, 5000))
        Next

        ef.Save("Preservation.xlsx")

    End Sub

End Module

Check next sample.