Interop excel设置宽度抛出异常C# [英] Interop excel setting width throwing exception C#

查看:154
本文介绍了Interop excel设置宽度抛出异常C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过WPF项目控制excel应用程序。 

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

xla.Width = 400; //这一行抛出了以下异常。

System.Runtime.InteropServices.COMException未处理
HResult = -2146827284
消息=来自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)在d:\ Test Projects \EmbedExcel1 \EmbedExcel1 \Form1.cs:第27行
在System.Windows.Forms.Control.OnClick(EventArgs e)
在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) System.Windows.Fornd.Button上的System.Windows.Forms.Control.WndProc(消息& m)
在System.Windows.Forms.ButtonBase.WndProc上的
(消息& m)
.WndProc(消息& m)
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在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原因,Int32 pvLoopData)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context )
在System.Windows.Forms.Application.Run(Form mainForm)
at EmbedExcel1.Program.Main()in d:\ Test Projects \EmbedExcel1 \EmbedExcel1 \Program.cs: System.AppDomain._nExecuteAssembly第19行
(RuntimeAssembly a ssembly,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项目控制excel应用程序。 

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

xla.Width = 400;

解决方案

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


Application.Width是一个ReadOnly属性,在文档中有明确说明:_Application.Width属性(Microsoft.Office.Interop.Excel) [ ^ ]。< /击>

I am trying to control the excel application via WPF project.

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: 
thanks in advance.



What I have tried:

I am trying to control the excel application via WPF project.

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

xla.Width = 400;

解决方案

Before setting Width property you should set xla.WindowState = xlNormal


Application.Width is a ReadOnly property as clearly stated in the documentation: _Application.Width property (Microsoft.Office.Interop.Excel)[^].


这篇关于Interop excel设置宽度抛出异常C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