2012-03-29 168 views
2

我只能保護一張工作表,但當我嘗試保護Excel文檔中的所有工作表時,在嘗試附加SheetProtection時出現錯誤。OpenXML - 保護所有工作表

如果我理解正確,我必須在SheetData之後附加SheetProtection。 這裏是我的代碼:

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

foreach(Sheet sheet in wSheet) 
{ 
    WorksheetPart worksheetPart = GetWorkSheetPartBySheetID(m_SpreadsheetDocument.WorkbookPart, sheet.Id); 
    worksheetPart.Worksheet.InsertAfter(wSheetProtection, worksheetPart.Worksheet.Descendants<SheetData>().LastOrDefault());      
} 
+3

它更好地爲您的問題創建一個答案,而不是改變問題本身。 – Flowerking 2012-03-29 15:13:52

回答

0

通過爲每個表創建的SheetProtection實例解決了這個。