使用office 2010 COM的asp.net Web服务

我正在编写一个Web服务,并希望将.docx或.doc更改为.xps。 我正在使用office com来帮助我保存为.xps格式如下:

[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方法调用时出现exception:

System.Runtime.InteropServices.COMException:在System.Dynamic的CallSite.Target(Closure,CallSite,ComObject,Object)上的System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult,ExcepInfo&excepInfo,UInt32 argErr,String message)中的Word发生问题。 System.Dynamic.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRet]上的CallSite.Target(Closure,CallSite,Object,Object)中的.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRet](CallSite站点,T0 arg0,T1 arg1)( CallSite站点,T0 arg0,T1 arg1)位于DocProcessService的C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:第145行的DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath,String&resultFilePath)。 OfficeToXps.ConvertToXps(String sourceFilePath,String&resultFilePath)位于C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:第63行,位于DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName,String m_org FileName)在C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ DocDownload.asmx.cs:第90行

使用office保存为xps的代码在我的WPF项目中运行良好。 如何在我的asp.net 4.0 Web服务中使用它? 谢谢。

MS不支持使用Windows服务中的自动化(例如IIS和ASP.NET)!

根据您的需要,您可以使用一些库(免费或商业):

  • 来自MS的OpenXML 2.0
  • Aspose.Words (商业)
  • VeryDoc.DOCX2XPS (商业)