ประเภทของการดำเนินการ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โดยใช้Execute methodsที่แตกต่างกันที่ยอมรับวัตถุข้อมูลต่างๆเป็น.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดำเนินการอย่างง่ายMail Mergeโดยใช้วิธีการอย่างใดอย่างหนึ่งExecute:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
// 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 ");
// Fill the fields in the document with user data.
doc.MailMerge.Execute(new string[] { "CustomerName", "Item", "Quantity" },
new object[] { "John Doe", "Hawaiian", "2" });
doc.Save(ArtifactsDir + "BaseOperations.SimpleMailMerge.docx");

คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะดำเนินการอย่างง่ายmail merge:

simple_mail_merge_template

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

execute_simple_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-.NET.git.
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DatabaseDir + "Northwind.accdb";
Document doc = new Document(MyDir + "Mail merge destination - Northwind suppliers.docx");
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Customers", conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable data = new DataTable();
da.Fill(data);
// Perform a loop through each DataRow to iterate through the DataTable. Clone the template document
// instead of loading it from disk for better speed performance before the mail merge operation.
// You can load the template document from a file or stream but it is faster to load the document
// only once and then clone it in memory before each mail merge operation.
int counter = 1;
foreach (DataRow row in data.Rows)
{
Document dstDoc = (Document) doc.Clone(true);
dstDoc.MailMerge.Execute(row);
dstDoc.Save(string.Format(ArtifactsDir + "BaseOperations.ProduceMultipleDocuments_{0}.docx", counter++));
}

Mail Mergeกับภูมิภาค

คุณสามารถสร้างภูมิภาคต่างๆในเทมเพลตของคุณเพื่อให้มีพื้นที่พิเศษที่คุณสามารถกรอกข้อมู ใช้Mail Mergeกับพื้นที่หากคุณต้องการแทรกตารางแถวที่มีข้อมูลซ้ำเพื่อให้เอกสารของคุณเติบโตได้อย่.

คุณสามารถสร้างพื้นที่ที่ซ้อนกัน(เด็ก)รวมทั้งผสานพื้นที่ ข้อได้เปรียบหลักของการใช้ประเภทนี้คือการเพิ่มชิ้นส่วนภายในเอกสารแบบไดนามิก ดูรายละเอียดเพิ่มเติมในบทความถัดไป"ซ้อนMail Mergeกับภูมิภาค".

วิธีการประมวลผลMail Mergeกับภูมิภาค

ภูมิภาคMail Mergeเป็นส่วนเฉพาะภายในเอกสารที่มีจุดเริ่มต้นและจุดสิ้นสุด จุดทั้งสองจุดจะแสดงเป็นMail Mergeฟิลด์ที่มีชื่อเฉพาะ*“TableStart:XXX”*และ*“TableEnd:XXX”* เนื้อหาทั้งหมดที่รวมอยู่ในภูมิภาคMail Mergeจะถูกทำซ้ำโดยอัตโนมัติสำหรับทุกระเบียนในแหล่งข้อมูล.

Aspose.Wordsอนุญาตให้คุณประมวลผลMail Mergeกับพื้นที่โดยใช้พื้นที่ต่างๆExecute methodsที่ยอมรับวัตถุข้อมูลต่างๆเป็นแหล่งข้อมูล.

ในฐานะที่เป็นขั้นตอนแรก,เราจำเป็นต้องสร้างDataSetที่จะผ่านมันในภายหลังเป็นพารามิเตอร์การป้อนข้อมูลไปยังวิธีการExecuteWithRegions:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
private DataSet CreateDataSet()
{
// Create the customers table.
DataTable tableCustomers = new DataTable("Customers");
tableCustomers.Columns.Add("CustomerID");
tableCustomers.Columns.Add("CustomerName");
tableCustomers.Rows.Add(new object[] { 1, "John Doe" });
tableCustomers.Rows.Add(new object[] { 2, "Jane Doe" });
// Create the orders table.
DataTable tableOrders = new DataTable("Orders");
tableOrders.Columns.Add("CustomerID");
tableOrders.Columns.Add("ItemName");
tableOrders.Columns.Add("Quantity");
tableOrders.Rows.Add(new object[] { 1, "Hawaiian", 2 });
tableOrders.Rows.Add(new object[] { 2, "Pepperoni", 1 });
tableOrders.Rows.Add(new object[] { 2, "Chicago", 1 });
// Add both tables to a data set.
DataSet dataSet = new DataSet();
dataSet.Tables.Add(tableCustomers);
dataSet.Tables.Add(tableOrders);
// The "CustomerID" column, also the primary key of the customers table is the foreign key for the Orders table.
dataSet.Relations.Add(tableCustomers.Columns["CustomerID"], tableOrders.Columns["CustomerID"]);
return dataSet;
}

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการรันMail Mergeกับภูมิภาคโดยใช้วิธีการExecuteWithRegions(DataSet):

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
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.MailMerge.ExecuteWithRegions(customersAndOrders);
doc.Save(ArtifactsDir + "BaseOperations.MailMergeWithRegions.docx");

คุณสามารถสังเกตเห็นความแตกต่างระหว่างเอกสารก่อนที่จะประมวลผลMail Mergeกับภูมิภาค:

mail_merge_with_regions_template

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

mail_merge_with_regions_execute

ข้อจำกัดของMail Mergeกับภูมิภาค

มีประเด็นสำคัญบางอย่างที่คุณต้องพิจารณาเมื่อดำเนินการMail Mergeกับภูมิภาค:

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

ถ้าหนึ่งในกฎเหล่านี้เสียคุณจะได้รับผลลัพธ์ที่ไม่คาดคิดหรือข้อยกเว้นอาจจะโยน.