Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am downloading an Excel file in my application through OpenXMl feature. Here, I have to apply lock on some of the Columns, but not on every column of the sheet.

I am able to lock the whole sheet using the following code:

VB
//************Sheet Protection******************
Protection objProtection = new Protection();
objProtection.Locked = true;
cellFormat.ApplyProtection = true;
cellFormat.Append(objProtection);
SheetProtection objStyleSheetP = new SheetProtection();
objStyleSheetP.Password = "123";
objStyleSheetP.Sheet = true;
objStyleSheetP.Objects = true;
objStyleSheetP.Scenarios = true;
styleSheet.CellFormats.Append(cellFormat);
workSheet.InsertAfter(objStyleSheetP, workSheet.Elements<SheetData>().First());
//***********Till Here********************


I have tried a lot to search on Google, but all in vain. Can anyone please help me in finding the exact code or give an idea of how to work it out.
Thank you!
Posted
Updated 24-Oct-23 13:22pm
v4
Comments
Sunasara Imdadhusen 25-Jun-14 5:51am    
I think you can hide your columns
Varun Sareen 25-Jun-14 6:58am    
Hiding the columns is not required dear friend. There is a functionality on Open XML of doing it that i know but how to do that i want to know.

Thanks
Varun
Nathan Minier 25-Jun-14 9:06am    
I found a video link on the OpenXMLDeveloper Blog[^] for mixing locked and unlocked cells on a sheet. I'm afraid I can't check it at work.
Varun Sareen 25-Jun-14 9:38am    
Thanks Nathan, BTW I also found the same on some site..but cannot check here in office so will check at home.

1 solution

Hi I have find solution:

I add a protection to sheet

SheetProtection objStyleSheetP = new SheetProtection();
objStyleSheetP.Password = "123";
objStyleSheetP.Sheet = true;
objStyleSheetP.Objects = true;
objStyleSheetP.Scenarios = true;

worksheetPart.Worksheet.InsertAfter(objStyleSheetP, worksheetPart.Worksheet.Elements().First());

If I want protect header and not protect body I add cell formats

Protection objProtection = new Protection();
objProtection.Locked = true;

Protection objProtection2 = new Protection();
objProtection2.Locked = false;

CellFormats cellFormats = new CellFormats(
new CellFormat {ApplyProtection = true, Protection= objProtection2 }, // body
new CellFormat {ApplyProtection = true, Protection = objProtection } // header
); ;

styleSheet = new Stylesheet(cellFormats);

I set style index to cells

cell.StyleIndex = 1; // Header

cell.StyleIndex = 0; //Body



Thanks for your help
 
Share this answer
 
Comments
CHill60 18-Oct-23 4:32am    
Be aware you are likely to attract downvotes for this response - it's a very old question and the solution was actually in the comments - the same solution as yours

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900