调用Excel.Workbook.SaveAs后使用Excel.Worksheet.Select时出现异常(HRESULT:0x800AC472)

use*_*390 10 c# excel

我正在打开一个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;
}
Run Code Online (Sandbox Code Playgroud)

输出文件名或路径没有问题,输出文件不存在.它保存前两张然后崩溃.我试着用4张不同的输入文件,它工作得很好,所以它与输入文件有关.

例外:

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: 
Run Code Online (Sandbox Code Playgroud)

任何提示赞赏!

mbm*_*ura 13

在我的情况下,抛出了异常,因为我的excel Interop工具显示了一个模态对话框(与过期的许可证密钥相关 - 对我很羞耻).如果我关闭对话框(这是在后台显示),然后打在Visual Studio"继续",该方案是能够连接到xlsx档案成功地retrive数据.


use*_*390 3

我认为这是一个具有约束力的问题,我只是用以下内容包围每个调用(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);
Run Code Online (Sandbox Code Playgroud)

  • 将异常转变为程序挂起。好的。当您的客户卸载您的程序时,这个问题就会自行解决。 (26认同)
  • 我想说你“必须”对迭代次数添加限制,而不是“可以” (6认同)
  • 这是根据[this](http://social.msdn.microsoft.com/Forums/vstudio/en-US/9168f9f2-e5bc-4535-8d7d-4e374ab8ff09/hresult-800ac472-from-set-operations-的唯一方法in-excel?forum=vsto) 在 MSDN 论坛上发帖。您可以对迭代次数添加限制,或者检查是否只捕获正确类型的异常。 (4认同)