为什么我在尝试向我的 Spreadsheet 添加第三个 sheet 时得到 "Invalid index"?

Why am I getting "Invalid index" when trying to add a third sheet to my Spreadsheet?

我有添加第三个的代码sheet:

// contextual code
private Excel.Application _xlApp;
private Excel.Workbook _xlBook;
private Excel.Sheets _xlSheets;
private Excel.Worksheet _xlSheet;
private Excel.Worksheet _xlSheetDelPerf;
private Excel.Worksheet _xlSheetListObjectTest;

// the line that kablooeys (sp?):
_xlSheetListObjectTest = (Excel.Worksheet)_xlSheets.Item[3]; // <= this is line 307, made infamous in the err msg screenshotted below

添加 sheets 1:

_xlSheet = (Excel.Worksheet)_xlSheets.Item[1];

...和 ​​2:

_xlSheetDelPerf = (Excel.Worksheet)_xlSheets.Item[2];

...工作正常,但是当我点击 kablooifies (Item[3]) 的行时,我得到:

为什么?我拍了一张传单并更改了这个:

_xlApp.SheetsInNewWorkbook = 1; // prevent the empty "sheet 2" etc.

...为此:

_xlApp.SheetsInNewWorkbook = 3; // prevent the empty "sheet 2" etc.

...如果将 "SheetsInNewWorkbook" 设置为 1 会阻止我添加更多 sheets,但不会,这没有任何区别。

那么,为什么“2”很好,而“3”是一个坏索引?

更新

对于要求提供更多背景信息的 Yacoub Massad:

_xlBook = _xlApp.Workbooks.Add(Type.Missing);
_xlBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

_xlApp.ActiveWindow.DisplayGridlines = false;
_xlApp.SheetsInNewWorkbook = 3; // prevent the empty "sheet 2" etc.
_xlSheets = _xlBook.Worksheets;

_xlSheet = (Excel.Worksheet)_xlSheets.Item[1];

如果 sheet 3 需要额外的“_xlBook.Worksheets.Add()”,为什么 sheet 2 不需要?

更新 2

这让我 "InvalidIndex" 在下面显示的 "Item[0]" 行上:

_xlApp.ErrorCheckingOptions.BackgroundChecking = false;
_xlApp.SheetsInNewWorkbook = 3;

_xlBook = _xlApp.Workbooks.Add(Type.Missing);
_xlBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_xlBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_xlBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

_xlApp.ActiveWindow.DisplayGridlines = false;

_xlSheets = _xlBook.Worksheets;

_xlSheet = (Excel.Worksheet)_xlSheets.Item[0]; // changed to 0 from 1
_xlSheetDelPerf = (Excel.Worksheet)_xlSheets.Item[1]; // changed to 1 from 2
_xlSheetListObjectTest = (Excel.Worksheet)_xlSheets.Item[2]; // changed to 2 from 3

更新 3

我将Update 2中的代码更改为原来的索引1、2、3(替换建议的0、1、2),我不再在那里得到"InvalidIndex";但是,我现在进一步向下调用 Sort():

fruitList.Range.Sort(
    fruitList.ListColumns[1].Range, Excel.XlSortOrder.xlAscending,
    fruitList.ListColumns[2].Range, Type.Missing, 
        Excel.XlSortOrder.xlAscending,
        Type.Missing, Excel.XlSortOrder.xlAscending,
        Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing,
        Excel.XlSortOrientation.xlSortColumns);

在上下文中:

private void WriteListObjectTestSheet()
{
    //_xlBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing); // see if this helps
    //_xlSheetListObjectTest = (Excel.Worksheet)_xlSheets.Item[2]; // changed to 2 from 3
    _xlSheetListObjectTest.Name = ProduceUsageListObjectSortSheetName;

    _xlSheetListObjectTest.Cells[5, 1] = "Apple";
    _xlSheetListObjectTest.Cells[6, 1] = "Strawberry";
    _xlSheetListObjectTest.Cells[7, 1] = "Cashew";
    _xlSheetListObjectTest.Cells[8, 1] = "Kumquat";
    _xlSheetListObjectTest.Cells[9, 1] = "Pomegranate";
    _xlSheetListObjectTest.Cells[10, 1] = "Banana";
    _xlSheetListObjectTest.Cells[11, 1] = "Pineapple";
    _xlSheetListObjectTest.Cells[12, 1] = "Kiwi";
    _xlSheetListObjectTest.Cells[13, 1] = "Huckleberry";
    _xlSheetListObjectTest.Cells[14, 1] = "Gooseberry";

    Excel.ListObject fruitList =
        _xlSheetListObjectTest.
            ListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange,
                _xlSheetListObjectTest.Range[
                    _xlSheetListObjectTest.Cells[4, 1],
                    _xlSheetListObjectTest.Cells[4, 1]], //13]], 
                Type.Missing, Excel.XlYesNoGuess.xlNo);

    fruitList.Range.Sort(
        fruitList.ListColumns[1].Range, Excel.XlSortOrder.xlAscending,
        fruitList.ListColumns[2].Range, Type.Missing, 
            Excel.XlSortOrder.xlAscending,
            Type.Missing, Excel.XlSortOrder.xlAscending,
            Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing,
            Excel.XlSortOrientation.xlSortColumns);
}

我从 here, and admit that I don't really understand it; I reckon the problem is in the ListColumns1 and/or ListColumns2 中引用 [a,o] 代码,但不知道为什么...

实际上,当您使用以下代码 (Excel.Worksheet)_xlSheets.Item[3] 时,您正在尝试访问第 4 个 sheet 而不是第 3 个 sheet,因为它的索引从零开始。

移动这一行:

_xlApp.SheetsInNewWorkbook = 3;

这一行之前:

_xlBook = _xlApp.Workbooks.Add(Type.Missing);

顾名思义,SheetsInNewWorkbook 设置尚未创建的工作簿的工作表数,而不是已经创建的工作簿。