【问题标题】:WORD Mail Merge with DataTable in ASP.Net C# with Page break带有分页符的 ASP.Net C# 中的 WORD 邮件与 DataTable 合并
【发布时间】:2019-01-06 04:40:58
【问题描述】:

我正在使用 word 模板创建一个 word 文件,并从数据表中转储数据以导出多页文件,我尝试了许多 dll,但获得了版权,我想要一些免费的东西,我需要你的想法和示例 我在 Microsoft.Office.Interop.Word 库中有以下代码,但以下页面为空白

    private void MailMerge(DataTable dt )
{
    Object oMissing = System.Reflection.Missing.Value;
    Object oTrue = true;
    Object oFalse = false;
    string filePath = Path.Combine(HttpContext.Current.Server.MapPath(@"~/ExportWord/MauSo04.dot"));
    Object oTemplatePath = filePath;

    Application wordApp = new Application();
    Document wordDoc = new Document();

    wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

    wordApp.Visible = true;
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        string Name= dt.Rows[i]["Name"].ToString();
        string Address= dt.Rows[i]["Address"].ToString();
        foreach (Field myMergeField in wordDoc.Fields)
        {
            int iTotalFields = 0;
            iTotalFields++;

            Range rngFieldCode = myMergeField.Code;

            String fieldText = rngFieldCode.Text;

            if (fieldText.StartsWith(" MERGEFIELD"))
            {
                Int32 endMerge = fieldText.IndexOf("\\");

                Int32 fieldNameLength = fieldText.Length - endMerge;

                String fieldName = fieldText.Substring(11, endMerge - 11);                  

                fieldName = fieldName.Trim();


                if (fieldName.Contains("Name"))
                {
                    myMergeField.Select();
                    wordApp.Selection.TypeText(Name);
                }else if (fieldName.Contains("Address"))
                {
                    myMergeField.Select();
                    wordApp.Selection.TypeText(Address);
                }
            }
        }
        wordDoc.Words.Last.InsertBreak(WdBreakType.wdPageBreak);
    }
    var path = Path.Combine(HttpContext.Current.Server.MapPath(@"~/ExportWord/Test.doc"));
    wordDoc.SaveAs(path);
    wordDoc.Close();
    wordApp.Application.Quit();
    Marshal.FinalReleaseComObject(wordApp);
    Marshal.FinalReleaseComObject(wordDoc);
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
}

【问题讨论】:

    标签: c# sql dll datatable


    【解决方案1】:

    我给你录了一个微信,希望对你有帮助

    Sub Macro()
        ActiveDocument.MailMerge.OpenDataSource Name:= _
            "C:ذDocuments\mail_m_example.xlsx", ConfirmConversions:= _
            False, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
            PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
            WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
            Connection:= _
            "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:ذDocuments\mail_m_example.xlsx;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37;Jet OLEDB:Database " _
            , SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="", SubType:= _
            wdMergeSubTypeAccess
        ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
            , Text:="""email2"""
        ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
            , Text:="""email"""
        ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
            , Text:="""email22"""
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=False
        End With
    End Sub
    

    【讨论】:

    • 我不明白
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多