Excel workbook protection

With GemBox.Spreadsheet you can protect the workbook structure in your Excel file. This protection prevents users from adding, moving, deleting, hiding, unhiding, and renaming worksheets from an Excel application.

Note that this protection doesn't affect the behavior of GemBox.Spreadsheet, you can modify the structure of protected workbooks just like unprotected ones.

The workbook protection is supported only in XLSX files. To enable the protection, you need to set the ExcelFile.Protected property to true and optionally set the password using the WorkbookProtection.SetPassword method.

The following example shows how to protect a workbook programmatically using C# and VB.NET.

Excel workbook protection
Screenshot of Excel workbook protection
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Workbook Protection");

        var protectionSettings = workbook.ProtectionSettings;
        protectionSettings.ProtectStructure = true;

        worksheet.Cells[0, 0].Value = "Workbook password is 123 (only supported for XLSX file format).";
        protectionSettings.SetPassword("123");

        workbook.Save("Workbook Protection.xlsx");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook As New ExcelFile()
        Dim worksheet = workbook.Worksheets.Add("Workbook Protection")

        Dim protectionSettings = workbook.ProtectionSettings
        protectionSettings.ProtectStructure = True

        worksheet.Cells(0, 0).Value = "Workbook password is 123 (only supported for XLSX file format)."
        protectionSettings.SetPassword("123")

        workbook.Save("Workbook Protection.xlsx")
    End Sub
End Module

To unprotect the protected workbook, just set the ExcelFile.Protected property to false. Note that GemBox.Spreadsheet can unprotect a workbook without requiring its password.

To check if the protected worksheet has a password, use WorkbookProtection.HasPassword property.

To remove the password, provide null to the WorkbookProtection.SetPassword method.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy