# Protect Workbook

Use DsExcel to protect or unprotect the workbook by encrypting it by a password and protect or unprotect it from modification.

## Content

DsExcel allows you to protect the workbook in case it contains any critical and confidential information that cannot be shared with others. Additionally, you can also protect it from modification so that other users can't perform certain operations on the workbook.
To protect or unprotect a workbook, you can perform the following tasks:

* [Protect Workbook using Password](/document-solutions/java-excel-api/docs/online/Features/ManageWorkbook/protect_workbook#protect-workbook-using-password)
* [Protect Workbook from Modification](/document-solutions/java-excel-api/docs/online/Features/ManageWorkbook/protect_workbook#protect-workbook-from-modification)
* [Unprotect Workbook from Modification](/document-solutions/java-excel-api/docs/online/Features/ManageWorkbook/protect_workbook#c)

## Protect Workbook using Password

DsExcel enables users to protect a workbook by encrypting it with a password. This is important when you have a business-critical workbook containing sensitive data that cannot be shared with everyone. You can secure a workbook using the [setPassword](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxOpenOptions.html#setPassword) method of [XlsxSaveOptions](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/XlsxOpenOptions.html) class.
Refer to the following example code to make your workbook password protected.

```Java
// Saving an excel file while setting password
XlsxSaveOptions options = new XlsxSaveOptions();
options.setPassword("123456");
workbook.save("SaveExcelWithPassword.xlsx", options);
```

## Protect Workbook from Modification

A workbook can be either modified by making changes in its data or by changing its structure and window. Hence, DsExcel allows you to protect a workbook from modification in following two ways:
**Protect Workbook from Modifying Data**
In many cases, you may want to share workbook data only for reference and do not want anyone to carry out unauthorised editing, intentionally or accidentally. For instance, a workbook storing list of expenses incurred during the office set up needs to be shared with stake holders while protecting its data from any kind of tampering.
DsExcel Java provides the [getWriteProtection](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IWorkbook.html#getWriteProtection) method of the [IWorkbook](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IWorkbook.html) interface which lets you set protect options while saving the workbook. This class lets you recommend the user to open only in reading mode by using the [setReadOnlyRecommended](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/WriteProtection.html#setReadOnlyRecommended) method or you can set the "modification password" through the [setWritePassword](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/WriteProtection.html#setWritePassword) method. DsExcel automatically opens the document when user provides the correct modification password. However, the behavior can be customized using result of [validatePassword](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/WriteProtection.html#validatePassword) method. The WriteProtection class also provides [getWriteReserved](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/WriteProtection.html#getWriteReserved) and [getWriteReservedBy](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/WriteProtection.html#getWriteReservedBy) methods to fetch whether the workbook is protected and by whom.
Refer to following example code to protect the workbook data from modification.

```Java
// Create a new workbook
Workbook workbook = new Workbook();
// Open an excel file.
InputStream fileStream = getResourceStream("Medical office start-up expenses 1.xlsx");
workbook.open(fileStream);

// Specify the name of the user who reserves write permission on this workbook.
workbook.getWriteProtection().setWriteReservedBy("Eric");

// Specify this workbook is saved as read-only recommended.  
// Microsoft Excel displays a message recommending that you open the workbook as read-only.
workbook.getWriteProtection().setReadOnlyRecommended(true);

// Protects the workbook with a password so that other users cannot accidentally or intentionally edit the data.
// The write-protection only happens when you open it with an Excel application.
workbook.getWriteProtection().setWritePassword("Y6dh!et5");
    
// Save to an excel file
workbook.save("CreateWriteProtectedWorkbook.xlsx");
```

**Protect Workbook from Modifying Structure and Windows**
The [Workbook](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/Workbook.html) class provides two overloaded [protect](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/Workbook.html#protect()) methods, one of which takes password as a parameter. Both the methods have two optional parameters, **structure** and **windows**, which provide different types of modification protection when set.

* If **structure** is true, worksheets cannot be added, moved, deleted, hidden or renamed, and hidden worksheets cannot be viewed. Its default value is true.
* If **windows** is true, the workbook window cannot be moved, resized, closed or hidden or unhidden. This option is supported only in Excel 2007, Excel 2010, Excel for Mac 2011 and Excel 2016 for Mac. Its default value is false.

Refer to the following example code to protect the workbook from modification using password.

```Java
// Initialize workbook
 Workbook workbook = new Workbook();
// Fetch default worksheet
 IWorksheet worksheet = workbook.getWorksheets().get(0);
// Data
 Object data = new Object[][] { { "Name", "City", "Sex", "Weight", "Height", "Age" },
        { "Bob", "NewYork", "male", 80, 180, 56 }, { "Betty", "NewYork", "female", 72, 168, 45 },
        { "Gary", "NewYork", "male", 71, 179, 50 }, { "Hunk", "Washington", "male", 80, 171, 59 },
        { "Cherry", "Washington", "female", 58, 161, 34 }, { "Coco", "Virginia", "female", 58, 181, 45 },
        { "Lance", "Chicago", "female", 49, 160, 57 }, { "Eva", "Washington", "female", 71, 180, 81 } };
    
// Set data        
worksheet.getRange("A1:G9").setValue(data);
//Protects the workbook with password so that other users cannot view hidden worksheets, add, move, delete, hide, or rename worksheets.
workbook.protect("Ygs_87@ytr");
// Save workbook to xlsx
workbook.save("ProtectWorkbook.xlsx", SaveFileFormat.Xlsx);
```

Refer to the following example code to protect the workbook from modification without using password.

```Java
// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.getWorksheets().get(0);
// Protects the workbook so that other users cannot view hidden worksheets, add,
// move, delete, hide, or rename worksheets.
workbook.protect();
// Saving workbook to xlsx
workbook.save("1-ProtectWorkbook.xlsx", SaveFileFormat.Xlsx);
```

## Unprotect Workbook from Modification

A protected workbook can be unprotected to make modifications using the [Unprotect](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/Workbook.html#unprotect) method of the [Workbook](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/Workbook.html) class, which removes the protection from a workbook.
To unprotect a password protected workbook, the correct password needs to be passed as a parameter to the **Unprotect** method. In case, the password is omitted or an incorrect password is passed, an exception message "Invalid Password" is thrown.
Refer to the following example code to unprotect a password protected workbook.

```Java
// Initialize workbook
 Workbook workbook = new Workbook();
// Fetch default worksheet 
 IWorksheet worksheet = workbook.getWorksheets().get(0);
 // Data
 Object data = new Object[][] { { "Name", "City", "Sex", "Weight", "Height", "Age" },
        { "Bob", "NewYork", "male", 80, 180, 56 }, { "Betty", "NewYork", "female", 72, 168, 45 },
        { "Gary", "NewYork", "male", 71, 179, 50 }, { "Hunk", "Washington", "male", 80, 171, 59 },
        { "Cherry", "Washington", "female", 58, 161, 34 }, { "Coco", "Virginia", "female", 58, 181, 45 },
        { "Lance", "Chicago", "female", 49, 160, 57 }, { "Eva", "Washington", "female", 71, 180, 81 } };
  // Set data    
  worksheet.getRange("A1:G9").setValue(data);
  workbook.protect("Ygs_87@ytr");
//Removes the above protection from the workbook
  workbook.unprotect("Ygs_87@ytr");
// Save workbook to xlsx
  workbook.save("UnProtectWorkbook.xlsx", SaveFileFormat.Xlsx);
```

If a workbook is not protected with a password, the password argument is ignored by the **Unprotect** method.
Refer to the following example code to unprotect the protected workbook.

```Java
// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.getWorksheets().get(0);
workbook.protect();
// Removes the above protection from the workbook.
workbook.unprotect();
// Saving workbook to xlsx
workbook.save("2-UnprotectWorkbook.xlsx", SaveFileFormat.Xlsx);
```