Click or drag to resize

Handle Excel Security Features with ExpertXls Library for .NET

Using ExpertXls Excel Library for .NET you can password protect a workbook (this feature is supported only for Excel 97-2003 workbooks), protect the workbook structure and windows, protect the worksheet data.

The DocumentSecurity property of the ExcelWorkbook class allows you to set an open password and a write password for the workbook. The DocumentSecurity.OpenWorkbookPassword property can be used to set the open password for the workbook required by an Excel viewer before opening the workbook and the DocumentSecurity.WriteWorkbookPassword property can be used to specify a password required by an Excel editor in order to modify the workbook.

Code Samples

Take a look at the following code sample:

//set a password required when the workbook is opened by an Excel viewer.
workbook.DocumentSecurity.OpenWorkbookPassword = "open";

//set a password required by an Excel editor in order to modify the workbook.
workbook.DocumentSecurity.WriteWorkbookPassword = "write";

The ExcelWorkbook.DocumentSecurity.ProtectWorkbook() method allows you to protect the workbook structure and windows. Optionally a password can be specified. This password is required in order to remove the protection.

The statement below shows how to protect the workbook structure and windows:

workbook.DocumentSecurity.ProtectWorkbook(true, true);

Another security feature allows you to protect the worksheet data. This feature can be accessed using the ExcelWorksheet.WorksheetSecurity property.

The code below will use the default protection flags to protect the worksheet data:

worksheet.WorksheetSecurity.ProtectWorksheet(ExcelProtectionAllowFlags.AllowDefault);