windowsforms-17595-controls-and-libraries-spreadsheet-examples-protection-how-to-protect-a-worksheet.md
You can protect a worksheet to lock the cells so that end-users can only perform a specific (restricted) set of actions specified by the WorksheetProtectionPermissions enumeration member. To accomplish this, use the Worksheet.Protect method as illustrated in the following code snippet:
Worksheet worksheet = workbook.Worksheets[0];
// Protect a worksheet. Prevent users from making changes to worksheet elements.
if (!worksheet.IsProtected)
worksheet.Protect("password", WorksheetProtectionPermissions.Default);
workbook.BeginUpdate();
worksheet["C3:F8"].Borders.SetOutsideBorders(Color.Red, BorderLineStyle.Thin);
worksheet["D5:E6"].Merge();
worksheet["D5"].Value = "Try to change me!";
worksheet["D5"].Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
workbook.EndUpdate();
Dim worksheet As Worksheet = workbook.Worksheets(0)
' Protect a worksheet. Prevent users from making changes to worksheet elements.
If Not worksheet.IsProtected Then
worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If
workbook.BeginUpdate()
worksheet("C3:F8").Borders.SetOutsideBorders(Color.Red, BorderLineStyle.Thin)
worksheet("D5:E6").Merge()
worksheet("D5").Value = "Try to change me!"
worksheet("D5").Alignment.Vertical = SpreadsheetVerticalAlignment.Center
workbook.EndUpdate()
To remove protection, the end-user can invoke the Protect Sheet dialog. In code, use the Worksheet.Unprotect method.