Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自HRESULT的异常:System.Runtime.InteropServices.COMException的0x8002000B(DISP_E_BADINDEX)_C#_.net_Excel_Interop_Excel Interop - Fatal编程技术网

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

C# 来自HRESULT的异常:System.Runtime.InteropServices.COMException的0x8002000B(DISP_E_BADINDEX),c#,.net,excel,interop,excel-interop,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

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

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");

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

WorksOrder_Template.xls文件中缺少“Sheet3”,因此,当代码试图获取“Sheet3”时,它生成了错误。

…Excel文件中确实有“Sheet3”吗?是的。我也算出来了。由于程序报告(WorksOrder_Template.xls)中存在迁移,因此报告文件中不幸缺少“Sheet3”。将Sheet3添加到该文件后,程序开始正常工作。我提出了你的问题。。。。不知道是谁把它记下来的,因为这是一个大问题,而且似乎没有人能够清楚地解释到底出了什么问题。。。我在尝试创建关系时也会收到此错误消息,但我可以手动执行,但不能在代码中执行。。。尽管我有另一种方法可以添加关系,但没问题,我复制了代码,编辑了它,现在它不起作用了。。。