翻譯|使用教程|編輯:吉煒煒|2025-02-14 14:12:04.767|閱讀 105 次
概述:在本文中,您將學(xué)習(xí)如何使用 Spire.Doc for .NET 在 Word 文檔中執(zhí)行郵件合并。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在本文中,您將學(xué)習(xí)如何使用 Spire.Doc for .NET 在 Word 文檔中執(zhí)行郵件合并。
首先,您需要將 Spire.Doc for.NET 包含的 DLL 文件作為引用添加到您的 .NET 項目中。DLL 文件可以從此鏈接下載,也可以通過 安裝。
PM> Install-Package Spire.Doc
想要生成帶有合并域的模板 Word 文檔,建議使用 Microsoft Word 等 Word 編輯器。Word 編輯器的視覺界面使您能夠自由地設(shè)計模板的獨特布局、格式和其他元素。
下圖展示了如何使用 MS Word 向 Word 文檔中添加郵件合并域。如果您想將圖像合并到合并域中,請記得使用“Image:FieldName”格式。
如果您想要使用 C# 創(chuàng)建一個模板文檔,請參照以下步驟:
using Spire.Doc; using Spire.Doc.Documents; namespace CreateTemplate { class Program { static void Main(string[] args) { // 創(chuàng)建一個 Document 對象 Document document = new Document(); // 添加一個節(jié) Section section = document.AddSection(); // 添加一個段落 Paragraph paragraph = section.AddParagraph(); // 在段落中添加文本和郵件合并域 paragraph.AppendText("姓名:"); paragraph.AppendField("Name", FieldType.FieldMergeField); paragraph.AppendBreak(BreakType.LineBreak); paragraph.AppendText("郵件地址:"); paragraph.AppendField("Email", FieldType.FieldMergeField); paragraph.AppendBreak(BreakType.LineBreak); paragraph.AppendText("個人頭像:"); paragraph.AppendField("Image:Avatar", FieldType.FieldMergeField); // 保存結(jié)果文檔 document.SaveToFile("/模板.docx", FileFormat.Docx2019); // 釋放資源 document.Dispose(); } } }
Spire.Doc 提供了 MailMerge.Execute() 方法,用于在 Word 文檔中執(zhí)行指定的郵件合并操作。該方法有 6 個重載版本,允許用戶從不同的數(shù)據(jù)源(如 DataTable、DataView 和字符串數(shù)組)執(zhí)行郵件合并。
使用數(shù)組提供的數(shù)據(jù)執(zhí)行郵件合并的步驟如下:
using Spire.Doc; using Spire.Doc.Reporting; using System.Drawing; namespace MailMergeInDocument { class Program { static void Main(string[] args) { // 創(chuàng)建一個 Document 對象 Document document = new Document(); // 加載 Word 模板 document.LoadFromFile("/模板.docx"); // 指定合并域的名稱 string[] fieldNames = { "Name", "Email", "Avatar" }; // 指定用來填充合并域的值 string[] fieldValues = { "謝雨", "xieyu @e-iceblue.com", "C:/Users/Administrator/Desktop/avatar.png" }; // 注冊合并圖片合并域時觸發(fā)的事件 document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField); // 將數(shù)據(jù)郵件合并到文檔中 document.MailMerge.Execute(fieldNames, fieldValues); // 保存結(jié)果文檔 document.SaveToFile("/郵件合并.docx", FileFormat.Docx2019); // 釋放資源 document.Dispose(); } // 使用圖片填充圖像合并域 private static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field) { string filePath = field.FieldValue as string; if (!string.IsNullOrEmpty(filePath)) { field.Image = Image.FromFile(filePath); } } } }
這里的區(qū)域指的是文檔中的特定區(qū)域,您可以在區(qū)域插入數(shù)據(jù)源中的數(shù)據(jù)。郵件合并會針對數(shù)據(jù)源中的每條記錄重復(fù)該區(qū)域。Spire.Doc 提供了 MailMerge.ExecuteWithRegion() 方法來執(zhí)行區(qū)域內(nèi)的郵件合并。
使用 DataTable 提供的數(shù)據(jù)執(zhí)行帶區(qū)域的郵件合并的步驟如下:
using Spire.Doc; using System.Data; namespace MailMergeWithGroup { class Program { static void Main(string[] args) { // 創(chuàng)建一個 Document 對象 Document document = new Document(); // 加載模板 Word 文件 document.LoadFromFile("/郵件模板.docx"); // 創(chuàng)建一個 數(shù)據(jù)表,并指定表名 DataTable table = new DataTable("OrderDetails"); // 添加示例數(shù)據(jù) table.Columns.Add("ProductName"); table.Columns.Add("Quantity"); table.Columns.Add("UnitPrice"); table.Columns.Add("TotalPrice"); table.Rows.Add("無線鼠標", "2", "39.99", "79.98"); table.Rows.Add("機械鍵盤", "1", "89.99", "89.99"); table.Rows.Add("電競耳機", "3", "59.99", "179.97"); table.Rows.Add("顯示器支架", "1", "49.99", "49.99"); // 在區(qū)域內(nèi)執(zhí)行郵件合并 document.MailMerge.ExecuteWidthRegion(table); // 保存修改后的文檔 document.SaveToFile("/區(qū)域郵件合并.docx", FileFormat.Docx2019); // 釋放資源 document.Dispose(); } } }
在執(zhí)行嵌套分組的郵件合并時,系統(tǒng)會將嵌套區(qū)域內(nèi)的合并域替換為按照層次結(jié)構(gòu)組織的數(shù)據(jù)。嵌套區(qū)域可以創(chuàng)建更復(fù)雜的布局,其中一個區(qū)域的內(nèi)容將依賴于另一個區(qū)域中的數(shù)據(jù)。
使用 XML 文件中的數(shù)據(jù)執(zhí)行嵌套區(qū)域郵件合并的步驟如下:
using Spire.Doc;
using System.Collections;
using System.Data;
namespace MailMergeWithNestedRegions
{
class Program
{
static void Main(string[] args)
{
// 創(chuàng)建一個 Document 對象
Document document = new Document();
// 加載 Word 模板文檔
document.LoadFromFile("/模板.docx");
// 從 XML 文件讀取數(shù)據(jù)到 DataSet 對象
DataSet dataSet = new DataSet();
dataSet.ReadXml("/Orders.xml");
// 創(chuàng)建一個 List 對象,用以存儲合并域信息
List list = new List();
// 創(chuàng)建兩個 DictionaryEntry 對象,并將它們添加到列表中(每個對象指定合并域名稱和相關(guān)的表達式)
DictionaryEntry dictionaryEntry = new DictionaryEntry("Customer", string.Empty);
list.Add(dictionaryEntry);
dictionaryEntry = new DictionaryEntry("Order", "Customer_Id = %Customer.Customer_Id%");
list.Add(dictionaryEntry);
// 執(zhí)行嵌套區(qū)域的郵件合并
document.MailMerge.ExecuteWidthNestedRegion(dataSet, list);
// 保存結(jié)果文件
document.SaveToFile("嵌套區(qū)域的郵件合并.docx", FileFormat.Docx2019);
// 釋放資源
document.Dispose();
}
}
}
慧都是E-iceblue的官方授權(quán)代理商,提供Spire系列產(chǎn)品免費試用,咨詢,正版銷售等于一體的專業(yè)化服務(wù)。
下載|體驗更多E-iceblue產(chǎn)品,請咨詢,或撥打產(chǎn)品熱線:023-68661681
加入E-iceblue技術(shù)交流QQ群(767755948),與更多小伙伴一起探討提升開發(fā)技能。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)