使用Office 2010的COM asp.net web服务 [英] asp.net web service using office 2010 COM
问题描述
我写一个Web服务,并想改变的.docx或.doc到.XPS。我使用的办公COM帮我保存为.XPS格式如下:
I am writing a web service and would like to change the .docx or .doc to .xps. I'm using office com to help me to save as .xps format as following:
[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方法调用:
However, I got exception when I try to call by the web method:
System.Runtime.InteropServices.COMException:Word中发生问题。
在System.Dynamic.ComRuntimeHelpers.CheckThrowException(的Int32 HRESULT,EXCEPINFO&安培; EXCEPINFO,UInt32的argErr,字符串消息)
在CallSite.Target(封闭,调用点,ComObject,对象)
在System.Dynamic.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRET](调用点现场,T0为arg0,ARG1 T1)
在CallSite.Target(封闭,调用点,对象,对象)
在System.Dynamic.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRET](调用点现场,T0为arg0,ARG1 T1)
在DocProcessService.OfficeToXps.ConvertFromWord(字符串sourceFilePath,字符串&安培; resultFilePath)在C:\\用户\\冰柱\\文档\\快图美WP7 \\ DocProcessService \\ DocProcessService \\ OfficeHelper \\ OfficeToXps.cs:145线
在DocProcessService.OfficeToXps.ConvertToXps(字符串sourceFilePath,字符串&安培; resultFilePath)在C:\\用户\\冰柱\\文档\\快图美WP7 \\ DocProcessService \\ DocProcessService \\ OfficeHelper \\ OfficeToXps.cs:行63
在DocProcessService.DocDownload.GetDoc previewUrl(字符串m_userName,字符串m_orgFileName)在C:\\用户\\冰柱\\文档\\快图美WP7 \\ DocProcessService \\ DocProcessService \\ DocDownload.asmx.cs:行90
System.Runtime.InteropServices.COMException: Word 發生問題。 at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message) at CallSite.Target(Closure , CallSite , ComObject , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 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 at DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath, String& resultFilePath) in C:\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:line 90
使用办公室保存为XPS在我的WPF项目运作良好的code。我怎样才能让使用它在我的asp.net 4.0 Web服务?谢谢你。
The code of using office to save as xps was working well in my WPF project. How can I make use it in my asp.net 4.0 web service? Thanks.
推荐答案
从Windows服务(IIS和ASP.NET为例)使用自动化是的不再被微软支持
Using Automation from Windows Service (IIS and ASP.NET for example) is NOT supported by MS !
根据你需要什么,你可以用一些库(免费或商业)本:
Depending on what you need you can use some library (free or commercial) for this:
- OpenXML与MS 2.0
- Aspose.Words (商业)
- VeryDoc.DOCX2XPS (商业)
- OpenXML 2.0 from MS
- Aspose.Words (commercial)
- VeryDoc.DOCX2XPS (commercial)
这篇关于使用Office 2010的COM asp.net web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!