3

I got stuck for setting "ReadOnly" column using "ClosedXML" utility for export to Excel. I am able to export records into Excel, however the first "ID" column I need to keep it read only, user should not type anything. If he want to add new row in the exported excel he can, except "ID" column. Please help me to solve this functionality (ws is my worksheet).

ws.Column("ID").Style.Protection.SetLocked(true);

Even I am setting "SetLocked" it is editable and I need non-editable field.

Raidri
  • 17,258
  • 9
  • 62
  • 65
Rocky Royalson
  • 119
  • 2
  • 6

2 Answers2

3

at first you need to protect the whole sheet then unlock only the cells you want to be editable as following

ws.Protect("yourPassword"); //hint: password parameter is optional
ws.Column("ID").Style.Protection.SetLocked(false); //set to false 
khaled saleh
  • 470
  • 7
  • 18
2

You need to also switch protection on for the worksheet with

ws.Protect()

See the documentation and this question for more details.

Raidri
  • 17,258
  • 9
  • 62
  • 65