# Open and Save Workbook

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

## Content

Once you create a workbook, you can open the workbook to make modifications and save the changes back to the workbook.
This topic includes the following tasks:

* [Open a workbook](/document-solutions/dot-net-excel-api/docs/online/Features/ManageWorkbook/open-and-save-workbook#open-a-workbook)
* [Save a workbook](/document-solutions/dot-net-excel-api/docs/online/Features/ManageWorkbook/open-and-save-workbook#save-a-workbook)

## Open a workbook

You can open an existing workbook by calling the [Open](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.Workbook.Open.html) 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<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. |
| [DoNotRecalculateAfterOpened](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.XlsxOpenOptions.DoNotRecalculateAfterOpened.html) |  | Do not recalculate when getting formula value after loading the file. The default value is false |
| [DoNotAutoFitAfterOpened](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.XlsxOpenOptions.DoNotAutoFitAfterOpened.html) |  | Do not autofit the row height after loading the file. The default value is true. |

Refer to the following example code to open a workbook.

```csharp
// Opening a workbook
 workbook.Open(@"Source.xlsx", OpenFileFormat.Xlsx);

//Opening a workbook with Import options

//Import only data from .xlsx document.

XlsxOpenOptions options = new XlsxOpenOptions();
options.ImportFlags = ImportFlags.Data;
workbook.Open(@"DemoOpen.xlsx", options);

//Don't recalculate after opened.
XlsxOpenOptions options1 = new XlsxOpenOptions();
options1.DoNotRecalculateAfterOpened = true;
            
//Don’t autofit row height 
options1.DoNotAutoFitAfterOpened = true;             
workbook.Open(@"DemoOpen.xlsx", options1);
```

> !type=note
> **Note:** While opening the workbook, you can check whether it is password protected or not by using the [IsEncryptedFile](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.Workbook.IsEncryptedFile.html) method of the Workbook class. If your workbook is password protected, you would need to provide a password everytime you open it.

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.

```csharp
//create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();
        
// Import JSON without options
workbook.Open("file.json");
        
// Import JSON with options
var options = new DeserializationOptions { IgnoreStyle = true };
workbook.Open("file.json", options);
```

## Save a workbook

You can save the changes made in the existing workbook by calling the [Save](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.Workbook.Save.html) method of the Workbook class.
Refer to the following example code to save your workbook.

```csharp
// Save the Excel file
workbook.Save(@"createWorkbook.xlsx", SaveFileFormat.Xlsx);
```

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/dot-net-excel-api/api/online/DS.Documents.Excel/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 [ExcludeEmptyRegionCells](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.XlsxSaveOptions.ExcludeEmptyRegionCells.html) property 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 [ExcludeUnusedNames](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.XlsxSaveOptions.ExcludeUnusedNames.html) and [ExcludeUnusedStyles](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.XlsxSaveOptions.ExcludeUnusedStyles.html) properties to **true**. The class also provides [IgnoreFormulas](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.XlsxSaveOptions.IgnoreFormulas.html) property 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 [IsCompactMode](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.SaveOptions.IsCompactMode.html) property.
The sample code below shows how to save your workbook using various options:

```csharp
Workbook workbook = new Workbook();       

//Add names and style for current workbook 
for (int i = 0; i < 10000; i++) 
{ 
  workbook.Names.Add($"name{i + 1}", $"=$A${i + 1}"); 
  workbook.Styles.Add($"style{i + 1}"); 
}  

//Exclude the unused styles, names and empty cell region 
XlsxSaveOptions option = new XlsxSaveOptions(); 
option.ExcludeUnusedStyles = true; 
option.ExcludeUnusedNames = true; 
option.ExcludeEmptyRegionCells = true; 

//save file with and without options 
workbook.Save("test_optimized.xlsx", option); 
workbook.Save("test_no_optimized.xlsx");
```

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 code in action, see [Option to optimize file size](https://developer.mescius.com/documents-api-excel/demos/optionstooptimizefilesize) demo.

## See Also

[Cut or Copy Across Sheets](/document-solutions/dot-net-excel-api/docs/online/Features/ManageWorkbook/CutOrCopyAcrossSheets)
[Import and Export .xlsx Document](/document-solutions/dot-net-excel-api/docs/online/ManageFileOperations/ImportAndExportxlsxDocument)

<br>
