【问题标题】:Exception (HRESULT: 0x800AC472) when using Excel.Worksheet.Select after calling Excel.Workbook.SaveAs调用 Excel.Workbook.SaveAs 后使用 Excel.Worksheet.Select 时出现异常(HRESULT:0x800AC472)
【发布时间】:2013-12-23 05:24:42
【问题描述】:

我正在打开一个 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;
}

输出文件名或路径没有问题,输出文件不存在。它保存前两张纸然后崩溃。我尝试了一个包含 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: 

感谢任何提示!

【问题讨论】:

  • 你具体遇到了什么错误???
  • LOL 我刚刚运行了它,它把它们全部保存了下来。删除文件后再次运行它,它停在第二张纸上!错误是“'System.Runtime.InteropServices.COMException' 类型的未处理异常发生在...附加信息:来自 HRESULT 的异常:0x800AC472”
  • Google“excel 错误 50290”,点击率很高。

标签: c# excel


【解决方案1】:

在我的情况下,抛出异常是因为我的 excel 互操作工具正在显示一个模式对话框(与过期的许可证密钥相关联 - 我真丢脸)。如果我关闭对话框(显示在后台)然后在 Visual Studio 中点击“继续”,程序就能够连接到 xlsx 文件并成功检索数据。

【讨论】:

  • 谢谢,这正是我们的问题。
【解决方案2】:

我认为这是一个绑定问题,我只是在每个调用 (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);

【讨论】:

  • 将异常转化为程序挂起。好的。当您的客户卸载您的程序时,这个问题就会自行解决。
  • 根据this MSDN 论坛上的帖子,这是唯一的方法。您可以为迭代次数添加限制或检查您是否只捕获正确类型的异常。
  • 我会说你“必须”增加迭代次数的限制,而不是“可以”
【解决方案3】:

在我的情况下,有一个相同的异常并检查了任务管理器,打开了很多 EXCEL.exe,杀死它们然后正常工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2011-01-23
    • 2023-04-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多