ประเภทของการดำเนินการMail Merge
แนวคิดหลักของMail Mergeคือการสร้างเอกสารหรือเอกสารหลายฉบับโดยอัตโนมัติตามเทมเพลตแ Aspose.Wordsช่วยให้คุณสามารถดำเนินการสองประเภทที่แตกต่างกันของการดำเนินงานMail Merge:ง่ายMail MergeและMail Mergeกับ.
ตัวอย่างที่พบบ่อยที่สุดของการใช้ง่ายMail Mergeคือเมื่อคุณต้องการส่งเอกสารสำหรับลูกค้าที่แตกต่าง การทำเช่นนี้,คุณต้องสร้างฟิลด์ผสานเช่นFirst NameและLast Nameในเทมเพลตของคุณ,และจากนั้นกรอกข้อมูล ในขณะที่ตัวอย่างที่พบบ่อยที่สุดของการใช้Mail Mergeกับภูมิภาคคือเมื่อคุณต้องการที่จะส่งเอกสารที่มี นเทมเพลตของคุณเองสำหรับการสั่งซื้อแต่ละครั้งเพื่อเติมเต็มข้อมูลที่จำเป็นทั้งหมดสำหรั.
ความแตกต่างหลักระหว่างการดำเนินการผสานทั้งสองอย่างคือMail Merge(ไม่มีภูมิภาค)ทำซ้ำเอกสารทั้งหมดต่อแต่ละระเบียนแหล่งข้อมูลในขณะที่Mail Mergeกับภูมิภาคจะทำซ้ำเฉพาะภูมิภาคที่กำหนดต่อเร็กคอร์ดเท่านั้น คุณสามารถคิดถึงการดำเนินการอย่างง่ายMail Mergeเป็นกรณีเฉพาะของการรวมกับภูมิภาคที่มีภูมิภ.
ง่ายMail Mergeการดำเนินงาน
ง่ายMail Mergeใช้ในการกรอกข้อมูลMail Mergeinsideในเทมเพลตของคุณด้วยข้อมูลที่จำเป็นจากแหล่งข้อมูลขอ มันจึงคล้ายกับคลาสสิกMail MergeในMicrosoft Word.
คุณสามารถเพิ่มฟิลด์ผสานอย่างน้อยหนึ่งฟิลด์ในเทมเพลตของคุณและดำเนินการอย่างง่ายMail Merge ขอแนะนำให้ใช้หากเทมเพลทของคุณไม่มีพื้นที่ผสาน.
ข้อจำกัดหลักของการใช้ชนิดนี้เป็นเนื้อหาเอกสารทั้งหมดจะถูกทำซ้ำสำหรับแต่ละระเบียน.
วิธีการดำเนินการอย่างง่ายMail Mergeการดำเนินการ
เมื่อเทมเพลตของคุณพร้อมแล้วคุณสามารถเริ่มดำเนินการอย่างง่ายMail Merge Aspose.Wordsช่วยให้คุณสามารถดำเนินการอย่างง่ายMail Mergeโดยใช้วิธีการดำเนินการที่แตกต่างกันที่ยอมรับวัตถุข้อมูลต่างๆเป็น.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดำเนินการอย่างง่ายMail Mergeโดยใช้วิธีการอย่างใดอย่างหนึ่งExecute:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Include the code for our template. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Create Merge Fields. | |
builder.insertField(" MERGEFIELD CustomerName "); | |
builder.insertParagraph(); | |
builder.insertField(" MERGEFIELD Item "); | |
builder.insertParagraph(); | |
builder.insertField(" MERGEFIELD Quantity "); | |
builder.getDocument().save(dataDir + "MailMerge.TestTemplate.docx"); | |
// Fill the fields in the document with user data. | |
doc.getMailMerge().execute(new String[] { "CustomerName", "Item", "Quantity" }, | |
new Object[] { "John Doe", "Hawaiian", "2" }); | |
builder.getDocument().save(dataDir + "MailMerge.Simple.docx"); |
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะดำเนินการอย่างง่ายmail merge:

และหลังจากที่ดำเนินการอย่างง่ายmail merge:

วิธีการสร้างเอกสารที่ผสาน
ในAspose.WordsการดำเนินงานมาตรฐานMail Mergeจะเติมเพียงเอกสารเดียวที่มีเนื้อหาจากแหล่งข้อมูลของคุณ ดังนั้นคุณจะต้องดำเนินการดำเนินการMail Mergeหลายครั้งเพื่อสร้างเอกสารที่รวมหลายเป็นเอาต์พุ.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างเอกสารที่รวมหลายรายการระหว่างการดำเนินการMail Merge:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public class ProduceMultipleDocumentsDuringMailMerge { | |
private static final String dataDir = Utils.getSharedDataDir(ProduceMultipleDocumentsDuringMailMerge.class) + "MailMerge/"; | |
public static void main(String[] args) throws Exception { | |
produceMultipleDocuments(dataDir, "TestFile.doc"); | |
} | |
public static void produceMultipleDocuments(String dataDir, String srcDoc) throws Exception { | |
// Open the database connection. | |
ResultSet rs = getData(dataDir, "SELECT * FROM Customers"); | |
// Open the template document. | |
Document doc = new Document(dataDir + srcDoc); | |
// A record of how many documents that have been generated so far. | |
int counter = 1; | |
// Loop though all records in the data source. | |
while (rs.next()) { | |
// Clone the template instead of loading it from disk (for speed). | |
Document dstDoc = (Document) doc.deepClone(true); | |
// Extract the data from the current row of the ResultSet into a Hashtable. | |
Hashtable dataMap = getRowData(rs); | |
// Execute mail merge. | |
dstDoc.getMailMerge().execute(keySetToArray(dataMap), dataMap.values().toArray()); | |
// Save the document. | |
dstDoc.save(MessageFormat.format(dataDir + "TestFile Out {0}.doc", counter++)); | |
System.out.println("Counter : "); | |
} | |
} | |
/** | |
* Creates a Hashtable from the name and value of each column in the current | |
* row of the ResultSet. | |
*/ | |
public static Hashtable getRowData(ResultSet rs) throws Exception { | |
ResultSetMetaData metaData = rs.getMetaData(); | |
Hashtable values = new Hashtable(); | |
for (int i = 1; i <= metaData.getColumnCount(); i++) { | |
values.put(metaData.getColumnName(i), rs.getObject(i)); | |
} | |
return values; | |
} | |
/** | |
* Utility function that returns the keys of a Hashtable as an array of | |
* Strings. | |
*/ | |
public static String[] keySetToArray(Hashtable table) { | |
return (String[]) table.keySet().toArray(new String[table.size()]); | |
} | |
/** | |
* Utility function that creates a connection to the Database. | |
*/ | |
public static ResultSet getData(String dataDir, String query) throws Exception { | |
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); | |
String connectionString = "jdbc:ucanaccess://" + dataDir + "Customers.mdb"; | |
// DSN-less DB connection. | |
Connection connection = DriverManager.getConnection(connectionString); | |
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); | |
return statement.executeQuery(query); | |
} | |
} |
Mail Mergeกับภูมิภาค
คุณสามารถสร้างภูมิภาคต่างๆในเทมเพลตของคุณเพื่อให้มีพื้นที่พิเศษที่คุณสามารถกรอกข้อมู ใช้Mail Mergeกับพื้นที่หากคุณต้องการแทรกตารางแถวที่มีข้อมูลซ้ำเพื่อให้เอกสารของคุณเติบโตได้อย่.
คุณสามารถสร้างพื้นที่ที่ซ้อนกัน(เด็ก)รวมทั้งผสานพื้นที่ ข้อได้เปรียบหลักของการใช้ประเภทนี้คือการเพิ่มชิ้นส่วนภายในเอกสารแบบไดนามิก ดูรายละเอียดเพิ่มเติมในบทความถัดไป"ซ้อนMail Mergeกับภูมิภาค".
วิธีการประมวลผลMail Mergeกับภูมิภาค
ภูมิภาคMail Mergeเป็นส่วนเฉพาะภายในเอกสารที่มีจุดเริ่มต้นและจุดสิ้นสุด จุดทั้งสองจุดจะแสดงเป็นMail Mergeฟิลด์ที่มีชื่อเฉพาะ*“TableStart:XXX”*และ*“TableEnd:XXX”* เนื้อหาทั้งหมดที่รวมอยู่ในภูมิภาคMail Mergeจะถูกทำซ้ำโดยอัตโนมัติสำหรับทุกระเบียนในแหล่งข้อมูล.
Aspose.Wordsอนุญาตให้คุณประมวลผลMail Mergeกับพื้นที่โดยใช้พื้นที่ต่างๆวิธีการดำเนินการที่ยอมรับวัตถุข้อมูลต่างๆเป็นแหล่งข้อมูล.
ในฐานะที่เป็นขั้นตอนแรก,เราจำเป็นต้องสร้างDataSet
ที่จะผ่านมันในภายหลังเป็นพารามิเตอร์การป้อนข้อมูลไปยังวิธีการExecuteWithRegions
:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static DataSet CreateDataSet() { | |
// Create the customers table. | |
DataTable tableCustomers = new DataTable("Customers"); | |
tableCustomers.getColumns().add("CustomerID"); | |
tableCustomers.getColumns().add("CustomerName"); | |
tableCustomers.getRows().add(new Object[] { 1, "John Doe" }); | |
tableCustomers.getRows().add(new Object[] { 2, "Jane Doe" }); | |
// Create the orders table. | |
DataTable tableOrders = new DataTable("Orders"); | |
tableOrders.getColumns().add("CustomerID"); | |
tableOrders.getColumns().add("ItemName"); | |
tableOrders.getColumns().add("Quantity"); | |
tableOrders.getRows().add(new Object[] { 1, "Hawaiian", 2 }); | |
tableOrders.getRows().add(new Object[] { 2, "Pepperoni", 1 }); | |
tableOrders.getRows().add(new Object[] { 2, "Chicago", 1 }); | |
// Add both tables to a data set. | |
DataSet dataSet = new DataSet(); | |
dataSet.getTables().add(tableCustomers); | |
dataSet.getTables().add(tableOrders); | |
// The "CustomerID" column, also the primary key of the customers table is the | |
// foreign key for the Orders table. | |
dataSet.getRelations().add(tableCustomers.getColumns().get("CustomerID"), | |
tableOrders.getColumns().get("CustomerID")); | |
return dataSet; | |
} |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการรันMail Mergeกับภูมิภาคโดยใช้วิธีการExecuteWithRegions(DataSet):
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// The start point of mail merge with regions the dataset. | |
builder.insertField(" MERGEFIELD TableStart:Customers"); | |
// Data from rows of the "CustomerName" column of the "Customers" table will go | |
// in this MERGEFIELD. | |
builder.write("Orders for "); | |
builder.insertField(" MERGEFIELD CustomerName"); | |
builder.write(":"); | |
// Create column headers | |
builder.startTable(); | |
builder.insertCell(); | |
builder.write("Item"); | |
builder.insertCell(); | |
builder.write("Quantity"); | |
builder.endRow(); | |
// We have a second data table called "Orders", which has a many-to-one | |
// relationship with "Customers" | |
// picking up rows with the same CustomerID value. | |
builder.insertCell(); | |
builder.insertField(" MERGEFIELD TableStart:Orders"); | |
builder.insertField(" MERGEFIELD ItemName"); | |
builder.insertCell(); | |
builder.insertField(" MERGEFIELD Quantity"); | |
builder.insertField(" MERGEFIELD TableEnd:Orders"); | |
builder.endTable(); | |
// The end point of mail merge with regions. | |
builder.insertField(" MERGEFIELD TableEnd:Customers"); | |
// Pass our dataset to perform mail merge with regions. | |
DataSet customersAndOrders = CreateDataSet(); | |
doc.getMailMerge().executeWithRegions(customersAndOrders); | |
// Save the result | |
doc.save(dataDir + "MailMerge.ExecuteWithRegions.docx"); |
คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะประมวลผลMail Mergeกับภูมิภาค:

และหลังจากดำเนินการMail Mergeกับภูมิภาค:

ข้อจำกัดของMail Mergeกับภูมิภาค
มีประเด็นสำคัญบางอย่างที่คุณต้องพิจารณาเมื่อดำเนินการMail Mergeกับภูมิภาค:
- จุดเริ่มต้นTableStart:Ordersและจุดสิ้นสุดTableEnd:Ordersทั้งสองต้องอยู่ในแถวหรือเซลล์เดียวกัน ตัวอย่างเช่นถ้าคุณเริ่มผสานพื้นที่ในเซลล์ของตารางคุณต้องสิ้นสุดพื้นที่ผสานในแถวเดียวกั.
- ชื่อฟิลด์ผสานต้องตรงกับชื่อคอลัมน์ในDataTableของคุณ เว้นแต่คุณจะระบุฟิลด์ที่แม็พไว้Mail Mergeที่มีพื้นที่จะไม่ประสบความสำเร็จสำหรับฟิลด์ผสานที่มีชื่อที่แต.
ถ้าหนึ่งในกฎเหล่านี้เสียคุณจะได้รับผลลัพธ์ที่ไม่คาดคิดหรือข้อยกเว้นอาจจะโยน.