2
votes

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.

2
Possible duplicate of Set lock true for group of cells - Raidri
Please play fair and mark the question as answered. - Francois Botha

2 Answers

2
votes

You need to also switch protection on for the worksheet with

ws.Protect()

See the documentation and this question for more details.

0
votes

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