private void ProtectCellContents() { if (!this.ProtectContents) { if (DialogResult.Yes == MessageBox.Show("Cell contents in this " + "worksheet are not protected. Protect cell contents?", "Example", MessageBoxButtons.YesNo)) { // Protect cell contents, but do not change any // other protection type. this.Protect(missing, this.ProtectDrawingObjects, true, this.ProtectScenarios, this.ProtectionMode, this.Protection.AllowFormattingCells, this.Protection.AllowFormattingColumns, this.Protection.AllowFormattingRows, this.Protection.AllowInsertingColumns, this.Protection.AllowInsertingRows, this.Protection.AllowInsertingHyperlinks, this.Protection.AllowDeletingColumns, this.Protection.AllowDeletingRows, this.Protection.AllowSorting, this.Protection.AllowFiltering, this.Protection.AllowUsingPivotTables); } } }
参数
- Password
-
工作表的密码,区分大小写。如果省略此参数,则无需使用密码即可取消对工作表的保护。否则,您必须指定密码才能取消对工作表的保护。如果忘记密码,则无法取消对工作表的保护。最好将密码及其对应文档名的列表保存在安全的位置。
- DrawingObjects
-
为 true 时保护形状。默认值为 false。
- Contents
-
为 true 可保护锁定单元格中的内容。默认值为 true。
- Scenarios
-
为 true 时保护方案。默认值为 true。
- UserInterfaceOnly
-
为 true 可保护用户界面,但不保护宏。如果省略此参数,则同时对宏和用户界面应用保护。
- AllowFormattingCells
-
为 true,则允许用户格式化受保护的工作表上的任何单元格。默认值为 false。
- AllowFormattingColumns
-
为 true,则允许用户格式化受保护的工作表上的任何列。默认值为 false。
- AllowFormattingRows
-
为 true,则允许用户格式化受保护的工作表上的任何行。默认值为 false。
- AllowInsertingColumns
-
为 true,则允许用户在受保护的工作表上插入列。默认值为 false。
- AllowInsertingRows
-
为 true,则允许用户在受保护的工作表上插入行。默认值为 false。
- AllowInsertingHyperlinks
-
为 true,则允许用户在工作表上插入超链接。默认值为 false。
- AllowDeletingColumns
-
为 true,则允许用户在受保护的工作表上删除列,此处要删除的列中的每个单元格都已被解除锁定。默认值为 false。
- AllowDeletingRows
-
为 true,则允许用户在受保护的工作表上删除行,此处要删除的行中的每个单元格都已被解除锁定。默认值为 false。
- AllowSorting
-
为 true,则允许用户在受保护的工作表上进行排序。排序范围中的每个单元格都必须已解除锁定或不受保护。默认值为 false。
- AllowFiltering
-
为 true,则允许用户在受保护的工作表上设置筛选器。用户可以更改筛选条件,但不能启用或禁用自动筛选。用户可以在现有的自动筛选上设置筛选器。默认值为 false。
- AllowUsingPivotTables
-
为 true,则允许用户在受保护的工作表上使用数据透视表。默认值为 false。

如果在 UserInterfaceOnly 参数设置为 true 的情况下应用此方法,然后保存该工作簿,则在重新打开该工作簿时,整个工作表(不仅仅是该界面)将被完全保护起来。若要在打开工作簿后重新启用用户界面保护,您必须在将 UserInterfaceOnly 设置为 true 的情况下再次应用此方法。
如果提供了密码,则可以对受保护的工作表进行更改。同时,另一个方法将解除对工作表的保护,进行必要的更改,然后再次将该工作表保护起来。
![]() |
---|
“未保护的”意味着该单元格可能被锁定(“设置单元格格式”对话框),但包含在“允许用户编辑范围”对话框中定义的范围中,并且用户已使用密码取消对范围的保护或已通过 NT 权限验证 |