使用 Office 2010 COM 的 asp.net Web 服务

发布于 2024-12-04 05:24:56 字数 2951 浏览 1 评论 0原文

我正在编写一个 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 服务中使用它?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-12-11 05:24:56

从 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文