在我的項目中,我需要在服務器端自動執行Excel和Word以供客戶端使用。我在示例控制檯應用程序中運行我的代碼,並且都運行良好,但是在WCF服務中,出現了一些錯誤。在WCF服務中使用Office自動處理引發異常
我的代碼如下所示:
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(對象&
IconIndex,對象&鏈接,對象&佈局,對象& DisplayAsIcon,
對象&數據類型,對象& IconFileName,對象& IconLabel)
在OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow []
worksheetD ATA)
在SyncInvokeDisplyWorksheet(對象,對象[],對象[])
在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(對象
例如,對象[]輸入,對象[] &輸出)
在系統.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc &
RPC)
System.Runtime.InteropServices.COMEx ception了未處理由用戶
代碼
消息從HRESULT =異常:0x800A03EC
源=的Microsoft.Office.Interop.Excel
錯誤碼= -2146827284
堆棧跟蹤:
在Microsoft.Office.Interop.Excel.ApplicationClass .get_Worksheets()
在OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow []
worksheetData)在C:\用戶\ Mahdi7s \文件\的Visual Studio
2010 \項目\ OfficeApiPlugin \ OfficeApiPlugin \ UsingOfficeApiService.cs:線
在SyncInvokeDisplyWorksheet(對象,對象[],對象[])
在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(對象
例如,對象[]輸入,對象[] &輸出)
在System.ServiceModel .Dispatcher.DispatchOperationRuntime。InvokeBegin(MessageRpc &
RPC)
我怎麼能做到這一點沒有這樣的錯誤?
你如何託管你的WCF服務? – Dyppl
錯誤似乎表明Word和Excel應用程序未安裝在您的服務器上。順便說一句:我總是試圖避免在服務器上使用COM互操作 - 嘗試使用其他方法(如OpenXML接口)在服務器上創建Word或Excel文檔,而無需安裝**在你的服務器上的辦公室.... –
@Dyppl:我在一個wpf應用程序中託管我的服務。 – VirtualWorld