andrea404
andrea404

Reputation: 149

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());                      
}

Upvotes: 3

Views: 4067

Answers (1)

andrea404
andrea404

Reputation: 149

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

Upvotes: 1

Related Questions