Documents for Excel, Java Edition Documentation
Features / Workbook / Open and Save Workbook
In This Topic
    Open and Save Workbook
    In This Topic

    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





    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.

    DoNotRecalculateAfterOpened Do not recalculate when getting formula value after loading the file. Default is false

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

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

    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 GcExcel 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:

    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.

    Copy Code
    //create a new workbook
    Workbook workbook = new Workbook();
    // Import JSON without options"spread_js_exported.json");
    // Import JSON with options
    DeserializationOptions options = new DeserializationOptions();
    options.setIgnoreStyle(true);"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.

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