Word处理控件Aspose.Words功能演示:使用 C# 或 VB.NET 在 Word 文档中合并邮件 - .NET 邮件合并 API

Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(qun:761297826)icon-default.png?t=N176https://www.evget.com/product/4116/download

在本文中,我将向您展示如何在没有 MS Word 或 Office Interop 的情况下使用 C# 或 VB.NET 执行 MS Word 邮件合并。Aspose.Words for .NET是一个功能丰富且功能强大的 Word API,它提供了所有基本的以及扩展的 MS Word 邮件合并功能。它允许您在 Windows 窗体、ASP.NET Web 应用程序或任何 .NET/.NET Core 应用程序中生成信件、信封、报告、发票和其他类型的文档。

一、什么是邮件合并?

邮件合并是一种自动生成报告、信件、信封、发票和其他类型文档的方式。MS Word 中的邮件合并允许您创建包含合并字段的模板文档,然后使用数据源中的记录填充这些字段。要了解邮件合并,假设您必须向十个不同的人发送一封信,并且只有姓名和地址字段需要更新。在这种情况下,只需创建一个字母模板,然后通过使用数据源填充名称和地址合并字段来动态生成字母。

二、邮件合并的数据源

邮件合并的数据可以从任何数据源(如 XML、JSON 或数据库)中获取。就 Aspose.Words for .NET 而言,您可以使用 ADO.NET 支持的任何数据源。数据可以加载到 DataSet、DataTable、DataView 或值数组中。

三、为邮件合并准备模板

邮件合并模板是包含合并字段的文档。当执行邮件合并时,这些字段随后会填充数据源中的数据。模板文档不需要是模板格式,可以是DOC/DOCX文档。这就是您可以为邮件合并准备模板的方法。

  • 打开您的文档或在 MS Word 中创建一个新文档。
  • 将光标置于要添加合并字段的位置。
  • 插入菜单中选择字段选项。
  • 字段名称列表中,选择MergeField
  • 在字段名称中输入合并字段的名称,然后按确定
  • 保存文档。

以下是示例模板文档的屏幕截图。

四、.NET 邮件合并 API - 安装

五、使用 C# 在 Word 文档中执行邮件合并

准备好模板后,您可以执行邮件合并以生成文档。以下是在上述模板上执行邮件合并的步骤。

  • 使用Document类加载模板文档。
  • 设置所需的邮件合并选项,例如Document.MailMerge.TrimWhitespaces。
  • 使用Document.MailMerge.Execute()方法执行邮件合并并将数据源作为参数传递。
  • 使用Document.Save(String)方法保存生成的文档。

以下代码示例显示如何使用 C# 中的值数组执行 MS Word 邮件合并。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_MailMergeAndReporting();
// Open an existing document.
Document doc = new Document(dataDir + "MailMerge.ExecuteArray.doc");

// Trim trailing and leading whitespaces mail merge values
doc.MailMerge.TrimWhitespaces = false;

// Fill the fields in the document with user data.
doc.MailMerge.Execute(
new string[] { "FullName", "Company", "Address", "Address2", "City" },
new object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });

dataDir = dataDir + "MailMerge.ExecuteArray_out.doc";
// Send the document in Word format to the client browser with an option to save to disk or open inside the current browser.

五、邮件合并后的 Word 文档

六、邮件合并后的 Word 文档

XML 文件广泛用于保存和导入/导出数据。Aspose.Words for .NET 也支持 XML 作为邮件合并的数据源。只需将 XML 读入DataSet对象并执行邮件合并。以下是我们将要使用的示例 XML 文件。

<customers>
<customer Name="John Ben Jan" ID="1" Domain="History" City="Boston"/>
<customer Name="Lisa Lane" ID="2" Domain="Chemistry" City="LA"/>
<customer Name="Dagomir Zits" ID="3" Domain="Heraldry" City="Milwaukee"/>
<customer Name="Sara Careira Santy" ID="4" Domain="IT" City="Miami"/>
</customers>

以下代码示例从 XML 数据源获取数据并使用 C# 执行邮件合并。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_MailMergeAndReporting();

// Create the Dataset and read the XML.
DataSet customersDs = new DataSet();
customersDs.ReadXml(dataDir + "Customers.xml");

string fileName = "TestFile XML.doc";
// Open a template document.
Document doc = new Document(dataDir + fileName);

// Execute mail merge to fill the template with data from XML using DataTable.
doc.MailMerge.Execute(customersDs.Tables["Customer"]);

dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the output document.
doc.Save(dataDir);

