【发布时间】:2021-06-10 17:43:07
【问题描述】:
虽然我的 C# 程序将数据连续写入 Excel 电子表格,但如果最终用户单击右上角的菜单并打开 Excel 选项 窗口,这会导致以下异常:
System.Runtime.InteropServices.COMException with HRESULT: 0x800AC472
这会中断数据写入电子表格。
理想情况下,应该允许用户执行此操作而不会导致异常。
我发现此错误代码的唯一解决方案是循环并等待异常消失: Exception from HRESULT: 0x800AC472 这有效地挂起应用程序,数据未写入 Excel,用户对问题一无所知。
我曾想过在写入时禁用 Excel 的主菜单,但找不到有关如何执行此操作的参考。
我的应用支持 Excel 2000 到 2013。
这里是重现问题的方法:
-
使用 Visual Studio Express 2013 for Windows Desktop、.NET 4.5.1 在 Windows 7 64 位和 Excel 2007 上创建一个新的 Visual C# 控制台应用程序项目。
-
添加对“Microsoft ExceL 12.0 Object Library”(用于 Excel)和“System.Windows.Forms”(用于消息框)的引用。
完整代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading.Tasks; using System.Threading; // for sleep using System.IO; using System.Runtime.InteropServices; using System.Reflection; using Microsoft.Win32; using Excel = Microsoft.Office.Interop.Excel; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int i = 3; // there is a split pane at row two Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; try { object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlApp.Visible = false; xlWorkBook = xlApp.Workbooks.Add(misValue); xlApp.Visible = true; xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // next 2 lines for split pane in Excel: xlWorkSheet.Application.ActiveWindow.SplitRow = 2; xlWorkSheet.Application.ActiveWindow.FreezePanes = true; xlWorkSheet.Cells[1, 1] = "Now open the"; xlWorkSheet.Cells[2, 1] = "Excel Options window"; } catch (System.Runtime.InteropServices.COMException) { System.Windows.Forms.MessageBox.Show("Microsoft Excel does not seem to be installed on this computer any longer (although there are still registry entries for it). Please save to a .tem file. (1)"); return; } catch (Exception) { System.Windows.Forms.MessageBox.Show("Microsoft Excel does not seem to be installed on this computer any longer (although there are still registry entries for it). Please save to a .tem file. (2)"); return; } while(i < 65000) { i++; try { xlWorkSheet.Cells[i, 1] = i.ToString(); Thread.Sleep(1000); } catch (System.Runtime.InteropServices.COMException) { System.Windows.Forms.MessageBox.Show("All right, what do I do here?"); } catch (Exception) { System.Windows.Forms.MessageBox.Show("Something else happened."); } } Console.ReadLine(); //Pause } } } -
启动应用程序,Excel 出现并且数据被写入其中。从菜单中打开 Excel 选项对话框窗口并弹出错误:
“System.Runtime.InteropServices.COMException”类型的异常发生在 mscorlib.dll 中,并且在托管/本机边界之前未处理
附加信息:HRESULT 异常:0x800AC472 -
点击继续,我的消息框“好的,我在这里做什么?”出现。
请指教?
最好的问候, 伯特兰
【问题讨论】:
-
All right, what do I do here?- 如果您无法避免抛出异常,那么您需要一种方法来处理它。The only solution I found to this error code was to loop and wait until the exception went away或I thought about disabling the main menu of Excel while writing to it, but cannot find a reference on how to do this.- 你不能在 Excel 中进入全屏模式或禁用功能区吗? -
听起来很深。如果您愿意禁用菜单,另一种方法是创建一个 99% 透明的喷嚏防护装置,它跟随 excel 窗口,但这仍然留下了键盘访问的问题。我想说最好的解决方案是抛出一个工具提示类型通知,让用户知道你在你的异常循环中。
-
我怎么做:在没有功能区的旧版本 Excel 中打开“关于”窗口会出现同样的错误,因此这不是功能区问题。
-
Mark Robbins:禁用菜单是一种可行但不受欢迎的解决方案。我真的想解决这个异常。
-
excel 的问题是每个工作簿都是单线程的,所以它不能异步工作。正如其他人所提到的,您最好在工作时将用户锁定在电子表格中。或者发一条消息说请不要在我工作的时候这样做
标签: c# .net excel excel-interop