在asp.net c#中合并Word邮件以添加多个页面并显示

在asp.net c#中合并Word邮件以添加多个页面并显示,c#,asp.net,ms-word,mailmerge,C#,Asp.net,Ms Word,Mailmerge,我在asp.net C#中创建了word邮件合并。我需要用数据表中的数据替换MergeField。问题是,只有第一个页面显示正确,其他页面被添加,但没有我想要的数据。 我想迭代到datatable行的末尾,为每一行生成一个word文档,并将所有这些文档放在一个word文件中,然后打开该文件。 我已经附上了全部代码 using Word = Microsoft.Office.Interop.Word; public void GenerateDoc(DataTable dt) { str

我在asp.net C#中创建了word邮件合并。我需要用数据表中的数据替换MergeField。问题是,只有第一个页面显示正确,其他页面被添加,但没有我想要的数据。 我想迭代到datatable行的末尾,为每一行生成一个word文档,并将所有这些文档放在一个word文件中,然后打开该文件。 我已经附上了全部代码

using Word = Microsoft.Office.Interop.Word;

public void GenerateDoc(DataTable dt)
{
    string schoolName = "Name 1";
    string schoolAddress = "Address 1";
    //OBJECT OF MISSING "NULL VALUE"
    Object oMissing = System.Reflection.Missing.Value;
    //OBJECTS OF FALSE AND TRUE
    Object oTrue = true;
    Object oFalse = false;

    //CREATING OBJECTS OF WORD AND DOCUMENT
    Word.Application oWord = new Word.Application();
    Word.Document oWordDoc = new Word.Document();

    //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE
    string filePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"App_Data\envelope3.dotx");
    Object oTemplatePath = filePath;

    //ADDING A NEW DOCUMENT FROM A TEMPLATE
    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        string studentName = dt.Rows[i]["StudentName"].ToString();
        string studentID = dt.Rows[i]["StudentID"].ToString();
        string guard = dt.Rows[i]["GuardName"].ToString();
        string phone = dt.Rows[i]["Phone"].ToString();
        string address = dt.Rows[i]["Address"].ToString();
        string className = dt.Rows[i]["ClassName"].ToString();
        foreach (Word.Field myMergeField in oWordDoc.Fields)
        {
            int iTotalFields = 0;
            iTotalFields++;
            Word.Range rngFieldCode = myMergeField.Code;
            String fieldText = rngFieldCode.Text;

            // ONLY GETTING THE MAILMERGE FIELDS

            if (fieldText.StartsWith(" MERGEFIELD"))
            {

                // THE TEXT COMES IN THE FORMAT OF 

                // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                //Int32 endMerge = fieldText.IndexOf("\\");
                //Int32 fieldNameLength = fieldText.Length - endMerge;

                String fieldName = fieldText;//.Substring(11, endMerge - 11);
                // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dotx FILE

                fieldName = fieldName.Trim();
                // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//
                // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE
                if (fieldName.Contains("SchoolName"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(schoolName);
                }
                else if (fieldName.Contains("SchoolAddr"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(schoolAddress);
                }
                else if (fieldName.Contains("StudentName"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(studentName);
                }
                else if (fieldName.Contains("StudentID"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(studentID);
                }
                else if (fieldName.Contains("GuardName"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(guard);
                }
                else if (fieldName.Contains("Phone"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(phone);
                }
                else if (fieldName.Contains("Address"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(address);
                }
                else if (fieldName.Contains("ClassName"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(className);
                }
            }
        }
        //Add to oWord to the final Word Document here
        Word.Selection sel = oWord.Selection;
        sel.InsertNewPage();
    }
    //SETTING THE VISIBILITY TO TRUE
    oWord.Visible = true;
}
使用Word=Microsoft.Office.Interop.Word;
生成的公共无效数据(数据表dt)
{
字符串schoolName=“Name 1”;
字符串schoolAddress=“地址1”;
//缺少“空值”的对象
对象omising=System.Reflection.Missing.Value;
//虚实之物
Object oTrue=true;
ALSE的对象=false;
//创建WORD和文档的对象
Word.Application oWord=新单词.Application();
Word.Document oWordDoc=新的Word.Document();
//模板文件在计算机上的位置
字符串filePath=Path.Combine(HostingEnvironment.ApplicationPhysicalPath,@“App_Data\envelope3.dotx”);
对象oTemplatePath=文件路径;
//从模板添加新文档
oWordDoc=oWord.Documents.Add(ref-oTemplatePath,ref-omising,ref-omising,ref-omising);
对于(int i=0;i
我最初做的是在每次迭代中调用oWord.Visible=true。它打开了很多word文档,虽然正确,但对我来说一点也不愉快。我希望通过向oWord添加新页面,将所有这些内容合并到一个文档中。但是,这些新页面不包含需要替换的合并字段。
已经到处搜索过了,但到目前为止没有结果。我希望继续使用Microsoft.Office.Interop

您所做的可能是个坏主意。服务器不支持使用Office ActiveX Automation,您将遇到许多问题。所以,即使你解决了今天的问题,你将来也会遇到更糟糕的问题。如果您可以通过开发工作站上的winforms驱动程序执行相同的自动化,那么这可能只是意味着您已经看到了服务器问题。我没有注意到你的陈述:当使word可见时,它起作用了(也许是你添加的)。这实际上保证了您的问题是由于在服务器上使用互操作的问题造成的。你无缘无故地把头撞在石头上。如果您的文档很简单,那么您可以相对轻松地通过代码编写与Word兼容的.rtf或.xml文件,否则请查看一些可用的库。有些是免费的,有些是大钱。你所做的将导致脱发。好的,我将尝试OpenXml格式,看看哪个有效,我将用它更新这个问题。你和哪些人共事过