title: EPPlus and Deleting sheets
body: <p>So I'm using EPPlus to populate a spreadsheet with data. Under certain conditions a sheet will be intentionally deleted, leaving only one sheet with data. The code runs without errors and runs when all sheets are left in, but when after I delete sheets, save and open the workbook I get a series of warnings and errors. The workbook <em>will</em> open normally after that and my process others seems to work fine. Any thoughts? Is deleting the sheets causing the OOXML to be come invalid, and I'm missing a step? Thanks.</p>
  
  <p><a href="https://i.stack.imgur.com/cLUES.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cLUES.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/GvyIG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GvyIG.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/8CBxv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8CBxv.png" alt="enter image description here"></a></p>
  
  <pre><code>    Using template As New MemoryStream(My.Resources.POWER_DOCKET_V2_Template)
      Using excel As New ExcelPackage(template)
  
          For worksheet = excel.Workbook.Worksheets.Count To 1 Step -1
          Dim grid = CType(radPageViewList.Find(Function(page) page.Name = pageGridSheetMap.Find(Function(sheet) sheet.WorksheetName = excel.Workbook.Worksheets(worksheet).Name).PageViewPageName).Controls(0), RadGridView)
          Dim workSheetForSelectedGrid As ExcelWorksheet = excel.Workbook.Worksheets(pageGridSheetMap.Find(Function(g) g.RadGridViewName = selectedGid.Name).WorksheetName)
          Dim rowRange As String = pageGridSheetMap.Find(Function(r) r.RadGridViewName = grid.Name).RowRange
          Dim sheetRange As String = pageGridSheetMap.Find(Function(r) r.RadGridViewName = grid.Name).SheetRange
          Dim rightMostColumn As Integer = pageGridSheetMap.Find(Function(r) r.RadGridViewName = grid.Name).RightMostColumn
  
          If exportSelectedGridOnly = True And excel.Workbook.Worksheets(worksheet).Name &lt;&gt; workSheetForSelectedGrid.Name Then
              excel.Workbook.Worksheets.Delete(worksheet)
          Else
              WriteRows(excel.Workbook.Worksheets(worksheet), grid, rowRange, sheetRange, rightMostColumn)        
          End If
          Next worksheet
  
          If Not Directory.Exists(ConfigurationManager.AppSettings("OutlawTempFolder")) Then Directory.CreateDirectory(ConfigurationManager.AppSettings("OutlawTempFolder"))
          excel.SaveAs(New FileInfo(filename))
          Process.Start(filename)
  
      End Using
      End Using
  </code></pre>