[]
Protects the workbook structure without a password by default.
When called without arguments, structure uses the default value
true and windows uses the default value
false, so workbook window protection is not enabled.
This protection restricts workbook structure operations, such as adding, moving,
deleting, hiding, unhiding, or renaming worksheets, in supported spreadsheet
applications. It does not encrypt the workbook, provide content access control,
or prevent changes made through the object model before saving.
void Protect(bool structure = true, bool windows = false)
Sub Protect(Optional [structure] As Boolean = True, Optional windows As Boolean = False)
| Type | Name | Description |
|---|---|---|
| bool | structure |
true
to protect the workbook structure; otherwise, false. The default is true.
|
| bool | windows |
true
to protect the workbook windows; otherwise, false. The default is false. This option is available only in Excel 2007, Excel 2010, Excel for Mac 2011, and Excel 2016 for Mac.
|
worksheet.Range["A1"].Value = "Quarterly Report";
worksheet.Visible = Visibility.Hidden;
workbook.Protect();
Protects a workbook so that it cannot be modified.
This protection only restricts workbook structure and window operations in supported spreadsheet applications. It does not encrypt workbook contents and should not be used as a confidentiality or access-control mechanism. To protect sensitive data, use file encryption such as password-protected XlsxSaveOptions or other access-control measures, and avoid storing unencrypted sensitive data in hidden worksheets.
void Protect(string password, bool structure = true, bool windows = false)
Sub Protect(password As String, Optional [structure] As Boolean = True, Optional windows As Boolean = False)
| Type | Name | Description |
|---|---|---|
| string | password | Password to protect the workbook. |
| bool | structure | True to protect the structure of the workbook (To prevent other users from viewing hidden worksheets, adding, moving, deleting, or hiding worksheets, and renaming worksheets). |
| bool | windows | True to prevent users from moving, resizing, or closing the workbook window, or hide/unhide windows. This option is available only in Excel 2007, Excel 2010, Excel for Mac 2011, and Excel 2016 for Mac. |
worksheet.Range["A1"].Value = "Quarterly Report";
string password = GetPasswordFromSecureSource();
workbook.Protect(password, true, true);
workbook.Unprotect(password);