在 WCF 服务中使用 Office 自动化会引发异常
在我的项目中,我需要自动化 Excel 和 Word 在服务器端供客户端使用。我在示例控制台应用程序中运行了我的代码,一切正常,但在 WCF 服务内部,我遇到了一些错误。
我的代码如下所示:
var wordApp = new Word.Application();
wordApp.Visible = true;
wordApp.Documents.Add();
wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true); //Throws exception
var _excelApp = new Excel.Application();
_excelApp.Visible = true;
_excelApp.Worksheets.Add(); //Throws exception
错误是:
System.Runtime.InteropServices.COMException 未由
处理 用户代码
HelpLink=wdmain11.chm#24822
消息=指定的数据类型不可用。
来源=Microsoft Word
错误代码=-2146822946
堆栈跟踪:
在 Microsoft.Office.Interop.Word.Selection.PasteSpecial(Object&
IconIndex、对象&链接、对象和放置、对象&显示为图标,
对象&数据类型、对象& IconFileName、对象&图标标签)
在 OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow[]
工作表数据)
在 SyncInvokeDisplyWorksheet(对象, 对象[], 对象[])
在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象
实例,对象[]输入,对象[]&输出)
在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
RPC)
System.Runtime.InteropServices.COMException 未被用户处理
代码
消息=来自 HRESULT 的异常:0x800A03EC
来源=Microsoft.Office.Interop.Excel
错误代码=-2146827284
堆栈跟踪:
在 Microsoft.Office.Interop.Excel.ApplicationClass.get_Worksheets()
在 OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow[]
worksheetData)位于 C:\Users\Mahdi7s\Documents\Visual Studio
2010\Projects\OfficeApiPlugin\OfficeApiPlugin\UsingOfficeApiService.cs:line
29
在 SyncInvokeDisplyWorksheet(对象, 对象[], 对象[])
在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象
实例,对象[]输入,对象[]&输出)
在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
RPC)
我怎样才能做到这一点而不出现此错误?
In my project, I need to automate Excel and Word at the server side for use by clients. I ran my code in a sample console application and all works well, but inside of the WCF service, I got some errors.
My code looks like this:
var wordApp = new Word.Application();
wordApp.Visible = true;
wordApp.Documents.Add();
wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true); //Throws exception
var _excelApp = new Excel.Application();
_excelApp.Visible = true;
_excelApp.Worksheets.Add(); //Throws exception
And the errors are:
System.Runtime.InteropServices.COMException was unhandled by
user code
HelpLink=wdmain11.chm#24822
Message=The specified data type is unavailable.
Source=Microsoft Word
ErrorCode=-2146822946
StackTrace:
at Microsoft.Office.Interop.Word.Selection.PasteSpecial(Object&
IconIndex, Object& Link, Object& Placement, Object& DisplayAsIcon,
Object& DataType, Object& IconFileName, Object& IconLabel)
at OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow[]
worksheetData)
at SyncInvokeDisplyWorksheet(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object
instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
rpc)
System.Runtime.InteropServices.COMException was unhandled by user
code
Message=Exception from HRESULT: 0x800A03EC
Source=Microsoft.Office.Interop.Excel
ErrorCode=-2146827284
StackTrace:
at Microsoft.Office.Interop.Excel.ApplicationClass.get_Worksheets()
at OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow[]
worksheetData) in C:\Users\Mahdi7s\Documents\Visual Studio
2010\Projects\OfficeApiPlugin\OfficeApiPlugin\UsingOfficeApiService.cs:line
29
at SyncInvokeDisplyWorksheet(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object
instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
rpc)
How can I do this without this errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于剪贴板(这是正在爆炸的操作)需要使用 STA COM 线程。 WCF 调用在线程池线程(MTA 线程)上执行。
使用 Thread 类启动您自己的线程,并将其 ApartmentState 设置为 ApartmentState.STA,然后从那里执行您的 Office 自动化。最简单的事情就是阻塞 WCF 线程,直到自动化完成。
但是,需要注意以下几点:
I think the problem is that the clipboard (which is the operation that is blowing up) requires the use of an STA COM thread. WCF calls are performed on threadpool threads which are MTA threads.
Spin up your own thread using the Thread class and set its ApartmentState to ApartmentState.STA and then perform your Office automation from there. The simplest thing then would be to block your WCF thread until the automation is complete.
A couple of things to be aware of, however: