- 5 minutes to read
The Spreadsheet control enables you to use password encryption to prevent unauthorized people from opening 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.
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.
|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.|
|The Password dialog.||A dialog prompts the end-user for a password.|
If neither method provides a correct password, the SpreadsheetControl.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 loads an empty document. The message contains localized text specified by the XtraSpreadsheetStringId.Msg_IncorrectPassword enumeration member.
Save the Encrypted File
You can specify a password to save the encrypted file in the following ways.
|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.||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.|
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 workbook using the following dialog.
The end-user interface for workbook protection is described in greater detail in the Protect Workbook Dialog topic.
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 have the Protection.Locked attribute set to true. When protection is applied to the worksheet, these cells become read-only. The SpreadsheetControl.ProtectionWarning event occurs on 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.
The Select locked cells and Select unlocked cells options (the WorksheetProtectionPermissions.SelectLockedCells and the WorksheetProtectionPermissions.SelectUnlockedCells parameters) do not affect SpreadsheetControl behavior. These cells are always selectable.
An end-user can protect a worksheet using the following dialog.
The end-user interface for worksheet protection is described in greater detail in the Protect Sheet Dialog topic.
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 specific user with a particular range in a worksheet.
To unlock a range for a specific user or user group, first create an EditRangePermission object for each user or group. Then transform a list of permissions to a security descriptor by using the ProtectedRange.CreateSecurityDescriptor method. Finally, 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.
For more details on the end-user interface, see the User Permissions Dialog topic.