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 the Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        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);
        }

        workbook.save("Preservation.xlsx");
    }
}

See also


Next steps

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

Download Buy