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

Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX) for System.Runtime.InteropServices.COMException

我有一部分代码试图将数据(从数据库)导出到 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");

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

"Sheet3" 从 WorksOrder_Template.xls 文件中丢失,因此,当代码试图获取 'Sheet3' 时,它产生了错误。