Document Solutions for Excel, Java Edition | Document Solutions
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.

    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 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.
    //Don't autofit row height

    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:

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

    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 do not require extended features of the workbook after saving it by using the setIsCompactMode property.

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

    Copy Code
            // Create a new workbook
            Workbook workbook = new Workbook();
    InputStream fileStream = getResourceStream("xlsx/file needs to be optimized.xlsx");;
    XlsxSaveOptions options = new XlsxSaveOptions();
    options.setExcludeUnusedNames(true);, 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:

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

    See Also