Protection
- 5 minutes to read
The Spreadsheet control enables you to use password encryption to prevent unauthorized people from opening or modifying your workbooks. It also offers a workbook protection functionality, to restrict changes to workbooks, worksheets, and specific ranges. In a protected workbook, worksheets cannot be added, deleted or renamed by users. Also, users are prohibited from moving worksheets within the workbook. A protected worksheet does not allow a user to change cell values or modify the worksheet in other ways. Also, different sheets can have different levels of protection. For example, a range in a protected worksheet can be unlocked for specific users (the Windows domain security model is used), but other users may be prompted for a password when they try to edit the range.
File Password Encryption
The Spreadsheet control can open and save password encrypted files in binary (.xls) and OpenXml (.xlsx) formats.
Open the Encrypted File
The table below lists ways to specify a password to open the encrypted file.
Method | Description |
---|---|
The WorkbookImportOptions.Password setting. | If this property contains the correct password, a document is loaded. If the password is not specified or is invalid, the SpreadsheetControl.EncryptedFilePasswordRequest event occurs. |
The SpreadsheetControl.EncryptedFilePasswordRequest event. | Subscribe to this event and specify a password using the EncryptedFilePasswordRequestEventArgs.Password property. Set the e.Handled property to true to not display a dialog that prompts the end-user for a password. |
A dialog prompts the end user for a password. |
If neither method provides a correct password, the EncryptedFilePasswordCheckFailed event occurs. Handle this event to obtain the error that led to this event (Error) and determine whether to prompt a user for a password (TryAgain).
When the TryAgain property is set to false, a warning message is displayed and the Spreadsheet control creates an empty document. The message contains localized text specified by the XtraSpreadsheetStringId.Msg_IncorrectPassword enumeration member.
Important
If an encrypted file is loaded using the SpreadsheetControl.DocumentSource property, its format cannot be detected. Use the SpreadsheetDocumentSource instance to bind encrypted files.
Save the Encrypted File
You can specify a password to save the encrypted file in the following ways.
Method | Description |
---|---|
The DocumentSettings.Encryption property. | Use the EncryptionOptions.Type property to set the desired encryption type and the EncryptionOptions.Password property to specify a password to encrypt a document. |
The Encrypt Document dialog allows end-users to specify a password to encrypt the contents of the document when it is saved to a file. To provide end-users with the capability to invoke the Encrypt Document dialog, add the Info Ribbon page group to the SpreadsheetControl. |
Workbook Protection
You can protect a workbook to lock its structure or freeze/unfreeze windows. For this purpose, use the IWorkbook.Protect method. You can provide an optional password parameter to prompt the end-user for a password to unprotect a worksheet. To unprotect a worksheet, use the IWorkbook.Unprotect method.
An end-user can protect a worksheet by using the following dialog:
Worksheet Protection
You can protect a worksheet to lock the cells so that end-users can only perform a specific (restricted) set of actions.
By default, all cells in the worksheet has the Protection.Locked attribute set to true. When protection is applied to the worksheet, these cells becomes read-only. The SpreadsheetControl.ProtectionWarning event occurs on an attempt to edit a locked cell.
Cells with the Protection.Locked attribute set to false are not protected when worksheet protection is applied, so that the end-users can edit their contents.
To apply worksheet protection, use the Worksheet.Protect method. You can provide an optional password parameter to prompt the end-user for a password to unprotect a worksheet. To unprotect a worksheet, use the Worksheet.Unprotect method.
By default (when you pass the WorksheetProtectionPermissions.Default parameter to the Protect method), a protected worksheet is locked so that the user is only able to select cells.
An end-user can protect a worksheet by using the following dialog:
Unlock Ranges in a Protected Worksheet
Cells with the Protection.Locked attribute set to false are editable when worksheet protection is applied. However, you might wish to protect the range of cells so that it is editable only for authenticated users. Here is a brief guide on how to accomplish this.
The Worksheet.ProtectedRanges collection contains ranges that can be unlocked for users providing the required credentials. A user can be authenticated in one of the following ways.
Authentication that uses the user account under which the spreadsheet application runs. The authentication itself is performed by Windows and the control uses standard security mechanisms that rely on domain security. The text that follows provides short instructions on how to associate a particular user with a particular range in a worksheet.
To unlock a range for a specific user or user group, perform the following steps. Create an EditRangePermission object for each user or group. Transform a list of permissions to a security descriptor by using the ProtectedRange.CreateSecurityDescriptor method. Specify the security descriptor for the range by using the ProtectedRange.SecurityDescriptor method.
- Authentication that uses a password. To specify a password for the range, use the ProtectedRange.SetPassword method. When an attempt is made to edit the range, the user will be prompted for a password.
Note that these permissions are in effect on a protected worksheet (use the Worksheet.Protect method).
An end-user can specify unlocked ranges in the following dialog: