文檔半島外圍網上直營>>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交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線