翻譯|使用教程|編輯:胡濤|2022-04-21 13:52:15.567|閱讀 233 次
概述:本文將演示如何在 Spire.Doc 的幫助下創(chuàng)建郵件合并模板,然后將文本值合并到 C# 中的模板。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
郵件合并是 Spire.Doc 提供的最重要的功能之一,使開發(fā)人員能夠創(chuàng)建報告并合并數(shù)據(jù)。本文將演示如何在 Spire.Doc 的幫助下創(chuàng)建郵件合并模板,然后將文本值合并到 C# 中的模板。
以下是如何創(chuàng)建郵件合并并將數(shù)據(jù)合并到其中的詳細信息。
第 1 步:創(chuàng)建一個新的 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產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn