Back to Devexpress

Worksheet.Protect(String, WorksheetProtectionPermissions) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-protect-x28-system-dot-string-devexpress-dot-spreadsheet-dot-worksheetprotectionpermissions-x29.md

latest7.7 KB
Original Source

Worksheet.Protect(String, WorksheetProtectionPermissions) Method

Protects worksheet elements to prevent them from being changed by the user.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void Protect(
    string password,
    WorksheetProtectionPermissions permissions
)
vb
Sub Protect(
    password As String,
    permissions As WorksheetProtectionPermissions
)

Parameters

NameTypeDescription
passwordString

A string that specifies a password for the worksheet. If the password is an empty string, the worksheet can be unprotected without using a password.

| | permissions | WorksheetProtectionPermissions |

A WorksheetProtectionPermissions enumeration that allows you to combine enumeration values to specify multiple worksheet elements to protect.

|

Remarks

If the worksheet is already protected, the Protect method throws an exception. Check the Worksheet.IsProtected property before calling the method.

View Example

csharp
Worksheet worksheet = workbook.Worksheets["ProtectionSample"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Protect the worksheet. Prevent end-users from changing worksheet elements.
if (!worksheet.IsProtected)
    worksheet.Protect("password", WorksheetProtectionPermissions.Default);
// Add a note.
worksheet["B2"].Value = "Worksheet is protected with a password. \n You cannot edit or format cells until protection is removed." +
                        "\nTo remove protection, on the Review tab, in the Changes group," +
                        "\nclick \"Unprotect Sheet\" and enter \"password\".";
worksheet.Visible = true;
vb
Dim worksheet As Worksheet = workbook.Worksheets("ProtectionSample")
workbook.Worksheets.ActiveWorksheet = worksheet

' Protect the worksheet. Prevent end-users from changing worksheet elements.
If Not worksheet.IsProtected Then
    worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If
' Add a note.
worksheet("B2").Value = "Worksheet is protected with a password. " & ControlChars.Lf & " You cannot edit or format cells until protection is removed." & ControlChars.Lf & "To remove protection, on the Review tab, in the Changes group," & ControlChars.Lf & "click ""Unprotect Sheet"" and enter ""password""."
worksheet.Visible = True

You can selectively unlock ranges in a protected worksheet. To do this, add a ProtectedRange to the Worksheet.ProtectedRanges collection. You can specify a password to edit the protected range with the ProtectedRange.SetPassword method and give specific users permission to edit the range with the ProtectedRange.CreateSecurityDescriptor method.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Protect(String, WorksheetProtectionPermissions) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_API_Part02/SpreadsheetActions/ProtectionActions.cs#L39

csharp
if (!worksheet.IsProtected)
    worksheet.Protect("password", WorksheetProtectionPermissions.Default);
workbook.BeginUpdate();

wpf-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/ProtectionActions.cs#L37

csharp
if (!worksheet.IsProtected)
    worksheet.Protect("password", WorksheetProtectionPermissions.Default);
workbook.BeginUpdate();

spreadsheet-document-api-examples-part2/CS/SpreadsheetDocServerAPIPart2/CodeExamples/ProtectionActions.cs#L52

csharp
if (!worksheet.IsProtected)
    worksheet.Protect("password", WorksheetProtectionPermissions.Default);
// Add a note.

winforms-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_API_Part02/SpreadsheetActions/ProtectionActions.vb#L43

vb
If Not worksheet.IsProtected Then
    worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If

wpf-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/ProtectionActions.vb#L41

vb
If Not worksheet.IsProtected Then
    worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If

spreadsheet-document-api-examples-part2/VB/SpreadsheetDocServerAPIPart2/CodeExamples/ProtectionActions.vb#L49

vb
If Not worksheet.IsProtected Then
    worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If

See Also

Protection in Spreadsheet Documents

ProtectedRange

EditRangePermission

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace