我一直收到空白excel打开错误,没有文件打开

时间:2016-06-29 08:12:35

标签: c# excel

object oMissing = System.Reflection.Missing.Value;
MessageBox.Show("The excel file about to open");
Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
oExcel.Visible = true;
Excel.Workbooks oBooks = oExcel.Workbooks;
Excel._Workbook oBook = null;
MessageBox.Show("opening excel sheet");
oBook = oBooks.Open(fileName, oMissing, oMissing,oMissing,oMissing,oMissing,oMissing, oMissing, oMissing,                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
MessageBox.Show("THE FILE IS OPEN");

MessageBox.Show("Macro about to be run");
RunMacro(oExcel, new Object[] { "Macro1" });

MessageBox.Show("the macro is completed");
oBook.Close(false, oMissing, oMissing); 
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
oBook = null;             
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
oBooks = null;
oExcel.Quit();   
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
oExcel = null;

此代码打开一个空白的Excel窗口,而不是打开的文件。 宏也已定义。

1 个答案:

答案 0 :(得分:0)

为什么不直接使用示例 https://msdn.microsoft.com/en-us/library/b3k79a5x.aspx

替换

oBook = oBooks.Open(fileName, oMissing, oMissing, oMissing, ...

使用

oBook = oBooks.Open(fileName);

另外,我会确保fileName具有您尝试打开的工作簿的完整路径