Allow users to edit ranges for the protected Excel worksheet in C#

Spire.XLS supports to protect the whole excel workbook and specified worksheet with password. When we deal with the protected worksheet, sometimes we need to allow users to edit some specified ranges of the excel worksheet. This article will focus on demonstrating how to use the AddAllowEditRange method offered by Spire.XLS to set the specified range on a protected worksheet to be editable by users.

Step 1: Initialize an instance of Workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet from the workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Define the specified ranges of Excel to allow users to edit while sheet is protected.

sheet.AddAllowEditRange("AAA", sheet.Range["C2:D8"], "");

Step 4: Protect the worksheet.

sheet.Protect("AAA", SheetProtectionType.All);

Step 5: Save the document to file.

workbook.SaveToFile("AllowEditRange.xlsx", ExcelVersion.Version2010);

Effective screenshot of Allow users to edit ranges:

Allow users to edit ranges for the protected Excel worksheet in C#

Full codes:

using Spire.Xls;
namespace EditRanges
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[0];

            sheet.AddAllowEditRange("AAA", sheet.Range["C2:D8"], "");
            sheet.Protect("AAA", SheetProtectionType.All);

            workbook.SaveToFile("AllowEditRange.xlsx", ExcelVersion.Version2010);

        }
    }
}