2017-05-30 79 views
2

我試圖通過WPF項目控制Excel應用程序。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: 

我該如何解決這個問題?

+0

你說你使用WPF,但堆棧跟蹤顯示一個Windows窗體應用程序。什麼是正確的? – dymanoid

回答

1

當Excel窗口最大化時,設置寬度不起作用。

之前設置Width屬性應該設置xla.WindowState = xlNormal

+0

非常感謝..它正在工作.. – user2131048

+0

嗨.. ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application(); ws.Cells [1,1] .Font.Bold = true; 當編譯器遇到上述行時,它會消耗很多時間(最少20秒)來完成該行。 即使我試圖在Task.Factory.StartNew中運行這些行,但沒有用。 請幫我解決這個問題。 – user2131048

+0

20秒來自應用程序啓動。 'ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application();'表示新的Excel進程已啓動。這一次不能縮短。您可以優化後續操作,即不會在操作等之間關閉Excel。您可以執行哪些操作 - 即在加載程序期間啓動Excel以加速後續操作。 – smartobelix

相關問題