我正在编写一个 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);
}
但是,当我尝试通过网络方法调用时出现异常:
System.Runtime.InteropServices.COMException: Word 发生问题。
在System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult,ExcepInfo&excepInfo,UInt32 argErr,字符串消息)
在CallSite.Target(闭包,CallSite,ComObject,对象)
在 System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite 站点,T0 arg0,T1 arg1)
在CallSite.Target(闭包,CallSite,对象,对象)
在 System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite 站点,T0 arg0,T1 arg1)
在 C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs 中的 DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath, String& resultFilePath):第 145 行
在 C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs 中的 DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath, String& resultFilePath):第 63 行
在 C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\DocDownload.asmx.cs 中的 DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName, String m_orgFileName):第 90 行
使用office另存为xps的代码在我的WPF项目中运行良好。如何在我的 ASP.NET 4.0 Web 服务中使用它?谢谢。
使用 Windows 服务自动化(例如 IIS 和 ASP.NET)是微软不支持 http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 !
根据您的需要,您可以使用一些库(免费或商业)来实现此目的:
-
OpenXML 2.0 http://msdn.microsoft.com/en-us/office/bb265236 from MS
-
Aspose.Words http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/index.html(商业的)
-
VeryDoc.DOCX2XPS http://www.verydoc.com/doc-to-any/docx-to-xps.html(商业的)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)