创建标签的最佳方法是使用现有的行业标准工具,例如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");
答案 0 :(得分:2)
首先,您应该学习如何制作Label模板文档。 例如,通过以下视频教程: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来自定义每个字段合并。