Back to Devexpress

How to: Protect a Workbook

windowsforms-17594-controls-and-libraries-spreadsheet-examples-protection-how-to-protect-a-workbook.md

latest1.9 KB
Original Source

How to: Protect a Workbook

  • Dec 12, 2022

The following code protects workbook structure with a password (“password”), preventing end-users from adding or deleting worksheets, and from displaying hidden worksheets.

View Example

csharp
// Protect workbook structure (prevents users from adding or deleting worksheets
// or from displaying hidden worksheets).
workbook.BeginUpdate();
if (!workbook.IsProtected)
    workbook.Protect("password", true, false);
workbook.Worksheets[0].Visible = false;
Worksheet worksheet = workbook.Worksheets[1];
worksheet["D5"].Value = "You are not allowed to add or delete a worksheet.";
worksheet["D6"].Value = "Hidden worksheets cannot be displayed.";
workbook.EndUpdate();
vb
' Protect workbook structure (prevents users from adding or deleting worksheets
' or from displaying hidden worksheets).
workbook.BeginUpdate()
If Not workbook.IsProtected Then
    workbook.Protect("password", True, False)
End If
workbook.Worksheets(0).Visible = False
Dim worksheet As Worksheet = workbook.Worksheets(1)
worksheet("D5").Value = "You are not allowed to add or delete a worksheet."
worksheet("D6").Value = "Hidden worksheets cannot be displayed."
workbook.EndUpdate()

Use the IWorkbook.Unprotect method to remove protection.

When protection is applied programmatically, end-users can invoke the Protect Workbook Dialog to remove protection.

See Also

Protection in Spreadsheet Documents