0

我在 asp.net C# 中创建了一个 word 邮件合并。我需要用数据表中的数据替换 MergeField。问题是,只有第一页正确显示,其他页面被添加,但没有我想要的数据。我想迭代到数据表行的末尾,为每一行生成一个 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;
}

我最初所做的是,我在每次迭代中都调用 oWord.Visible = true 。它打开了很多word文档,虽然正确,但对我来说一点也不愉快。我希望通过向 oWord 添加新页面将所有这些放在一个文档中。然而,这些新页面不包含我需要替换的合并字段。已经四处寻找,但目前没有结果。我希望继续使用 Microsoft.Office.Interop

4

0 回答 0