[]
        
(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.

Refer to the following example code to open a workbook.

// Create a new workbook.        
Workbook workbook = new Workbook();

// Opening a workbook.        
workbook.open("OpenWorkbook.xlsx");

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.

In addition to .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

An exception is thrown if you try to open an unsupported file format. For information about importing JSON, SSJSON, and SJS files with serialization options, see Import and Export Excel Files.

For information about configuring import options when opening Excel files, see Excel Import 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 an Excel workbook.

// Saving an excel file (.xlsx document)
workbook.save("SaveExcel.xlsx");
        
// Saving an excel file with saveFileFormat
workbook.save("SaveExcelWithFormat.xlsx", SaveFileFormat.Xlsx);

In addition to .xlsx files, 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

For information about configuring export options when saving Excel files, see Excel Export Options.

See Also