Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete non-empty worksheets from excel workbook

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

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

Any clues?

like image 346
Jan Avatar asked Oct 05 '22 03:10

Jan


1 Answers

try to turn off 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;
like image 54
Dzmitry Martavoi Avatar answered Oct 13 '22 11:10

Dzmitry Martavoi