c#使用for循环删除excel表格

c#使用for循环删除excel表格,c#,C#,我正在尝试使用asp.net web应用程序中的下拉菜单删除除用户选择的工作表之外的使用for循环的excel工作表。因此,我用c#编写了一个代码 int index=DropDownList1.SelectedIndex; int max=DropDownList1.Items.Count; int i=3; 对于(i=1;i这两个xlWorkBook.Sheets和DropDownList1.SelectedIndex索引从0开始,而不是1,因此您需要像这样更改循环: int index =

我正在尝试使用asp.net web应用程序中的下拉菜单删除除用户选择的工作表之外的使用for循环的excel工作表。因此,我用c#编写了一个代码

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

对于(i=1;i这两个
xlWorkBook.Sheets
DropDownList1.SelectedIndex
索引从
0
开始,而不是
1
,因此您需要像这样更改循环:

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();
    }
}
int index=DropDownList1.SelectedIndex;
int max=DropDownList1.Items.Count;
对于(int i=0;i
两个
xlWorkBook.Sheets
DropDownList1.SelectedIndex
索引从
0
开始,而不是
1
,因此您需要像这样更改循环:

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();
    }
}
int index=DropDownList1.SelectedIndex;
int max=DropDownList1.Items.Count;
对于(int i=0;i
请记住,在删除工作表时,索引位置可能会发生更改,因此您还需要减小int值以获得正确的索引,否则在随后删除工作表时会再次出现相同的异常,或导致删除不想删除的工作表

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;
    }
}
int index=DropDownList1.SelectedIndex;
int max=DropDownList1.Items.Count;
对于(int i=0;i
请记住,在删除工作表时,索引位置可能会发生更改,因此您还需要减小int值以获得正确的索引,否则在随后删除工作表时会再次出现相同的异常,或导致删除不想删除的工作表

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;
    }
}
int index=DropDownList1.SelectedIndex;
int max=DropDownList1.Items.Count;
对于(int i=0;i
我不知道,但无论如何,当我反转循环时,它开始工作了

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

}

我不知道,但不管怎样,当我反转循环时,它开始工作了

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

}
Lets索引=5;
该错误表示索引5处的工作表不可用。
工作表的索引以1开始,而不是以0开始,这是正确的

我已对您的代码进行了一些修改。我正在使用工作簿中的工作表计数对您当前的索引(I)进行额外检查,以跳过错误索引。希望这能起作用

 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--;
             }
         }
     }
 }
使用系统;
使用Excel=Microsoft.Office.Interop.Excel;
公共部分类WebForm1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{     
int i=3,max=5,index=1;
字符串文件路径=@“C:\Users\anandra\Desktop\Book1.xlsx”;
Excel.Application excelApp=新的Excel.Application();
Excel.Workbook工作簿=excelApp.Workbooks.Open(文件路径);
对于(i=1;i)
{
Excel.Worksheet worksheets=(Excel.Worksheet)workBook.Sheets[i];
excelApp.DisplayAlerts=false;
工作表。删除();
excelApp.DisplayAlerts=true;
//减少索引和i的值,因为删除表后,索引将从1开始。
我--;
索引--;
}
}
}
}
让索引=5;
该错误表示索引5处的工作表不可用。
工作表的索引以1开始,而不是以0开始,这是正确的

我已对您的代码进行了一些修改。我正在使用工作簿中的工作表计数对您当前的索引(I)进行额外检查,以跳过错误索引。希望这能起作用

 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--;
             }
         }
     }
 }
使用系统;
使用Excel=Microsoft.Office.Interop.Excel;
公共部分类WebForm1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{     
int i=3,max=5,index=1;
字符串文件路径=@“C:\Users\anandra\Desktop\Book1.xlsx”;
Excel.Application excelApp=新的Excel.Application();
Excel.Workbook工作簿=excelApp.Workbooks.Open(文件路径);
对于(i=1;i)
{
Excel.Worksheet worksheets=(Excel.Worksheet)workBook.Sheets[i];
excelApp.DisplayAlerts=false;
工作表。删除();
excelApp.DisplayAlerts=true;
//减少索引和i的值,因为删除表后,索引将从1开始。
我--;
索引--;
}
}
}
}

我相信索引从零开始!你从1开始。书中有两张工作表吗?[0]&[1]您的Excel文件有多少工作表?工作簿中有三个工作表。我做过研究,对于工作表,索引仅从1开始。我相信索引从零开始!您从1开始。书中有两个工作表吗?[0]&[1]您的Excel文件有多少工作表?工作簿中有三个工作表。我做过研究,工作表索引仅从1开始。这是因为如果删除工作表2,则工作表3将变为工作表2(2以上的所有索引都会从1中下拉)。因此,从最高到最低可以避免此问题-它将影响的唯一索引是它已经查看过的索引。这是因为如果删除表2,则表3将变为表2(2以上的所有索引都会下拉一个)。因此