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

    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.

    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.

    Java
    Copy Code
    // 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);

    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.  

    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.

    Java
    Copy Code
    // 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);
    See Also