翻译|使用教程|编辑:胡涛|2023-02-20 11:02:10.410|阅读 154 次
概述:在本文中,我将向您展示如何在没有 MS Word 或 Office Interop 的情况下使用 C# 或 VB.NET 执行 MS Word 邮件合并。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
在本文中,我将向您展示如何在没有 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文档。这就是您可以为邮件合并准备模板的方法。
以下是示例模板文档的屏幕截图。
准备好模板后,您可以执行邮件合并以生成文档。以下是在上述模板上执行邮件合并的步骤。
以下代码示例显示如何使用 C# 中的值数组执行 MS Word 邮件合并。
// For complete examples and data files, please go to //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.
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 //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 //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 //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; }
当您需要填充和重复 Word 文档中的特定区域时,可能会出现这种情况。对于这种情况,您可以使用区域邮件合并。要创建区域,您需要指定区域的开始和结束,然后 Mail Megre 将为数据源中的每条记录重复该区域。例如,以下模板包含两个区域,Orders 和 OrderDetails,分别具有合并字段 «TableStart:Orders»、«TableEnd:Orders» 和 «TableStart:OrderDetails»、«TableEnd:OrderDetails»。
以下是上述模板对区域进行Mail Megre的代码示例。
// For complete examples and data files, please go to //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 //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="//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="//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 //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 文档的第一页。
以上便是使用 C# 或 VB.NET 在 Word 文档中合并邮件 - .NET 邮件合并 API ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@capbkgr.cn