2017-05-10 1 views
1

J'essaie de définir des permissions de feuille de calcul pour un fichier XLSM en utilisant EPPlus mais il semble que je puisse seulement définir le niveau de protection par défaut, aucune protection individuelle n'étant définie. Pour l'anecdote, j'essaie d'accomplir la méthode 1 par programmation en this article. Voici le code que je utilise:Définition de la protection de la feuille de calcul Excel avec EPPlus

using (var p = new ExcelPackage("output.xlsm")) 
{ 
    var ws = p.Workbook.Worksheets["MySheet"]; 

    // Set some cell values here 

    // Filtering, sorting, protection 
    ws.Cells[7, 1, 10, 5].AutoFilter = true; 
    ws.View.FreezePanes(7, 1); 
    ws.ProtectedRanges.Add("FilteredCells", new ExcelAddress(7, 1, 10, 5)); 

    // Worksheet protection 
    ws.Protection.AllowAutoFilter = true; 
    ws.Protection.AllowDeleteColumns = false; 
    ws.Protection.AllowDeleteRows = false; 
    ws.Protection.AllowEditObject = false; 
    ws.Protection.AllowEditScenarios = false; 
    ws.Protection.AllowFormatCells = false; 
    ws.Protection.AllowFormatColumns = false; 
    ws.Protection.AllowFormatRows = false; 
    ws.Protection.AllowInsertColumns = false; 
    ws.Protection.AllowInsertHyperlinks = false; 
    ws.Protection.AllowInsertRows = false; 
    ws.Protection.AllowPivotTables = false; 
    ws.Protection.AllowSelectLockedCells = false; 
    ws.Protection.AllowSelectUnlockedCells = true; 
    ws.Protection.AllowSort = true; 
    ws.Protection.IsProtected = true; 
    ws.Protection.SetPassword("hunter2"); 

    p.SaveAs(new FileInfo("output.xlsm")); 
} 

Cela va sans erreur, mais quand j'ouvre le fichier dans Excel, ou de le charger de nouveau dans EPPlus, je trouve que les différentes options de protection ont été appliquées:

AllowAutoFilter = false 
AllowDeleteColumns = false 
AllowDeleteRows = false 
AllowEditObject = true 
AllowEditScenarios = true 
AllowFormatCells = false 
AllowFormatColumns = false 
AllowFormatRows = false 
AllowInsertColumns = false 
AllowInsertHyperlinks = false 
AllowInsertRows = false 
AllowPivotTables = false 
AllowSelectLockedCells = true 
AllowSelectUnlockedCells = true 
AllowSort = false 
IsProtected = true 

Ce ne sont évidemment pas les autorisations que j'ai définies auparavant, alors comment puis-je m'assurer qu'elles sont correctement définies? Tout le reste est enregistré correctement.

Répondre

2

Voici la source:

https://github.com/pruiz/EPPlus/blob/master/EPPlus/ExcelSheetProtection.cs

Réglage de la IsProtected propriété est Surgraver vos choix:

public bool IsProtected 
    { 
     get 
     { 
      return GetXmlNodeBool(_isProtectedPath, false); 
     } 
     set 
     { 
      SetXmlNodeBool(_isProtectedPath, value, false); 
      if (value) 
      { 
       AllowEditObject = true; 
       AllowEditScenarios = true; 
      } 
      else 
      { 
       DeleteAllNode(_isProtectedPath); //delete the whole sheetprotection node 
      } 
     } 
    } 

Déplacez votre IsProtected = true appel au début du code ou gérer comme vous le voulez, mais vous remplacerez accidentellement votre choix précédent. Je regarderais les propriétés à ce lien pour voir lesquelles vont remplacer vos sélections existantes.