我正在编写一个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,String message) 在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) at DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath,String& resultFilePath)在C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:第145行 at DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath,String& resultFilePath)在C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ OfficeHelper \ OfficeToXps.cs:第63行 at DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName,String m_orgFileName)在C:\ Users \ Icicle \ Documents \ Fotomax WP7 \ DocProcessService \ DocProcessService \ DocDownload.asmx.cs:第90行
使用office保存为xps的代码在我的WPF项目中运行良好。如何在我的asp.net 4.0 Web服务中使用它?感谢。
答案 0 :(得分:3)
从Windows服务(例如IIS和ASP.NET)使用自动化是NOT supported by MS!
根据您的需要,您可以使用一些库(免费或商业):