C# 为什么我会得到;无效索引“;当尝试将第三张工作表添加到我的电子表格时?
我有代码添加第三张图纸:C# 为什么我会得到;无效索引“;当尝试将第三张工作表添加到我的电子表格时?,c#,excel,excel-interop,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 _xlSheetListObjectTes
// 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
…和2:
_xlSheetDelPerf = (Excel.Worksheet)_xlSheets.Item[2];
…工作正常,但当我点击kablooifies(第[3]项)时,我得到:
为什么??我拿了张传单,改变了这个:
_xlApp.SheetsInNewWorkbook = 1; // prevent the empty "sheet 2" etc.
……为此:
_xlApp.SheetsInNewWorkbook = 3; // prevent the empty "sheet 2" etc.
…如果将“SheetWenwWorkbook”设置为1会阻止我添加更多的工作表,但是没有,这没有什么区别
那么,为什么“3”是一个糟糕的指数,而“2”是好的呢
更新
对于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];
如果第3页需要额外的“\u xlBook.Worksheets.Add()”,为什么第2页不需要
更新2
这将在下面显示的“项目[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
我将更新2中的代码更改为原始索引1、2和3(替换建议的0、1和2),并且不再在那里获得“InvalidIndex”;但是,我现在在下面调用Sort()时执行以下操作:
在这方面:
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);
}
我接受了那个代码,并承认我并不真正理解它;我想问题出在ListColumns和/或ListColumns中,但不知道为什么…实际上,当您使用以下代码
(Excel.Worksheet)\u xlSheets.Item[3]
时,您试图访问第四页而不是第三页,因为它的索引是零基的。移动此行:
_xlApp.SheetsInNewWorkbook = 3;
_xlBook = _xlApp.Workbooks.Add(Type.Missing);
在这一行之前:
_xlApp.SheetsInNewWorkbook = 3;
_xlBook = _xlApp.Workbooks.Add(Type.Missing);
顾名思义,
sheetsinewworkbook
为尚未创建的工作簿而不是已经创建的工作簿设置工作表的数量。我没有Excel Interop的经验,但是为什么访问数组/列表索引\xlSheets。项[1]
会创建新的工作表?在我看来,索引1处的工作表似乎是以前创建(或添加)的,您只是在访问它。会发生什么情况?您在设置[0]
索引而不是[1]
时开始使用\u xlApp.sheetsinewworkbook
?创建工作簿之前还是之后?你能显示更完整的代码吗?添加第1项和第2项工作;只有当加3时,它才会爆炸。那么为什么1和2起作用呢?Excel互操作似乎在其他任何地方都是基于1的(行索引、列索引)。如果创建新的Excel,默认情况下会创建三个工作表。我认为这就是为什么1&2索引对您有效;从0开始显然是一个错误。所以您必须告诉它要创建多少,然后显式创建它们?不。您在哪里显式创建它们<代码>\u xlSheets.Item[3]不创建工作表,它只是访问工作表。“\u xlSheet=(Excel.Worksheet)\u xlSheets.Items[N];”代码正在显式创建它们,不是吗?\u xlBook.Worksheets.Add(Type.Missing)
将添加新工作表。但是,如果您将sheetwWorkbook
设置为10,然后创建工作簿,然后调用\xlBook.Worksheets.Add(Type.Missing)
,那么您将总共得到11张工作表。是。只要在创建工作簿之前设置它。