Question about making a column read-only


#1

Can someone please post sample code to make an entire column read-only? (Is that even possible in Excel?)

If there is a solution to this, what version of Excel is required for this functionality to work?

Thanks.


#2

Following is sample code to make column B read-only.

Excel excel = new Excel();

//Protects the whole worksheet
excel.Worksheets[0].Protect(ProtectionType.All);

//Unlocks all columns except column B
for(int i = 0; i < 256; i ++)
{
if(i != 1)
{
Style style = excel.Worksheets[0].Cells.Columns[(byte)i].Style;
style.IsLocked = false;
excel.Worksheets[0].Cells.Columns[(byte)i].Style = style;
}
}
excel.Save("d:\\test\\abc.xls");


#3

Thanks, Laurence. Does this work only with Excel XP?


#4

It works from Excel97 to Excel2003.


#5

Excellent. Thank you.