下面是将用 XML 数据填充的邮件合并模板。

以下是执行邮件合并后得到的结果 Word 文档的第 1 页。

七、合并字段的自定义格式

Aspose.Words for .NET 让您在执行期间更好地控制邮件合并。MailMerge.FieldMergingCallback属性允许您在遇到任何合并字段时自定义邮件合并。MailMerge.FieldMergingCallback 接受实现IFieldMergingCallback.FieldMerging和IFieldMergingCallback.ImageFieldMerging方法的类。

下面的代码示例显示了如何自定义邮件合并操作并将格式应用于此模板中的单元格。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_MailMergeAndReporting();
Document doc = new Document(dataDir + "MailMerge.AlternatingRows.doc");

// Add a handler for the MergeField event.
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldAlternatingRows();

// Execute mail merge with regions.
DataTable dataTable = GetSuppliersDataTable();
doc.MailMerge.ExecuteWithRegions(dataTable);
dataDir = dataDir + "MailMerge.AlternatingRows_out.doc";
doc.Save(dataDir);

以下是HandleMergeFieldAlternatingRows类的实现。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
private class HandleMergeFieldAlternatingRows : IFieldMergingCallback
{
/// <summary>
/// Called for every merge field encountered in the document.
/// We can either return some data to the mail merge engine or do something
/// Else with the document. In this case we modify cell formatting.
/// </summary>
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
if (mBuilder == null)
mBuilder = new DocumentBuilder(e.Document);

// This way we catch the beginning of a new row.
if (e.FieldName.Equals("CompanyName"))
{
// Select the color depending on whether the row number is even or odd.
Color rowColor;
if (IsOdd(mRowIdx))
rowColor = Color.FromArgb(213, 227, 235);
else
rowColor = Color.FromArgb(242, 242, 242);

// There is no way to set cell properties for the whole row at the moment,
// So we have to iterate over all cells in the row.
for (int colIdx = 0; colIdx < 4; colIdx++)
{
mBuilder.MoveToCell(0, mRowIdx, colIdx, 0);
mBuilder.CellFormat.Shading.BackgroundPatternColor = rowColor;
}

mRowIdx++;
}
}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing.
}

private DocumentBuilder mBuilder;
private int mRowIdx;
}
/// <summary>
/// Returns true if the value is odd; false if the value is even.
/// </summary>
private static bool IsOdd(int value)
{
// The code is a bit complex, but otherwise automatic conversion to VB does not work.
return ((value / 2) * 2).Equals(value);
}
/// <summary>
/// Create DataTable and fill it with data.
/// In real life this DataTable should be filled from a database.
/// </summary>
private static DataTable GetSuppliersDataTable()
{
DataTable dataTable = new DataTable("Suppliers");
dataTable.Columns.Add("CompanyName");
dataTable.Columns.Add("ContactName");
for (int i = 0; i < 10; i++)
{
DataRow datarow = dataTable.NewRow();
dataTable.Rows.Add(datarow);
datarow[0] = "Company " + i.ToString();
datarow[1] = "Contact " + i.ToString();
}
return dataTable;
}

八、使用 C# 与区域合并邮件

当您需要填充和重复 Word 文档中的特定区域时,可能会出现这种情况。对于这种情况,您可以使用区域邮件合并。要创建区域,您需要指定区域的开始和结束,然后 Mail Megre 将为数据源中的每条记录重复该区域。例如,以下模板包含两个区域,Orders 和 OrderDetails,分别具有合并字段 «TableStart:Orders»、«TableEnd:Orders» 和 «TableStart:OrderDetails»、«TableEnd:OrderDetails»。

以下是上述模板对区域进行Mail Megre的代码示例。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_MailMergeAndReporting();
string fileName = "MailMerge.ExecuteWithRegions.doc";
Document doc = new Document(dataDir + fileName);

// Use DataTable as a data source.
int orderId = 10444;
DataTable orderTable = GetTestOrder(orderId);
doc.MailMerge.ExecuteWithRegions(orderTable);

// Instead of using DataTable, you can create a DataView for custom sort or filter and then mail merge.
DataView orderDetailsView = new DataView(GetTestOrderDetails(orderId));
orderDetailsView.Sort = "ExtendedPrice DESC";

// Execute the mail merge operation.
doc.MailMerge.ExecuteWithRegions(orderDetailsView);

// Save the merged document.
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
doc.Save(dataDir);

