Delete non-empty worksheets from an Excel workbook

I want to delete some sheets from an Excel workbook. When my program is loaded, it reads the sheets in the book, lists them in a gridview, where the user can select which sheets should be in the output file. When the user clicks the save button, I delete the worksheets based on the selection and save the workbook. It all works. EXCEPT when there is content on the worksheet. This will remove the empty worksheets, but not the worksheets.

foreach (var item in _view.Sheets) { Exc.Worksheet ws = wb.Worksheets[item.Name]; if (!item.Include) { ws.Delete(); } } 

Any clues?

+4
source share
1 answer

try disabling alerts:

  app.DisplayAlerts = false; foreach (var item in _view.Sheets) { Exc.Worksheet ws = wb.Worksheets[item.Name]; if (!item.Include) { ws.Delete(); } } app.DisplayAlerts = true; 
+3
source

Source: https://habr.com/ru/post/1480746/


All Articles