Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenXML - Protect all sheets

I'm able to protect just one sheet, but when I try to protect all sheets in my Excel Documents I get an error while trying to append the SheetProtection.

If I've understood correctly, I must append SheetProtection after the SheetData. Here is my code:

SheetProtectiond sheetProtection = 
  new SheetProtection                              
  {
    Sheet = true,
    Objects = true,
    Scenarios = true,
    Password = GetSheetPassword(workbookPassword)
  };

foreach(Sheet sheet in sheet)
{
  WorksheetPart worksheetPart = 
    GetWorksheetPartBySheetID(m_SpreadsheetDocument.WorkbookPart, sheet.Id);
  worksheetPart.Worksheet.InsertAfter(sheetProtection, 
    worksheetPart.Worksheet.Descendants<SheetData>().LastOrDefault());                      
}
like image 772
andrea404 Avatar asked Jul 02 '26 12:07

andrea404


1 Answers

Solved this by creating an instance of SheetProtection for each sheets.

like image 66
andrea404 Avatar answered Jul 05 '26 07:07

andrea404



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!