Ber*_*ghy 9 .net c# excel excel-interop
当我的C#程序将数据连续写入Excel电子表格时,如果最终用户单击右上方菜单并打开Excel选项窗口,则会导致带有HRESULT的System.Runtime.InteropServices.COMException:0x800AC472中断数据被写入到电子表格.
理想情况下,应允许用户执行此操作而不会导致异常.
我发现这个错误代码的唯一解决方案是循环并等待,直到异常消失: 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对象库"(对于Excel)和"System.Windows.Forms"(对于messagebox)的引用.
这是完整的代码:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
Lanch应用程序,Excel出现并写入数据.从菜单中打开Excel选项对话框窗口,然后弹出错误:
mscorlib.dll中出现"System.Runtime.InteropServices.COMException"类型的异常,并且在托管/本地边界之前未处理
附加信息:HRESULT的异常:0x800AC472
点击继续和我的消息框"好的,我该怎么办?" 出现.
请指教?
最好的问候,伯特兰
小智 5
我们终于解决了这个问题。他们的最终回应是:
我能够重现该问题。我对此进行了进一步研究,发现这种行为是预期的并且是设计使然。由于Excel繁忙并且将不为任何对象模型调用提供服务,因此引发了此异常0x800AC472 – VBA_E_IGNORE。这是讨论此问题的讨论之一。 http://social.msdn.microsoft.com/Forums/vstudio/en-US/9168f9f2-e5bc-4535-8d7d-4e374ab8ff09/hresult-800ac472-from-set-operations-in-excel?forum=vsto 周围的工作我看到的是明确捕获此异常,然后在一段时间后重试,直到完成您想要的操作为止。
由于我们无法理解可能决定打开窗口或做笔记的用户的想法,而没有意识到软件已停止记录(如果掩盖了错误),因此我们决定使用以下方法:
xlWorkSheet.EnableSelection = Microsoft.Office.Interop.Excel.XlEnableSelection.xlNoSelection;
Run Code Online (Sandbox Code Playgroud)
锁定Excel窗口UI。我们提供了一个明显的“解锁”按钮,但是当用户单击该按钮时,会在消息框中严厉警告他,并显示“您是否希望继续?”
| 归档时间: |
|
| 查看次数: |
18606 次 |
| 最近记录: |