为什么当我尝试向我的电子表格中添加第三张表格时出现“无效索引”?

我有代码添加第三个工作表:

// 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(Item [3])时,我得到:

在这里输入图像说明

为什么? 我拿了传单,并改变了这一点:

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

对此:

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

…如果将“SheetsInNewWorkbook”设置为1,可能会阻止我添加更多的表单,但是不会。

那么为什么当“2”罚款时“3”是一个糟糕的指数?

UPDATE

雅各布马萨德,谁要求更多的上下文:

 _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]; 

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

更新2

这给了我下面显示的“Item [0]”行上的“InvalidIndex”:

 _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(replacebuild议的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); } 

我广告[a,o]从这里推出了这些代码,并承认我并不真正了解它。 我认为问题是在ListColumns 1和/或ListColumns 2 ,但不知道为什么…

移动这一行:

 _xlApp.SheetsInNewWorkbook = 3; 

在此行之前:

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

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

实际上,当您使用以下代码(Excel.Worksheet)_xlSheets.Item[3]由于其基于零的索引,您正试图访问第四张而非第三张。