Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed Dec 15, 2021 10:09 pm

I'm trying to lock specific cells from editing (not the entire sheet).

sheet.Range["A1"].Style.Locked = true;
sheet.Protect("123", SheetProtectionType.All);

the above doesn't work, it locks the entire sheet out and no cell can be edited. When the protection is removed, all cells are editable.
I also tried:

sheet.Protect("123", SheetProtectionType.LockedCells);

Still doesn't work.
I'm just trying to set a column (or cell) to read-only.

surfdmountain
 
Posts: 8
Joined: Wed Dec 15, 2021 9:49 pm

Thu Dec 16, 2021 12:44 am

I figured this out. Now I can define which cells are read only.
By default, all my cells were locked. You must explicitly define cells as unlocked.

sheet.Range["A1"].Style.Locked = false;

Then use this:

sheet.Protect("123", SheetProtectionType.UnLockedCells);

Setting the sheet protection type to Unlocked Cells will ALLOW unlocked cells to be edited.

Using sheet.Protect("123", SheetProtectionType.All); will make all the cells editable, locked or not.

surfdmountain
 
Posts: 8
Joined: Wed Dec 15, 2021 9:49 pm

Thu Dec 16, 2021 3:25 am

Hello,

Glad to hear that the solution has been found by yourself.
If you encounter other issues related to our products in the future, please feel free to contact us. Wish you all the best!

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.XLS

cron