【问题标题】:How to generate word document in windows server 2003?windows server 2003如何生成word文档?
【发布时间】:2012-06-18 08:14:10
【问题描述】:

我编写了一个函数来使用 Office Interop Word 对象生成 Word 文档。我的代码如下所示。它在我的本地系统中运行良好。但是当我尝试从window server 2003 运行它时它不起作用。它一直在 Word.Document wordDoc = wordApp.Documents.Open 处加载并且不执行任何操作。你能帮帮我吗?

private void GenerateEmploymentCertificate()
{
    object Nothing = System.Reflection.Missing.Value;
    object format = Word.WdSaveFormat.wdFormatDocument;
    Word.Application wordApp = new Word.ApplicationClass();

    object srcFileName = Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Templates\Employment Certificate.doc"));
    Word.Document wordDoc = wordApp.Documents.Open
   (ref srcFileName, ref format, ref Nothing, ref Nothing,
   ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
   ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
   ref Nothing, ref Nothing);
    try
    {

        object bookmarkDate = "Date";
        wordDoc.Bookmarks.get_Item(ref bookmarkDate).Select();
        wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblRequestdate.Text);

        object bookmarkEmployeeName = "EmployeeName";
        wordDoc.Bookmarks.get_Item(ref bookmarkEmployeeName).Select();
        wordApp.Selection.Text = lblEmployeeName1.Text;

        object bookmarkCompany = "Company";
        wordDoc.Bookmarks.get_Item(ref bookmarkCompany).Select();
        wordApp.Selection.Text = lblCompanyName1.Text; 

        object bookmarkJoiningDate = "JoiningDate";
        wordDoc.Bookmarks.get_Item(ref bookmarkJoiningDate).Select();
        wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblJoiningDate1.Text); 

        object bookmarkDesignation = "Designation";
        wordDoc.Bookmarks.get_Item(ref bookmarkDesignation).Select();
        wordApp.Selection.Text = lblDesignation1.Text;

        string DocName;
        DocName = string.Format("{0}_employment_certificate", lblRequestNo.Text);
        hFilename.Value = DocName;
        wordDoc.SaveAs(Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Letters\" + DocName + ".doc")));

    }
    catch (Exception exp)
    {
        Session["generalError"] = null;
        Session["generalError"] = "There was an error at generating the letter. Please send email to unify.admin@unilever.com with this screen shot.<br /><br /><br />Request No:" + lblRequestNo.Text + "<br />Action:Submit<br />" + exp.StackTrace.ToString();
        LogManager logHelper = new LogManager(Request.PhysicalApplicationPath.Trim(), "Leave System - Malaysia");
        logHelper.LogError("[btnSubmit_Click - ]" + exp.Message + ".StackTrace - " + exp.StackTrace.ToString());
        Response.Redirect(ResolveUrl("~/Error/ErrorHandler.aspx"));
    }
    finally
    {
        // Close wordDoc2
        wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        if (wordDoc != null)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
            wordDoc = null;
        }
        // close wordApp
        wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        if (wordApp != null)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
            wordApp = null;
        }
    }
    GC.Collect();}

【问题讨论】:

标签: ms-word office-interop


【解决方案1】:

MS office 在服务器环境中并不能真正工作。 Even MS says so.

我们已使其与 LibreOffice 一起使用。你可以在我们的blog阅读它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多