c# delete excel sheets using for loop

0

I am trying to delete excel sheets using for loop except selected sheet by User using Dropdown menu in asp.net web application. So, I have written a code in c#.

int index = DropDownList1.SelectedIndex;
int max = DropDownList1.Items.Count;
int i=3;
for (i=1; i <= max; i++)
{
    if (i != index+1)
    {
        Excel.Worksheet worksheets = (Excel.Worksheet)xlWorkBook.Sheets[i];
        worksheets.Delete();
    }

}

And I am getting an error like this -

System.Runtime.InteropServices.COMException: 'Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))'

c#
asked on Stack Overflow Jun 7, 2017 by Karan • edited Jun 7, 2017 by Mike Beeler

4 Answers

2

Both xlWorkBook.Sheets and DropDownList1.SelectedIndex indexes starts at 0, not 1, so you need to change your loop like this:

int index = DropDownList1.SelectedIndex;
int max = DropDownList1.Items.Count;

for (int i = 0; i < max; i++)
{
    if (i != index)
    {
        Excel.Worksheet worksheets = (Excel.Worksheet)xlWorkBook.Sheets[i];
        worksheets.Delete();
    }
}
answered on Stack Overflow Jun 7, 2017 by Koby Douek
0

Keep in mind that while deleting a worksheet, index position might get change so you'll also need to decrease the int value to get correct index otherwise you'll again get the same exception while deleting the sheets afterwards or lead to delete a sheet which you dont want to delete

int index = DropDownList1.SelectedIndex;
int max = DropDownList1.Items.Count;

for (int i = 0; i < max; i++)
{
    if (i != index)
    {
        Excel.Worksheet worksheet = (Excel.Worksheet)xlWorkBook.Sheets[i];
        worksheet.Delete();

        //decrease the value
        --i;
    }
}
answered on Stack Overflow Jun 7, 2017 by kashi_rock
0

I don't know but anyhow when I reversed the loop, It started working.

for (i=max; i > 0; i--)
{
    if (i != index+1)
    {
         Excel.Worksheet worksheets = (Excel.Worksheet)xlWorkBook.Sheets[i];
         worksheets.Delete();
    }

}
answered on Stack Overflow Jun 7, 2017 by Karan
0

Lets index=5;
The error means that the sheet at index 5 is not available.
You are correct that the index of worksheet starts with 1 and not 0.

I have done some modification in your code. I am performing an additional check of your current index (i) with the Sheets count present in the workbook to skip the Bad Index Error. Hope this will work.

 using System;
 using Excel = Microsoft.Office.Interop.Excel;
 public partial class WebForm1 : System.Web.UI.Page
 {
     protected void Page_Load(object sender, EventArgs e)
     {     
         int i = 3, max = 5, index = 1;
         string filePath = @"C:\Users\anandra\Desktop\Book1.xlsx";
         Excel.Application excelApp = new Excel.Application();
         Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);
         for (i = 1; i <= max; i++)
         {
           //Adding an extra check here to skip your error
             if (i != index + 1 && workBook.Sheets.Count>i )
             {
                 Excel.Worksheet worksheets = (Excel.Worksheet)workBook.Sheets[i];
                 excelApp.DisplayAlerts = false;
                 worksheets.Delete();
                 excelApp.DisplayAlerts = true;
                 //Decreasing the value of index and i as after deleting the sheet the index will start agarin from 1.
                 i--;
                 index--;
             }
         }
     }
 }
answered on Stack Overflow Jun 7, 2017 by RahulGo8u • edited Jun 7, 2017 by RahulGo8u

User contributions licensed under CC BY-SA 3.0