Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

c# - asp.net web service using office 2010 COM

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:UsersIcicleDocumentsFotomax WP7DocProcessServiceDocProcessServiceOfficeHelperOfficeToXps.cs:line 145 at DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath, String& resultFilePath) in C:UsersIcicleDocumentsFotomax WP7DocProcessServiceDocProcessServiceOfficeHelperOfficeToXps.cs:line 63 at DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName, String m_orgFileName) in C:UsersIcicleDocumentsFotomax WP7DocProcessServiceDocProcessServiceDocDownload.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...