Skip to main content Skip to footer

How to unlock cells of a protected sheet in C#

Background:

When protecting a worksheet/workbook, by default all cells are Locked making them unable to be modified by a user. GcExcel .NET allows you to Unlock certain cells or specified ranges so users can modify them based on your requirement.

Steps to Complete:

1. Get a specified range on a sheet

2. Set the Locked Property to False

3. Protect the sheet

Getting Started:

Step 1: Get a specified range on a sheet

var sheet = workbook.ActiveSheet;
var range = sheet.Range["A5:H10"];

Step 2: Set the Locked Property to False

Set the specified ranges Locked Property to False

var sheet = workbook.ActiveSheet;
var range = sheet.Range["A5:H10"];
range.Locked = false;

Step 3: Protect the sheet

Finally, using the Protect Method, protect the workbook so that it cannot be modified

var sheet = workbook.ActiveSheet;
var sheet = workbook.ActiveSheet; var range = sheet.Range["A5:H10"]; range.Locked = false; sheet.Protection = true;
var range = sheet.Range["A5:H10"]; range.Locked = false; sheet.Protection = true;

Notice after protecting the workbook, all of the cells expect for the unlocked range is unable to be modified.

 

Tags:

Mackenzie Albitz