title: Setting Excel worksheet protection with EPPlus
body: <p>I'm trying to set worksheet permissions for an XLSM file using EPPlus but it seems I can only set the default protection level, individual protections are not being set. For the record, I'm trying to accomplish programmatically method 1 in <a href="http://blog.softartisans.com/2013/10/01/kb-sorting-locked-cells-in-protected-worksheets/" rel="nofollow noreferrer">this article</a>. Here's the code I'm using:</p>
  
  <pre><code>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"));
  }
  </code></pre>
  
  <p>This runs without errors, but when I open the file in Excel, or load it back into EPPlus, I find that different protection options have been applied:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>These obviously aren't the permissions I set before, so how can I make sure they are set correctly? Everything else is saved correctly.</p>