下面是从数据库中读取数据的方法。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
private static DataTable GetTestOrder(int orderId)
{
DataTable table = ExecuteDataTable(string.Format(
"SELECT * FROM AsposeWordOrders WHERE OrderId = {0}", orderId));
table.TableName = "Orders";
return table;
}
private static DataTable GetTestOrderDetails(int orderId)
{
DataTable table = ExecuteDataTable(string.Format(
"SELECT * FROM AsposeWordOrderDetails WHERE OrderId = {0} ORDER BY ProductID", orderId));
table.TableName = "OrderDetails";
return table;
}
/// <summary>
/// Utility function that creates a connection, command,
/// Executes the command and return the result in a DataTable.
/// </summary>
private static DataTable ExecuteDataTable(string commandText)
{
// Open the database connection.
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
RunExamples.GetDataDir_Database() + "Northwind.mdb";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();

// Create and execute a command.
OleDbCommand cmd = new OleDbCommand(commandText, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);

// Close the database.
conn.Close();

return table;
}

九、嵌套邮件合并区域

大多数情况下,我们在数据源中拥有的数据以关系的形式出现。例如,表“Order”将与“OrderDetails”具有一对多关系,后者将保存订单中的项目记录。为了处理这种父子关系,使用了嵌套的邮件合并。以下是非常适合这种情况的示例发票模板。

以下是我们将用于嵌套邮件合并的示例 XML 数据源。

<?xml version="1.0" encoding="utf-8"?>
<Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="OrdersSchema.xsd">
<Order>
<Number>23</Number>
<Address>Nelson Street</Address>
<Suburb>Howick</Suburb>
<City>Auckland</City>
<Phonenumber>543 1234</Phonenumber>
<Date>03/01/2010</Date>
<Total>14.00</Total>
<Item>
<Name>BBQ Chicken Pizza</Name>
<Price>6.00</Price>
<Quantity>1</Quantity>
<ItemTotal>6.00</ItemTotal>
</Item>
<Item>
<Name>1.5 Litre Coke</Name>
<Price>4.00</Price>
<Quantity>2</Quantity>
<ItemTotal>8.00</ItemTotal>
</Item>
</Order>
<Order>
<Number>10</Number>
<Address>Parkville Avenue</Address>
<Suburb>Pakuranga</Suburb>
<City>Auckland</City>
<Phonenumber>548 7342</Phonenumber>
<Date>05/03/2010</Date>
<Total>6.00</Total>
<Item>
<Name>Hawaiian Pizza</Name>
<Price>4.00</Price>
<Quantity>1</Quantity>
<ItemTotal>4.00</ItemTotal>
</Item>
<Item>
<Name>Fries</Name>
<Price>1.00</Price>
<Quantity>2</Quantity>
<ItemTotal>2.00</ItemTotal>
</Item>
</Order>
</Orders>

而此 XML 的OrderSchema.xsd是:

<?xml version="1.0" encoding ="utf-8"?>
<xs:schema id="OrdersSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Orders">
<xs:complexType>
<xs:sequence>
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="Number"/>
<xs:element name="Address"/>
<xs:element name="Suburb"/>
<xs:element name="City"/>
<xs:element name="Phonenumber">
<xs:element name="Date"/>
<xs:element name="Total"/>
<xs:element name="Item">
<xs:complexType>
<xs:sequence>
<xs:element name="Name"/>
<xs:element name="Price"/>
<xs:element name="Quantity"/>
<xs:element name="ItemTotal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

以下代码示例用于使用 C# 执行嵌套邮件合并。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_MailMergeAndReporting();

// Create the Dataset and read the XML.
DataSet pizzaDs = new DataSet();

// The Datatable.TableNames and the DataSet.Relations are defined implicitly by .NET through ReadXml.
pizzaDs.ReadXml(dataDir + "CustomerData.xml");
string fileName = "Invoice Template.doc";

// Open the template document.
Document doc = new Document(dataDir + fileName);

// Trim trailing and leading whitespaces mail merge values.
doc.MailMerge.TrimWhitespaces = false;

// Execute the nested mail merge with regions.
doc.MailMerge.ExecuteWithRegions(pizzaDs);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);

// Save the output to file.
doc.Save(dataDir);

Debug.Assert(doc.MailMerge.GetFieldNames().Length == 0, "There was a problem with mail merge");
Console.WriteLine("\nMail merge performed with nested data successfully.\nFile saved at " + dataDir);


十、邮件合并后的 Word 文档

下面是执行邮件合并后生成的 Word 文档的第一页。

以上便是使用 C# 或 VB.NET 在 Word 文档中合并邮件 - .NET 邮件合并 API ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值