3

我正在编写一个 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);
    }

但是,当我尝试通过 web 方法调用时出现异常:

System.Runtime.InteropServices.COMException: Word 发生问题。在 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 站点,T0 arg0,T1 arg1)在 CallSite.Target(闭包,CallSite,对象,对象)在 System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,Tret]( CallSite 站点,T0 arg0,T1 arg1) 在 DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath, String& resultFilePath) 在 C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs: DocProcessService 的第 145 行。 C 中的 OfficeToXps.ConvertToXps(String sourceFilePath, String& resultFilePath):\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs:C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\ DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName, String m_orgFileName) 的第 63 行DocProcessService\DocDownload.asmx.cs:第 90 行

使用office另存为xps的代码在我的WPF项目中运行良好。如何在我的 asp.net 4.0 网络服务中使用它?谢谢。

4

1 回答 1

3

MS 不支持使用 Windows 服务中的自动化(例如 IIS 和 ASP.NET)!

根据您的需要,您可以为此使用一些库(免费或商业):

于 2011-09-12T06:26:05.030 回答