Introduction
We have discussed about different approaches of protecting worksheets using Aspose.Cells. But, what if a developer needs to unprotect a protected worksheet at runtime so that some changes can be made to the file and so on? Well, developers can do it very easily with the help of the easiest API of Aspose.Cells. Let's see below that how can we do it.
Unprotecting a Worksheet
Using MS Excel
To unprotect a worksheet, you would need to unprotect the worksheet by selecting the Tools | Protection | Unprotect Sheet menu item as shown below:
|
Figure: Selecting Unprotect Sheet menu item
|
After you select Unprotect Sheet menu item, the worksheet gets unprotected (if it is not protected by a password). But, if the worksheet is already protected by a password then a dialog will open that would prompt you to enter the password so that you may work on the worksheet as shown below:
|
Figure: Entering password to unprotect the worksheet
|
Using Aspose.Cells
A worksheet can be unprotected by calling the Unprotect method of the Worksheet class. Unprotect method can be used in two ways that are described below in more detail.
Unprotecting a Simply Protected Worksheet
A simply protected worksheet is that one, which is not protected with a password. Such worksheets can be unprotected by calling the Unprotect method without passing a parameter.
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Protecting a worksheet without a password
worksheet.Protect(ProtectionType.All);
//Unprotecting the worksheet without a password
worksheet.Unprotect();
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Protecting a worksheet without a password
worksheet.Protect(ProtectionType.All)
'Unprotecting the worksheet without a password
worksheet.Unprotect()
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheets worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.getSheet(0);
Protection protection = new Protection();
worksheet.protect(protection);
//The following 3 methods are only for Excel 2000 and earlier formats
protection.setEditingContentsAllowed(false);
protection.setEditingObjectsAllowed(false);
protection.setEditingScenariosAllowed(false);
//Unprotecting the worksheet
worksheet.unprotect();
Unprotecting a Password Protected Worksheet
A password protected worksheet is that one, which is protected with a password. Such worksheets can be unprotected by calling an overloaded version of Unprotect method that takes the password (required to unprotect the worksheet) as a parameter.
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Protecting a worksheet with a password
worksheet.Protect(ProtectionType.All, "Password",null);
//Unprotecting the worksheet with a password
worksheet.Unprotect("Password");
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Protecting a worksheet with a password
worksheet.Protect(ProtectionType.All, "Password",Nothing)
'Unprotecting the worksheet with a password
worksheet.Unprotect("Password")
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheets worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.getSheet(0);
Protection protection = new Protection();
worksheet.protect(protection);
//The following 3 methods are only for Excel 2000 and earlier formats
protection.setEditingContentsAllowed(false);
protection.setEditingObjectsAllowed(false);
protection.setEditingScenariosAllowed(false);
//Protects the first worksheet with a password
protection.setPassword("password");
//Unprotecting the worksheet with a password
worksheet.unprotect("password");