Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Protect Workbooks and Worksheets

    • 8 minutes to read

    The Spreadsheet Document API allows you to protect a workbook and individual sheets from modification. Spreadsheet applications (for example, Microsoft Excel) do not allow users to modify protected elements until users remove protection.

    You can protect the following document elements:

    Element Restrictions in Spreadsheet Applications Method
    Workbook Users cannot add, delete, move, rename, hide, or unhide sheets. Users cannot move, resize, or close workbook windows. Workbook.Protect(options)
    Worksheet Users cannot modify locked cells and locked drawing objects or see hidden formulas. Worksheet permissions specify other actions that users can perform. Worksheet.Protect(options)
    Chart sheet Users cannot modify the chart and other objects on the chart sheet. ChartSheet.Protect(options)

    Protection does not encrypt the document. Users can open a protected workbook without a password. To require a password to open a workbook, encrypt the workbook.

    Note

    Protection settings apply to users in spreadsheet applications (for example, Microsoft Excel). The Spreadsheet Document API does not check most protection settings when your code modifies a protected workbook or sheet. If you delete rows, columns, or cells that contain locked cells on a protected worksheet, the API throws an InvalidOperationException.

    Protect Workbook Structure

    Call the Workbook.Protect method to protect a workbook. Pass a WorkbookProtectionOptions object to the Protect method to specify the password and protection settings:

    Property Description
    LockStructure Gets or sets whether to lock workbook structure.
    LockWindows Gets or sets whether to lock the size and position of workbook windows.
    RequirePasswordToModify Gets or sets whether users must enter the modification password the workbook.

    The Workbook.Protect method uses the password to lock workbook elements. The password is optional. If you do not specify a password, users can unlock workbook elements without a password.

    If the RequirePasswordToModify property is true, the password also protects the workbook from modification. Spreadsheet applications ask users to enter the modification password the workbook or to open the workbook in read-only mode. To specify the user name that spreadsheet applications display in the password prompt, pass the name to the WorkbookProtectionOptions(password, userName, hashAlgorithm) constructor.

    The following code snippet protects workbook structure with a password, displays the workbook protection mode in the console, and saves the workbook:

    using System;
    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    workbook.Worksheets.Add(new Worksheet("Data"));
    workbook.Protect(new WorkbookProtectionOptions("password"));
    
    Console.WriteLine($"Protection mode: {workbook.ProtectionMode}");
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Recommend Read-Only Mode

    Set the Workbook.ReadOnlyRecommended property to true to recommend that users open the workbook in read-only mode. Users can decline the recommendation and edit the workbook. The recommendation does not protect the workbook, and the Workbook.Protect method does not modify the ReadOnlyRecommended property.

    Protect a Worksheet

    Call the Worksheet.Protect method to protect a worksheet. Pass a WorksheetProtectionOptions object to the Protect method to specify the password and permissions.

    In a protected worksheet, users cannot modify locked cells. The WorksheetProtectionOptions.Permissions property specifies actions that users can perform (for example, format columns, insert rows, or sort data). Combine WorksheetProtectionPermissions flags to allow multiple actions.

    The worksheet password is optional. If you do not specify a password, users can remove worksheet protection without a password.

    The following code snippet unlocks input cells, protects the worksheet with a password, and allows users to resize columns and rows in the protected worksheet:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Cells["A1"].Value = "Quantity";
    worksheet.Cells["B1"].Value = 10;
    worksheet.Cells["A2"].Value = "Total";
    worksheet.Cells["B2"].Formula = "=B1*18";
    
    // Unlock the input cell.
    worksheet["B1"].ApplyProtection(new ProtectionFormat(locked: false, hidden: false));
    
    // Protect the worksheet.
    WorksheetProtectionOptions options = new WorksheetProtectionOptions("password") {
        Permissions = WorksheetProtectionPermissions.Default
            | WorksheetProtectionPermissions.FormatColumns
            | WorksheetProtectionPermissions.FormatRows
    };
    worksheet.Protect(options);
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Lock Cells and Hide Formulas

    Cell protection settings specify cells that users can edit and formulas that users can see in a protected worksheet:

    Property Description
    ProtectionFormat.Locked Gets whether cells are locked.
    ProtectionFormat.Hidden Gets whether cell formulas are hidden.

    To specify cell protection settings, pass a ProtectionFormat value to the CellRange.ApplyProtection(protection) method, or assign the value to the CellFormat.Protection or Style.Protection property.

    To allow users to edit cells in a protected worksheet, unlock cells or add cells to a protected range.

    The following code snippet unlocks an input cell, hides the formula in a calculated cell, and protects the worksheet:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Cells["A1"].Value = "Quantity";
    worksheet.Cells["B1"].Value = 10;
    worksheet.Cells["A2"].Value = "Total";
    worksheet.Cells["B2"].Formula = "=B1*18";
    
    // Unlock the input cell.
    worksheet["B1"].ApplyProtection(new ProtectionFormat(locked: false, hidden: false));
    
    // Hide the formula and keep the cell locked.
    worksheet.Cells["B2"].Format.Protection = new ProtectionFormat().WithHidden(true);
    
    // Protect the worksheet. Protection settings take effect in a protected worksheet.
    worksheet.Protect(new WorksheetProtectionOptions("password"));
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Lock Drawing Objects

    In a protected worksheet, the ShapeBase.Lock property specifies whether users can move, resize, or delete a drawing object (a shape, picture, chart, or shape group). The ShapeBase.LockText property specifies whether users can modify shape text.

    To allow users to modify drawing objects in a protected worksheet, add WorksheetProtectionPermissions.Objects to worksheet permissions.

    Protect a Chart Sheet

    Call the ChartSheet.Protect method to protect a chart sheet. Pass a ChartSheetProtectionOptions object to the Protect method to specify the password and chart sheet elements to lock:

    Property Description
    ChartSheetProtectionOptions.LockContent Gets or sets whether to lock the chart on the chart sheet.
    ChartSheetProtectionOptions.LockObjects Gets or sets whether to lock shapes and other graphic objects on the chart sheet.

    The chart sheet password is optional. If LockContent and LockObjects properties are set to false and you do not specify a password, the ChartSheet.Protect method removes chart sheet protection.

    The following code snippet creates a chart sheet and protects the chart and other chart sheet objects with a password:

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using var workbook = new Workbook();
    var worksheet = workbook.Worksheets[0];
    worksheet.Cells["A1"].Value = "North";
    worksheet.Cells["A2"].Value = "South";
    worksheet.Cells["B1"].Value = 420;
    worksheet.Cells["B2"].Value = 310;
    
    var chart = new Chart();
    chart.Series.Add(new PieSeries {
        Text = "Sales by Region",
        Arguments = worksheet["A1:A2"].GetExpressionReference(),
        Values = worksheet["B1:B2"].GetExpressionReference()
    });
    var chartSheet = new ChartSheet("Sales Chart", chart);
    workbook.ChartSheets.Add(chartSheet);
    
    // Protect the chart sheet.
    chartSheet.Protect(new ChartSheetProtectionOptions("password"));
    
    using var stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Password Hash Algorithm

    WorkbookProtectionOptions, WorksheetProtectionOptions, and ChartSheetProtectionOptions classes derive from the WriteProtectionOptions class. Constructors of these classes accept a password and a hash algorithm (HashAlgorithmType).

    Get Protection Settings

    Use the following properties to get workbook and sheet protection settings (for example, after you load a document):

    Property Description
    Workbook.ProtectionMode Gets workbook protection settings.
    Sheet.IsProtected Gets whether the sheet is protected.
    Worksheet.ProtectionPermissions Gets actions that users can perform in the protected worksheet.
    ChartSheet.ProtectionMode Gets chart sheet elements that the protection locks.

    Remove Protection

    Call the Workbook.RemoveProtection() method to remove workbook protection. Call the Sheet.RemoveProtection() method to remove protection from a worksheet or chart sheet. These methods do not require a password.

    Workbook protection and sheet protection are independent. The Workbook.RemoveProtection method does not remove sheet protection.

    The following code snippet removes workbook protection and protection from all sheets in a workbook:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using FileStream inputStream = File.OpenRead("Document.xlsx");
    using Workbook workbook = new Workbook(inputStream);
    
    // Remove workbook protection.
    workbook.RemoveProtection();
    
    // Remove sheet protection.
    foreach (Sheet sheet in workbook.Sheets) {
        if (sheet.IsProtected)
            sheet.RemoveProtection();
    }
    
    using FileStream outputStream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(outputStream);
    

    Next Steps