简体   繁体   中英

C# Excel protected sheet

I am using c# to do some automation with Excel. I compile the data and create a table around it so that a user can calculate the sum of a column. Everytime I protect the sheet, no matter which options I turn true or false, I can never gain access to that feature again. Is there a way to do it?

here is the relevant function definition from Microsoft https://msdn.microsoft.com/en-us/library/office/ff840611.aspx

a snippet of relevant code

warrantySheet.Cells.Locked = false;
getWarrantySheetUsedRange().Locked = true;
warrantySheet.Protect(SHEET_PASSWORD,true, true, true,true, true, true, true,false, false,false,false,false, true,true,true);

here is the imgur for what I am talking about http://imgur.com/TXjDXdt

You're locking the used range where subtotals functionality happens (Sum, Average, Count, etc.) Assuming you only have 1 table ( ListObject ) on your sheet. Try this:

warrantySheet.Cells.Locked = false;
getWarrantySheetUsedRange().Locked = true;
warrantySheet.ListObjects[1].TotalsRowRange.Locked = false;
warrantySheet.Protect(SHEET_PASSWORD,true, true, true,true, true, true, true,false, false,false,false,false, true,true,true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM