日期:2014-05-17  浏览次数:20806 次

设置execl工作表出现问题
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.SheetsInNewWorkbook = 4; //设置工作表的个数
excel.Workbooks.Add(true);
Worksheet sheet1 = (Worksheet)excel.ActiveWorkbook.Worksheets[1];
sheet1.Name = "兆基报价";
sheet1.Cells[1, 1] = "兆基报价";

Worksheet sheet2 = (Worksheet)excel.ActiveWorkbook.Worksheets[2];
sheet2.Name = "线割报价";
sheet2.Cells[1, 1] = "线割报价";

excel.Visible = true;
}

Worksheet sheet2 = (Worksheet)excel.ActiveWorkbook.Worksheets[2];
这行代码会出错(无效索引。 (异常来自HRESULT:0x8002000B (DISP_E_BADINDEX)))
但是我在前面设置个工作表的个数为4,这里到索引2就出错了,按理应该是到4都不会出错的吧。不知道为何,前面的设置是否无效. 那位大侠能够帮忙看下问题出在哪?

------解决方案--------------------
C# code
     sheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);