访问第二张总是无效索引

Access second sheets always invalid index

我正在尝试创建一个新的 excel sheet,但是当我尝试访问第二个 sheet 时,它总是抛出无效索引异常。

总是在xlWs3 = xlWb2.Worksheets.get_Item(2);

处显示错误
xlApp2 = new Excel.Application();
xlWb2 = xlApp2.Workbooks.Add();
xlWs2 = xlWb2.Worksheets.get_Item(1);
xlWs2.Name = "Format OR";
for (int j = 1; j < rowor; j++)
{
    xlWb2.Sheets[1].Activate();
    if (fulldataOR[j][23] != "RJCT")
    {
        createHeaderformatOR(xlWs2);
        xlWs2.Cells[berhasilOr, 1] = fulldataOR[j][2];
        xlWs2.Cells[berhasilOr, 2] = fulldataOR[j][3];
        xlWs2.Cells[berhasilOr, 3] = fulldataOR[j][4];
        xlWs2.Cells[berhasilOr, 4] = fulldataOR[j][5];
        xlWs2.Cells[berhasilOr, 5] = fulldataOR[j][6];
        xlWs2.Cells[berhasilOr, 6] = fulldataOR[j][7];
        xlWs2.Cells[berhasilOr, 7] = fulldataOR[j][8];
        xlWs2.Cells[berhasilOr, 8] = fulldataOR[j][9];
        xlWs2.Cells[berhasilOr, 9] = fulldataOR[j][12];
        xlWs2.Cells[berhasilOr, 10] = fulldataOR[j][13];
        xlWs2.Cells[berhasilOr, 11] = fulldataOR[j][14];
        xlWs2.Cells[berhasilOr, 12] = fulldataOR[j][15];
        xlWs2.Cells[berhasilOr, 13] = fulldataOR[j][16];
        xlWs2.Cells[berhasilOr, 14] = fulldataOR[j][17];
        xlWs2.Cells[berhasilOr, 15] = fulldataOR[j][18];
        xlWs2.Cells[berhasilOr, 16] = fulldataOR[j][19];
        xlWs2.Cells[berhasilOr, 17] = fulldataOR[j][20];
        xlWs2.Columns.AutoFit();
        berhasilOr++;
    }
}

xlWs3 = xlWb2.Worksheets.get_Item(2);
xlWs3.Name = "PBK - USD";
for (int j = 1; j < rowusd; j++)
{
    xlWb2.Sheets[2].Activate();
    if (fulldataUSD[j][9] == "USD")
    {
        if (fulldataUSD[j][11] != "RJCT")
        {
            //MessageBox.Show(fulldataUSD[j][1]);
            createHeaderPBKUSD2(xlWs3);
            xlWs3.Cells[berhasilusd, 1] = fulldataUSD[j][4];
            xlWs3.Cells[berhasilusd, 2] = fulldataUSD[j][5];
            xlWs3.Cells[berhasilusd, 3] = "";
            xlWs3.Cells[berhasilusd, 4] = "";
            xlWs3.Cells[berhasilusd, 5] = "";
            xlWs3.Cells[berhasilusd, 6] = "";
            xlWs3.Cells[berhasilusd, 7] = "";
            xlWs3.Cells[berhasilusd, 8] = "";
            xlWs3.Cells[berhasilusd, 9] = "";
            xlWs3.Cells[berhasilusd, 10] = "";
            xlWs3.Cells[berhasilusd, 11] = fulldataUSD[j][3];
            xlWs3.Cells[berhasilusd, 12] = "";
            xlWs3.Cells[berhasilusd, 13] = fulldataUSD[j][2];
            xlWs3.Cells[berhasilusd, 14] = fulldataUSD[j][9];
            xlWs3.Cells[berhasilusd, 15] = fulldataUSD[j][6];
            xlWs3.Cells[berhasilusd, 16] = "";
            xlWs3.Cells[berhasilusd, 17] = fulldataUSD[j][7];
            xlWs3.Cells[berhasilusd, 18] = "";
            xlWs3.Cells[berhasilusd, 19] = "";
            berhasilusd++;
            xlWs3.Columns.AutoFit();
        }
    }
}

感谢您的帮助

首先尝试从索引 0 开始。

xlWs2 = xlWb2.Worksheets.get_Item(0);

来自 MasLoo 的评论:

  1. 检查您的新工作簿中是否还有 xlWb.Sheets.Count

  2. 如果 count == 1 ,你应该先添加一个新的工作表 xlWb.Sheets.Add(After: xlWb.Sheets[xlWb.Sheets.Count]);

  3. 之后你调用了 xlWb.Worksheets.get_Item(2);

  4. 尽情享受吧!

谢谢 MasLoo 的帮助