asp.net web service using office 2010 COM

烈酒焚心 提交于 2019-12-28 03:11:20

问题


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);
    }

However, I got exception when I try to call by the web method:

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

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.


回答1:


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 2.0 from MS
  • Aspose.Words (commercial)
  • VeryDoc.DOCX2XPS (commercial)


来源:https://stackoverflow.com/questions/7382704/asp-net-web-service-using-office-2010-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!