Excel worksheet protection

The following example shows how you can protect a worksheet.

Note that specifying advanced settings like sheet password or allowing some objects to be editable is supported only for XLSX file format.

Excel worksheet protected with GemBox.Spreadsheet
Screenshot of Excel worksheet protected with GemBox.Spreadsheet
using GemBox.Spreadsheet;

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

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

        worksheet.Cells[0, 2].Value = "Only cells from A1 to A10 are editable.";

        for (int i = 0; i < 10; i++)
        {
            var cell = worksheet.Cells[i, 0];
            cell.SetValue(i);
            cell.Style.Locked = false;
        }

        worksheet.Protected = true;

        // ProtectionSettings class is supported only for XLSX file format.
        worksheet.Cells[2, 2].Value = "Inserting columns is allowed (only supported for XLSX file format).";
        var protectionSettings = worksheet.ProtectionSettings;
        protectionSettings.AllowInsertingColumns = true;

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

        workbook.Save("Sheet Protection.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

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

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

        worksheet.Cells(0, 2).Value = "Only cells from A1 to A10 are editable."

        For i = 0 To 9 Step 1

            Dim cell = worksheet.Cells(i, 0)
            cell.SetValue(i)
            cell.Style.Locked = False
        Next

        worksheet.Protected = True

        ' ProtectionSettings class is supported only for XLSX file format.
        worksheet.Cells(2, 2).Value = "Inserting columns is allowed (only supported for XLSX file format)."
        Dim protectionSettings = worksheet.ProtectionSettings
        protectionSettings.AllowInsertingColumns = True

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

        workbook.Save("Sheet Protection.%OutputFileType%")
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.