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 sample 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)

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 = ExcelFile.Load("ChartTemplate.xlsx");
14
15        int numberOfEmployees = 4;
16
17        var ws = ef.Worksheets[0];
18
19        // Update named ranges 'Names' and 'Salaries' which are used by preserved chart.
20        ws.NamedRanges["Names"].Range = ws.Cells.GetSubrangeAbsolute(1, 0, numberOfEmployees, 0);
21        ws.NamedRanges["Salaries"].Range = ws.Cells.GetSubrangeAbsolute(1, 1, numberOfEmployees, 1);
22
23        // Add data which is used by preserved chart through named ranges 'Names' and 'Salaries'.
24        var names = new string[] { "John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat" };
25        var random = new Random();
26        for (int i = 0; i < numberOfEmployees; i++)
27        {
28            ws.Cells[i + 1, 0].Value = names[i % names.Length] + (i < names.Length ? string.Empty : ' ' + (i / names.Length + 1).ToString());
29            ws.Cells[i + 1, 1].SetValue(random.Next(1000, 5000));
30        }
31
32        ef.Save("Preservation.xlsx");
33    }
34}
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 = ExcelFile.Load("ChartTemplate.xlsx")
13
14        Dim numberOfEmployees As Integer = 4
15
16        Dim ws = ef.Worksheets(0)
17
18        ' Update named ranges 'Names' and 'Salaries' which are used by preserved chart.
19        ws.NamedRanges("Names").Range = ws.Cells.GetSubrangeAbsolute(1, 0, numberOfEmployees, 0)
20        ws.NamedRanges("Salaries").Range = ws.Cells.GetSubrangeAbsolute(1, 1, numberOfEmployees, 1)
21
22        ' Add data which is used by preserved chart through named ranges 'Names' and 'Salaries'.
23        Dim names = New String() {"John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat"}
24        Dim random = New Random()
25        For i As Integer = 0 To numberOfEmployees - 1
26            ws.Cells(i + 1, 0).Value = names(i Mod names.Length) & (If(i < names.Length, String.Empty, " "c & (i \ names.Length + 1).ToString()))
27            ws.Cells(i + 1, 1).SetValue(random.Next(1000, 5000))
28        Next
29
30        ef.Save("Preservation.xlsx")
31
32    End Sub
33
34End Module

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