exception(HRESULT:0x800AC472)使用Excel.Worksheet.Select时调用Excel.Workbook.SaveAs

我正在打开一个xlsx文件,并将每个工作表保存为一个csv文件。

以下是保存的相关代码:

int i=0; foreach (Excel.Worksheet s in app.ActiveWorkbook.Sheets) { s.Select(true); // Error here String outfile = outputpath + "("+i+")" + outputfilename + ".csv"; wkb.SaveAs(outfile, Excel.XlFileFormat.xlCSVMSDOS); ++i; } 

输出文件名或path没有问题,并且输出文件不存在。 它保存了前两张,然后崩溃。 我试着用4张不同的input文件,它完美的工作,所以它与input文件。

例外

 System.Runtime.InteropServices.COMException was unhandled HResult=-2146777998 Message=Exception from HRESULT: 0x800AC472 Source=ExcelXlsx2Csv ErrorCode=-2146777998 StackTrace: at Microsoft.Office.Interop.Excel._Worksheet.Select(Object Replace) at ExcelXlsx2Csv.Program.Main(String[] args) in c:\Users\Edward\Documents\Visual Studio 2013\Projects\ExcelXlsx2Csv\ExcelXlsx2Csv\Program.cs:line 109 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: 

任何提示赞赏!

在我的情况下,抛出exception是因为我的Excel Interop工具显示一个模式对话框(与过期的许可证密钥相关 – 对我来说是耻辱)。 如果我closures了对话框(在后台显示),然后在Visual Studio中点击“继续”,程序就可以连接到xlsx文件并成功检索数据。

我认为这是一个有约束力的问题,我只是用每个呼叫(SaveAs,Select)

 bool failed = false; do { try { // Call goes here failed = false; } catch (System.Runtime.InteropServices.COMException e) { failed = true; } System.Threading.Thread.Sleep(10); } while (failed);