我正在编写一个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方法调用时,出现异常:
System.Runtime.InteropServices.COMException:Word发生问题。
在System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult,ExcepInfo&excepInfo,UInt32 argErr,String消息)
在CallSite.Target(Closure,CallSite,ComObject,Object)
在System.Dynamic.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRet](CallSite站点,T0 arg0,T1 arg1)
在CallSite.Target(Closure,CallSite,Object,Object)
在System.Dynamic.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRet](CallSite站点,T0 arg0,T1 arg1)
在DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath,String&resultFilePath)中位于C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:line 145
在DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath,String&resultFilePath)中的C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:line 63
在DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName,String m_orgFileName)在C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ DocDownload.asmx.cs:第90行
在WPF项目中,使用office保存为xps的代码运行良好。如何在asp.net 4.0 Web服务中使用它?谢谢。
最佳答案
从Windows Service(例如IIS和ASP.NET)使用自动化是NOT supported by MS!
根据您的需要,您可以为此使用一些库(免费或商业):
来自MS的OpenXML 2.0
Aspose.Words(商业)
VeryDoc.DOCX2XPS(商业)