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.

Excel chart preserved with GemBox.Spreadsheet for Java
Screenshot of Excel chart preserved with GemBox.Spreadsheet for Java
Upload your file (Drag file here)
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.

        ExcelFile workbook = ExcelFile.load("%InputFileName%");

        int numberOfEmployees = %EmployeesCount%;

        ExcelWorksheet worksheet = workbook.getWorksheet(0);

        // Update named ranges 'Names' and 'Salaries' which are used by preserved chart.
        worksheet.getNamedRange("Names").setRange(worksheet.getCells().getSubrangeAbsolute(1, 0, numberOfEmployees, 0));
        worksheet.getNamedRange("Salaries").setRange(worksheet.getCells().getSubrangeAbsolute(1, 1, numberOfEmployees, 1));

        // Add data which is used by preserved chart through named ranges 'Names' and 'Salaries'.
        String[] names = new String[] { "John Doe", "Fred Nurk", "Hans Meier", "Ivan Horvat" };
        java.util.Random random = new java.util.Random();
        for (int i = 0; i < numberOfEmployees; i++) {
            worksheet.getCell(i + 1, 0).setValue(names[i % names.length] + (i < names.length ? "" : " " + (i / names.length + 1)));
            worksheet.getCell(i + 1, 1).setValue(random.nextInt(4000) + 1000);


Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.