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.

ExcelWorksheet Protection

Following example demonstrates how to 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.

Screenshot
Sheet Protection Screenshot

See the full code below, use Run Example to execute.

using GemBox.Spreadsheet;
using System.IO;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        ExcelFile ef = new ExcelFile();
        ExcelWorksheet ws = ef.Worksheets.Add("Sheet Protection");

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

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

        ws.Protected = true;

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

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

        ef.Save("Sheet Protection.xlsx");
    }
}
Imports GemBox.Spreadsheet
Imports System.IO

Module Samples

    Sub Main()

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

        Dim ef As ExcelFile = New ExcelFile
        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Sheet Protection")

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

        For i = 0 To 9 Step 1

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

        Next

        ws.Protected = True

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

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

        ef.Save("Sheet Protection.xlsx")

    End Sub

End Module

Check next sample.