Подтвердить что ты не робот

Веб-сервис asp.net с использованием Office 2010 COM

Я пишу веб-сервис и хочу изменить .docx или .doc на .xps. Я использую офис 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)    в 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)    в DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath, String & resultFilePath) в C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs: строка 145    в DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath, String & resultFilePath) в C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs: строка 63    в DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName, String m_orgFileName) в C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\DocDownload.asmx.cs: строка 90

Код использования офиса для сохранения в виде xps хорошо работал в моем проекте WPF. Как я могу использовать его в моем веб-сервисе asp.net 4.0? Спасибо.

4b9b3361

Ответ 1

Использование автоматизации из службы Windows (например, IIS и ASP.NET) НЕ поддерживается MS!

В зависимости от того, что вам нужно, вы можете использовать некоторую библиотеку (бесплатную или коммерческую) для этого: