【问题标题】:Excel application thows an exception on setting widthExcel 应用程序在设置宽度时引发异常
【发布时间】:2017-05-30 04:46:38
【问题描述】:

我正在尝试通过 WPF 项目控制 Excel 应用程序。

var   xla = new Microsoft.Office.Interop.Excel.Application();

xla.Width = 400; // This line is throwing the below exception.

它可以正常工作几次,几天后同一行给出如下所示的异常。注销并登录到系统暂时修复问题。

System.Runtime.InteropServices.COMException was unhandled
  HResult=-2146827284
  Message=Exception from HRESULT: 0x800A03EC
  Source=Microsoft.Office.Interop.Excel
  ErrorCode=-2146827284
  StackTrace:
       at Microsoft.Office.Interop.Excel.ApplicationClass.set_Width(Double RHS)
       at EmbedExcel1.Form1.button1_Click(Object sender, EventArgs e) in d:\Test Projects\EmbedExcel1\EmbedExcel1\Form1.cs:line 27
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at EmbedExcel1.Program.Main() in d:\Test Projects\EmbedExcel1\EmbedExcel1\Program.cs:line 19
       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: 

我该如何解决这个问题?

【问题讨论】:

  • 您说您使用的是 WPF,但堆栈跟踪显示了一个 Windows 窗体应用程序。什么是正确的?

标签: c# excel excel-interop comexception


【解决方案1】:

Excel 窗口最大化时设置宽度不起作用。

在设置 Width 属性之前,你应该设置 xla.WindowState = xlNormal

【讨论】:

  • 嗨.. ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application(); ws.Cells[1, 1].Font.Bold = true;当编译器遇到上述行时,它会花费大量时间(最少 20 秒)来完成该行。即使我尝试在Task.Factory.StartNew中运行这些行,但没有用..请帮助我。
  • 20 秒来自应用程序启动。 ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application(); 表示新的 Excel 进程已启动。这个时间不能缩短。您可以优化以后的操作,即在操作之间不关闭 Excel 等。您可以做什么 - 即在加载程序期间启动 Excel 以加快以后的操作。
  • 嗨..感谢您的回复..实际上,系统并没有花时间来创建对象,而是将字体粗体设置为 true。 ws.Cells[1, 1].Font.Bold = true; // 这一行..
猜你喜欢
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
相关资源
最近更新 更多