文档半岛外围网上直营>>E-iceblue中文文档>>创建邮件合并和合并文本值
创建邮件合并和合并文本值
邮件合并是 Spire.Doc 提供的最重要的功能之一,使开发人员能够创建报告并合并数据。本文将演示如何在 Spire.Doc 的帮助下创建邮件合并模板,然后将文本值合并到 C# 中的模板。
以下是如何创建邮件合并并将数据合并到其中的详细信息。
第 1 步:创建一个新的 Word 文档并添加部分和段落。
Document doc = new Document(); Section sec = doc.AddSection(); Paragraph para = sec.AddParagraph();
第 2 步:在段落中添加两个邮件合并模板。
para.AppendText("Quantity: ");
para.AppendField("Quantity", FieldType.FieldMergeField);
para.AppendBreak( BreakType.LineBreak);
para.AppendText("Date: ");
para.AppendField("Date", FieldType.FieldMergeField);
第 3 步:通过字段名设置邮件合并模板的值。
string[] fieldName = { "Quantity", "Date"};
string[] fieldValue = { "1800", DateTime.Now .ToShortDateString()};
第 4 步:将指定的值合并到模板中。
doc.MailMerge.Execute(fieldName, fieldValue);
第 5 步:将文档保存到文件中。
doc.SaveToFile("result.docx", FileFormat.Docx);
邮件合并的有效截图:
完整代码:
using Spire.Doc;
using Spire.Doc.Documents;
using System;
namespace MailMerge
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();
para.AppendText("Quantity: ");
para.AppendField("Quantity", FieldType.FieldMergeField);
para.AppendBreak(BreakType.LineBreak);
para.AppendText("Date: ");
para.AppendField("Date", FieldType.FieldMergeField);
string[] fieldName = { "Quantity", "Date" };
string[] fieldValue = { "1800", DateTime.Now.ToShortDateString() };
doc.MailMerge.Execute(fieldName, fieldValue);
doc.SaveToFile("result.docx", FileFormat.Docx);
}
}
欢迎下载|体验更多E-iceblue产品

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线