OpenXML - Protect all sheets

◇◆丶佛笑我妖孽 提交于 2019-12-11 02:48:28

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/9922743/openxml-protect-all-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!