DsExcel allows you to protect a 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:
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 Password property of XlsxSaveOptions class.
Refer to the following example code to make your workbook password protected.
C# |
Copy Code |
---|---|
// Save the Excel file and protect it using password. XlsxSaveOptions options = new XlsxSaveOptions(); options.Password = "123456"; workbook.Save(@"ProtectWorkbook.xlsx", options); |
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 .NET provides WriteProtection class of the IWorkbook 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 ReadOnlyRecommended property or you can set the "modification password" through WritePassword property. DsExcel automatically opens the document when user provides the correct modification password. However, the behavior can be customized using result of ValidatePassword property. The WriteProtection class also provides WriteReserved and WriteReservedBy property to fetch whether the workbook is protected and by whom.
Refer to following example code to protect the workbook data from modification.
C# |
Copy Code |
---|---|
//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); // Open an excel file. var fileStream = GetResourceStream("xlsx\\Medical office start-up expenses 1.xlsx"); workbook.Open(fileStream); // Specify the name of the user who reserves write permission on this workbook. workbook.WriteProtection.WriteReservedBy = "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.WriteProtection.ReadOnlyRecommended = 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.WriteProtection.WritePassword = "Y6dh!et5"; // Save to an excel file workbook.Save("createwriteprotectedworkbook.xlsx"); |
Protect Workbook from Modifying Structure and Windows
The Workbook class provides two overloaded 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.
Refer to the following example code to protect the workbook from modification using password.
C# |
Copy Code |
---|---|
// Initialize workbook Workbook workbook = new Workbook(); //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(@"ProtectWorkbookWithPassword.xlsx", SaveFileFormat.Xlsx); |
Refer to the following example code to protect the workbook from modification without using password.
C# |
Copy Code |
---|---|
// Initialize workbook Workbook workbook = new Workbook(); //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(@"ProtectWorkbook.xlsx", SaveFileFormat.Xlsx); |
A protected workbook can be unprotected to make modifications using the Unprotect method of the Workbook 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.
C# |
Copy Code |
---|---|
// Initialize workbook Workbook workbook = new Workbook(); workbook.Protect("Ygs_87@ytr"); //Removes the above protection from the workbook workbook.Unprotect("Ygs_87@ytr"); // Save workbook to xlsx workbook.Save(@"UnprotectWorkbookWithPassword.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.
C# |
Copy Code |
---|---|
// Initialize workbook Workbook workbook = new Workbook(); workbook.Protect(); //Removes the above protection from the workbook. workbook.Unprotect(); // Saving workbook to xlsx workbook.Save(@"UnprotectWorkbook.xlsx", SaveFileFormat.Xlsx); |