C# .NET - How do I create an excel file with locked (read-only) cells?

Asked By Greg Abernethy on 18-Sep-07 03:41 PM

I want to create an excel file and format some columns/cells so they are read-only and the users can't change the data. Note that I want the users to be able to change some of the columns, but not all so setting the whole file to read-only would not work.


It's not too hard I think - Jacquie Horner replied to Greg Abernethy on 19-Sep-07 05:02 AM

Try this

Write the sheet you want then

Select the cells or columns you want to protect

Then go to
end tab protection

Do that for the entire sheet

Then go to
protect sheet

the cells you have locked you should not be able to change the cells that are not locked should be able to be changed

Sorry - I meant using C# to format the cells. - Greg Abernethy replied to Jacquie Horner on 19-Sep-07 10:25 AM

I need to create an excel file using C# and I need to format some of the columns using C# to make them read-only. I can do some basic formatting on excel cells using Microsoft.Office.Interop.Excel.Range such as:

Range r = excelWorksheet.get_Range("A1","D1");

r.Font.Bold = true;

r.wrapText = true;

r.Cells.RowHeight = 34;

but I cannot find a way to make a Range readonly. There is an AllowEdit getter method, but I do not see where this is set.

read only - Jacquie Horner replied to Greg Abernethy on 19-Sep-07 11:43 AM

Sorry can't help you on that,

I learnt how to lock cells this way from someone I worked with in accounts, locking the cells and then protecting them essentially makes they read only and the cells you haven't locked will be open to edit.

Can you lock a range and then protect?

Thanks! - Greg Abernethy replied to Jacquie Horner on 19-Sep-07 03:25 PM

Yes - I can lock a range and then protect.

Thanks again.

abc - Manmohan Bradeeya replied to Greg Abernethy on 01-Oct-07 01:35 PM
hello how are you
help on this. - nayan abhiram replied to Greg Abernethy on 20-Jan-09 09:07 AM
hey Greg, i am also facing the problem. I want to make a particular few cells on my sheet read only, am using C# too. Could you please post the code to do the same?!

Will be very thankful.

I used a COM object to create the excel file and - Greg Abernethy replied to nayan abhiram on 27-Jan-09 05:17 PM

made the entire file read-only. Then I formatted the columns (range) that I wanted the users to be able to edit by setting the ExcelRange.locked(false). Then I password protected the file so the users could not change the read-only settings for the columns I did not want them to change.

There is an article here that might help you:




bab replied to Greg Abernethy on 16-May-11 11:20 AM
Using HTML / XML to create the excel file, you can set the style of the cells to locked/unlocked or hidden/visible.
eg:  style="mso-protection: locked hidden"