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 of Excel chart preserved with GemBox.Spreadsheet
Excel chart preserved with GemBox.Spreadsheet
Upload your file (Drag file here)
using System;
using GemBox.Spreadsheet;

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

        var workbook = ExcelFile.Load("%InputFileName%");

        int numberOfEmployees = %EmployeesCount%;

        var worksheet = workbook.Worksheets[0];

        // Update named ranges 'Names' and 'Salaries' which are used by preserved chart.
        worksheet.NamedRanges["Names"].Range = worksheet.Cells.GetSubrangeAbsolute(1, 0, numberOfEmployees, 0);
        worksheet.NamedRanges["Salaries"].Range = worksheet.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++)
        {
            worksheet.Cells[i + 1, 0].Value = names[i % names.Length] + (i < names.Length ? string.Empty : ' ' + (i / names.Length + 1).ToString());
            worksheet.Cells[i + 1, 1].SetValue(random.Next(1000, 5000));
        }

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

Module Program

    Sub Main()

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

        Dim workbook = ExcelFile.Load("%InputFileName%")

        Dim numberOfEmployees As Integer = %EmployeesCount%

        Dim worksheet = workbook.Worksheets(0)

        ' Update named ranges 'Names' and 'Salaries' which are used by preserved chart.
        worksheet.NamedRanges("Names").Range = worksheet.Cells.GetSubrangeAbsolute(1, 0, numberOfEmployees, 0)
        worksheet.NamedRanges("Salaries").Range = worksheet.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

            worksheet.Cells(i + 1, 0).Value = names(i Mod names.Length) & (If(i < names.Length, String.Empty, " "c & (i \ names.Length + 1).ToString()))
            worksheet.Cells(i + 1, 1).SetValue(random.Next(1000, 5000))
        Next

        workbook.Save("Preservation.xlsx")
    End Sub
End Module

Check next example or download examples from GitHub.