为什么我得到"索引无效"在尝试将第三张表添加到我的电子表格时?

时间:2015-12-04 18:23:01

标签: c# excel excel-interop

我已经有了添加第三张表的代码:

// 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

添加表1:

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

...和2:

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

...工作正常,但当我点击kablooifies(项目[3])时,我得到:

enter image description here

为什么呢?我拿了一张传单改了这个:

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

......对此:

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

...以防万一设置&#34; SheetsInNewWorkbook&#34;为1阻止我添加更多的床单,但不,它没有任何区别。

那么为什么&#34; 3&#34; &#34; 2&#34;很好吗?

更新

对于要求更多背景的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];

如果附加&#34; _xlBook.Worksheets.Add()&#34;第3页需要,为什么不使用第2页?

更新2

这给了我&#34; InvalidIndex&#34;在&#34;项目[0]&#34;如下所示:

_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

我将更新2中的代码更改为原始索引1,2和3(替换建议的0,1和2),我不再使用#34; InvalidIndex&#34;那里;但是,我现在进一步向下,在这个调用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);
}

我广告[a,o]来自here的代码,并承认我并非真正理解它;我认为问题出在ListColumns 1和/或ListColumns 2中,但不知道为什么......

2 个答案:

答案 0 :(得分:1)

移动此行:

_xlApp.SheetsInNewWorkbook = 3;

在此之前:

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

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

答案 1 :(得分:0)

实际上,当您使用以下代码(Excel.Worksheet)_xlSheets.Item[3]时,您正尝试访问第4张而非第3张,因为它的基于零的索引。