【问题标题】:Mail Merge into word邮件合并成word
【发布时间】:2017-05-06 04:03:34
【问题描述】:

创建标签的最佳方式是使用现有的行业标准工具,例如 Microsoft word。

您如何执行此操作并设置运输标签?

我不确定如何将合并字段映射到数据网格视图列。这是我到目前为止的代码:

// Create a new empty document.
DocumentModel document = new DocumentModel();

// Add document content.
document.Sections.Add(
    new Section(document,
        new Paragraph(document,
            new Field(document, FieldType.MergeField, "FullName"))));

// Save the document to a file and open it with Microsoft Word.
document.Save("TemplateDocument.docx");
// If document appears empty in Microsoft Word, press Alt + F9.
Process.Start("TemplateDocument.docx");

// Initialize mail merge data source.
var dataSource = new { FullName = "John Doe" };

// Execute mail merge.
document.MailMerge.Execute(dataSource);

// Save the document to a file and open it with Microsoft Word.
document.Save("Document.docx");
Process.Start("Document.docx");

【问题讨论】:

  • 你的问题非常不清楚。
  • 如果您以编程方式生成文档,则根本不需要使用邮件合并。

标签: c# ms-word mailmerge


【解决方案1】:

首先,您应该学习如何制作标签模板文档。 例如,按照这个视频教程:www.youtube.com/watch?v=tIg70utT72Q

在保存模板文档之前,删除在邮件合并过程中创建的邮件合并数据源,因为您将使用 .NET 对象作为邮件合并数据源。 要删除邮件合并数据源,请转到邮件选项卡 -> 开始邮件合并 -> 选择普通 Word 文档,如图所示:

将文档保存到文件中,例如“LabelTemplate.docx”。 当您按 Alt + F9 时,您应该会看到如下图所示的域代码:

现在您已准备好使用GemBox.Document 组件(您在问题中使用的代码)执行程序化邮件合并。 以下是如何执行邮件合并的 C# 代码:

// Set licensing info.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
ComponentInfo.FreeLimitReached += (sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;

// Create mail merge data source. 
// You should use DataGridView.DataSource property - it will return DataView or DataTable that is data-bound to your DataGridView.
var dataTable = new DataTable()
{
    Columns =
    {
        new DataColumn("Name"),
        new DataColumn("Surname"),
        new DataColumn("Company")
    },
    Rows =
    {
        { "John", "Doe", "ACME" },
        { "Fred", "Nurk", "ACME" },
        { "Hans", "Meier", "ACME" }
    }
};

var document = DocumentModel.Load("LabelTemplate.docx", LoadOptions.DocxDefault);

// Use this if field names and data column names differ. If they are the same (case-insensitive), then you don't need to define mappings explicitly.
document.MailMerge.FieldMappings.Add("First_Name", "Name");
document.MailMerge.FieldMappings.Add("Last_Name", "Surname");
document.MailMerge.FieldMappings.Add("Company_Name", "Company");

// Execute mail merge. Each mail merge field will be replaced with the data from the data source's appropriate row and column.
document.MailMerge.Execute(dataTable);

// Remove any left mail merge field.
document.MailMerge.RemoveMergeFields();

// Save resulting document to a file.
document.Save("Labels.docx");

生成的“Labels.docx”文档应如下所示:

由于该组件在试用模式下使用,因此会自动添加促销标题。 GemBox.Document mail merge非常灵活,它支持hierarchical mail merge,通过处理FieldMerging event或者指定merge field format string来自定义每个字段的合并。

【讨论】:

  • 非常感谢您,它完全按照您的解释工作,而且非常有帮助。你们摇滚!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多