来自HRESULT的异常:0x8002000B(DISP_E_BADINDEX)用于System.Runtime.InteropServices.COMException

时间:2015-03-03 11:32:37

标签: c# .net excel interop excel-interop

我有一部分代码试图将数据(从数据库)导出到Excel。当我尝试执行此任务时,它会生成此错误:

System.Runtime.InteropServices.COMException occurred
Additional information: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

生成此错误的代码:

string ExcelFileName = RootFolder + "\\" + "Work_Sheet.xls";
File.Copy(RootFolder + "\\" + "WorksOrder_Template.xls", ExcelFileName);
Excel.Workbook xlWorkBook;
xlWorkBook = excelApp.Workbooks.Open(ExcelFileName, 0, false, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, false, true, Type.Missing);
Excel.Worksheet Page2;
Excel.Worksheet Page3;
Page2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("Sheet2");
Page3 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("Sheet3");

代码行:

Page3 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("Sheet3");

产生上述错误。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:10)

WorksOrder_Template.xls文件中缺少

“Sheet3”,因此,当代码尝试获取“Sheet3”时,它会生成错误。