[]
        
(Showing Draft Content)

Open and Save Workbook

After a workbook is created, you can open the workbook to incorporate modifications, save the changes back to the workbook and protect it with a password in order to ensure security.

This topic includes the following tasks:

Open a workbook

You can open an existing workbook by calling the open method of the Workbook class.

While opening a workbook, you can also choose from several import options listed in the below table:

Open Options

Description

Import Flags

NoFlag=0

Data=1

Formulas=2

Default

Read only the data from the worksheet

Read only the data, formula, defined names and table from the worksheet. Table is included for table formula.

setDoNotRecalculateAfterOpened


Do not recalculate when getting formula value after loading the file. The default value is false

setDoNotAutofitAfterOpened


Do not autofit the row height after loading the file. The default value is true.

Refer to the following example code in order to open a workbook.

// Opening a workbook
workbook.open("OpenWorkbook.xlsx");
        
// Opening a workbook with Import options
        
// Import only data from .xlsx document
XlsxOpenOptions options = new XlsxOpenOptions();
options.setImportFlags(EnumSet.of(ImportFlags.Data));
workbook.open("OpenWorkbookWithOptions.xlsx", options);
        
// Don't recalculate after opened.
options.setDoNotRecalculateAfterOpened(true);

//Don't autofit row height
options.setDoNotAutoFitAfterOpened(true);

!type=note

Note: While opening the workbook, you can check whether it is password protected or not by using the isEncryptedFile method of the Workbook class. If your workbook is password protected, you would need to provide a password everytime you open it.

While opening a password protected excel file (version 2013 or later) in DsExcel Java, the illegal key size exception will be thrown. This happens because Java compiler supports 128 bit key and doesn't support the 256 bit key (Excel 2013 or later versions use 256 bit key) by default. In such a scenario, users can apply the JCE unlimited strength policy files in order to resolve the issue.

Apart from .xlsx files, you can also open the below file formats by using the overloads of open method in Workbook class:

  • .xlsm

  • .xltx

  • .csv

  • .json

  • .ssjson

  • .sjs

However, an exception is thrown when unsupported file formats are opened. While opening a JSON file, the DeserializationOptions are supported as well.

Refer to the following example code to open a JSON file with and without options.

//create a new workbook
Workbook workbook = new Workbook();
        
// Import JSON without options
workbook.open("spread_js_exported.json");

// Import JSON with options
DeserializationOptions options = new DeserializationOptions();
options.setIgnoreStyle(true);
workbook.open("spread_js_exported.json", options);

Save a workbook

You can save the changes made in the existing workbook by calling the save method of the Workbook class.

Refer to the following example code to save your workbook.

// Saving an excel file (.xlsx document)
workbook.save("SaveExcel.xlsx");
        
// Saving an excel file with saveFileFormat
workbook.save("SaveExcelWithFormat.xlsx", SaveFileFormat.Xlsx);
        
// Saving an excel file while setting password
XlsxSaveOptions options = new XlsxSaveOptions();
options.setPassword("123456");
workbook.save("SaveExcelWithPassword.xlsx", options);

Sometimes workbook may contain many unused styles, unused defined names, or empty cells with styles applied on it which increases the file size. The XlsxSaveOptions class provides you with options to save .xlsx files without these unnecessary items so that you can optimize the file size. The setExcludeEmptyRegionCells method of the class, when set to true, lets you exclude the empty cells outside the used data range. Similarly, you can exclude the unused names and styles of a workbook while exporting by setting the setExcludeUnusedNames and setExcludeUnusedStyles method to true. The class also provides setIgnoreFormula method which lets you save the formula cells as value cells in the saved .xlsx file. You can even save the workbook in compact mode if you want a smaller file size after saving it by using the setIsCompactMode method.

The sample code below shows how to save your workbook using various options:

// Create a new workbook
        Workbook workbook = new Workbook();
InputStream fileStream = getResourceStream("xlsx/file needs to be optimized.xlsx");
workbook.open(fileStream);

XlsxSaveOptions options = new XlsxSaveOptions();
options.setExcludeEmptyRegionCells(true);
options.setExcludeUnusedStyles(true);
options.setExcludeUnusedNames(true);

workbook.save(outputStream, options);

Apart from saving files in .xlsx format, you can also save files in the below file formats by using the overloads of Save method in Workbook class:

  • .xlsm

  • .xltx

  • .csv

  • .html

  • .pdf

  • .json

  • .ssjson

  • .sjs

To view the feature in action, see Option to optimize file size demo.

See Also

Cut or Copy Across Sheets