邮件合并成单词 [英] Mail Merge into word

查看:149
本文介绍了邮件合并成单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The Best way to create labels is using existing industry standard tools , such as Microsoft word.

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

How do you execute this and set up the shipping labels ?

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

I am unsure how to map merge fields against data grid view columns. This is the code I have so far:

// 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");

推荐答案

首先,您应该学习如何制作Label模板文档. 例如,通过遵循此视频教程: www.youtube.com/watch?v=tIg70utT72Q

First, you should learn how to make Label template document. For example, by following this video tutorial: www.youtube.com/watch?v=tIg70utT72Q

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

Before saving the template document, remove mail merge data source created in the mail merge process because you will be using .NET object as mail merge data source. To remove mail merge data source go to Mailings tab -> Start Mail Merge -> select Normal Word Document, like in the image:

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

Save the document to a file, for example "LabelTemplate.docx". When you press Alt + F9 you should see the field codes like in the following image:

现在,您可以使用 GemBox.Document 组件(该代码为您在问题中使用). 这是如何执行邮件合并的C#代码:

Now you are ready to perform programmatic mail merge with GemBox.Document component (which code you use in the question). Here is a C# code how to perform a mail merge:

// 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"文档应如下所示:

The resulting "Labels.docx" document should look like this:

促销标头是自动添加的,因为该组件在试用"模式下使用. GemBox.Document邮件合并非常灵活,它支持分层邮件合并,通过处理

Promotional header is added automatically because the component is used in the Trial mode. GemBox.Document mail merge is very flexible, it supports hierarchical mail merge, customizing each field merging by handling FieldMerging event or by specifying merge field format string.

这篇关于邮件合并成单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