2011-09-12 37 views
3

我正在編寫一個Web服務,並且想要將.docx或.doc更改爲.xps。我使用的辦公COM幫我保存爲.XPS格式如下:使用Office 2010的asp.net Web服務COM

 [WebMethod] 
    public string GetDocPreviewUrl(string m_userName, string m_orgFileName) 
    { 
     string m_returnUrl = ""; 

     string m_orgFilePath = _currentDirectory + "\\" + m_userName + "\\" + m_orgFileName; 
     if (File.Exists(m_orgFilePath)) 
     { 
      string m_xpsFilePath = _currentDirectory + "\\" + m_userName + "\\" + 
            Path.GetFileNameWithoutExtension(m_orgFileName) + ".xps"; 

      OfficeToXpsConversionResult m_converstionResult = OfficeToXps.ConvertToXps(m_orgFilePath, ref m_xpsFilePath); 

      m_returnUrl = _baseUrl + m_userName + "/"+ Path.GetFileName(m_xpsFilePath); 
     } 
     return m_returnUrl; 
    } 

     private static OfficeToXpsConversionResult ConvertFromWord(string sourceFilePath, ref string resultFilePath) 
    { 
     object pSourceDocPath = sourceFilePath; 

     string pExportFilePath = string.IsNullOrWhiteSpace(resultFilePath) ? GetTempXpsFilePath() : resultFilePath; 


     Word.Application() wordApplication = new Word.Application(); 

     //wordDocument = wordApplication.Documents.Open(ref pSourceDocPath); 
     dynamic wordDocument = wordApplication.Documents.Add(pSourceDocPath); 

       //return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc); 

     if (wordDocument != null) 
     { 
      wordDocument.SaveAs(pExportFilePath, WdSaveFormat.wdFormatXPS); 
     } 

     resultFilePath = pExportFilePath; 

     return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath); 
    } 

但是,我得到了異常,當我嘗試通過Web方法調用:

System.Runtime。 InteropServices.COMException:字發生問題 在System.Dynamic.ComRuntimeHelpers.CheckThrowException(的Int32 HRESULT,EXCEPINFO & EXCEPINFO,UInt32的argErr,字符串消息) 在CallSite.Target(封閉,調用點,ComObject,對象) 在System.Dynamic。 .UpdateDelegates.UpdateAndExecute2 [T0,T1,TRet](CallSite站點,T0 arg0,T 1(arg1) at CallSite.Target(Closure,CallSite,Object,Object) at System.Dynamic.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRet](CallSite site,T0 arg0,T1 arg1) at DocProcessService.OfficeToXps.ConvertFromWord (String sourceFilePath,String & resultFilePath)in C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:line 145 (String sourceFilePath,String & resultFilePath) :\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:line 63 at DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName,String m_orgFileName)in C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ DocDownload.asmx.cs:行90

使用office保存爲xps的代碼在我的WPF項目中運行良好。我如何在我的asp.net 4.0 web服務中使用它?謝謝。

回答