# Open and Save Workbook

Learn how to open and save a workbook using DsExcel through sample codes.

## Content

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](/document-solutions/java-excel-api/docs/online/Features/ManageWorkbook/OpenSaveProtectWorkbook#open-a-workbook)
* [Save a workbook](/document-solutions/java-excel-api/docs/online/Features/ManageWorkbook/OpenSaveProtectWorkbook#save-a-workbook)

## Open a workbook

You can open an existing workbook by calling the [open](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/Workbook.html#open) method of the **Workbook** class.
While opening a workbook, you can also choose from several import options listed in the below table:

| @cols=2:**Open Options** | **Description** |
| :----------- | :----------- | ----------- |
| Import Flags | NoFlag=0<br>Data=1<br>Formulas=2 | Default<br>Read only the data from the worksheet<br>Read only the data, formula, defined names and table from the worksheet. Table is included for table formula. |
| [setDoNotRecalculateAfterOpened](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxOpenOptions.html#setDoNotRecalculateAfterOpened) |  | Do not recalculate when getting formula value after loading the file. The default value is false |
| [setDoNotAutofitAfterOpened](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxOpenOptions.html#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.

```Java
// 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](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/Workbook.html#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](https://www.oracle.com/java/technologies/javase/jceunlimitedstrghpolicyfilereadme.html) 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.

```Java
//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](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/Workbook.html#save) method of the **Workbook** class.
Refer to the following example code to save your workbook.

```Java
// 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](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxSaveOptions.html) class provides you with options to save .xlsx files without these unnecessary items so that you can optimize the file size. The [setExcludeEmptyRegionCells](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxSaveOptions.html#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](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxSaveOptions.html#setExcludeUnusedNames) and [setExcludeUnusedStyles](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxSaveOptions.html#setExcludeUnusedStyles) method to true. The class also provides [setIgnoreFormula](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxSaveOptions.html#setIgnoreFormulas) 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](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxSaveOptions.html#setIsCompactMode) method.
The sample code below shows how to save your workbook using various options:

```Java
// 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](https://developer.mescius.com/documents-api-excel-java/demos/optionstooptimizefilesize) demo.

## See Also

[Cut or Copy Across Sheets](/document-solutions/java-excel-api/docs/online/Features/ManageWorkbook/CutOrCopyAcrossSheets)