gpt4 book ai didi

c# - 邮件合并到word

转载 作者:行者123 更新时间:2023-11-30 12:31:19 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

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

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

现在您已准备好使用 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”文档应如下所示: Label mail merge result

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

关于c# - 邮件合并到word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com