|
1 | | -using Syncfusion.DocIO; |
| 1 | +using Syncfusion.DocIO; |
2 | 2 | using Syncfusion.DocIO.DLS; |
3 | | -using System.Collections.Generic; |
| 3 | +using System.Data; |
4 | 4 | using System.IO; |
5 | 5 |
|
6 | | -namespace Execute_Mail_Merge_in_columnwise |
| 6 | +namespace Generate_Documents_for_each_record |
7 | 7 | { |
8 | 8 | class Program |
9 | 9 | { |
10 | 10 | static void Main(string[] args) |
11 | 11 | { |
12 | | - |
13 | | - using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) |
| 12 | + //Open the file as Stream. |
| 13 | + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open)) |
14 | 14 | { |
15 | | - //Loads an existing Word document into DocIO instance. |
16 | | - using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic)) |
| 15 | + //Get the data for mail merge. |
| 16 | + DataTable table = GetDataTable(); |
| 17 | + //Iterate to the each row and generate mail merged document for each rows. |
| 18 | + for (int i = 0; i < table.Rows.Count; i++) |
17 | 19 | { |
18 | | - //Gets the customer details as “IEnumerable” collection |
19 | | - List<CustomerDetail> customerDetails = GetCustomerDetails(); |
20 | | - //Creates an instance of “MailMergeDataTable” by specifying mail merge group name and “IEnumerable” collection. |
21 | | - MailMergeDataTable dataTable = new MailMergeDataTable("CustomerDetails", customerDetails); |
22 | | - //Removes the empty paragraph, if the field not have value. |
23 | | - document.MailMerge.RemoveEmptyParagraphs = true; |
24 | | - //Performs Mail merge |
25 | | - document.MailMerge.ExecuteGroup(dataTable); |
26 | | - |
27 | | - //Creates file stream. |
28 | | - using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 20 | + //Load file stream into Word document. |
| 21 | + using (WordDocument document = new WordDocument(fileStream, FormatType.Docx)) |
29 | 22 | { |
30 | | - //Saves the Word document to file stream. |
31 | | - document.Save(outputStream, FormatType.Docx); |
| 23 | + //Executes mail merge using the data row. |
| 24 | + document.MailMerge.Execute(table.Rows[i]); |
| 25 | + |
| 26 | + //Create a file stream. |
| 27 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + "Record_" + (i + 1) + ".docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 28 | + { |
| 29 | + //Save the Word document to the file stream. |
| 30 | + document.Save(outputFileStream, FormatType.Docx); |
| 31 | + } |
32 | 32 | } |
33 | 33 | } |
34 | 34 | } |
35 | 35 | } |
36 | | - |
37 | 36 | /// <summary> |
38 | | - /// Create a data to execute mail merge in Word document. |
| 37 | + /// Get the data for mail merge. |
39 | 38 | /// </summary> |
40 | 39 | /// <returns></returns> |
41 | | - public static List<CustomerDetail> GetCustomerDetails() |
| 40 | + static DataTable GetDataTable() |
42 | 41 | { |
43 | | - List<CustomerDetail> customers = new List<CustomerDetail>(); |
44 | | - customers.Add(new CustomerDetail("Sathish Kumar", "F3, Bharath PST Castle,", "14, Renga Nagar, Srirangam", "Trichy,", "Tamilnadu", string.Empty)); |
45 | | - customers.Add(new CustomerDetail("Swathi", "No12, DN Aparments", "15, Thillai ganga nagar", "Chennai,", "Tamilnadu", string.Empty)); |
46 | | - customers.Add(new CustomerDetail("Brent", "#12, London Road", string.Empty, string.Empty, "Oxford,", "United Kingdom")); |
47 | | - customers.Add(new CustomerDetail("Mani", "#12, Steve Lane,", string.Empty, string.Empty, "Dublin,", "Ireland")); |
48 | | - return customers; |
49 | | - } |
50 | | - } |
51 | | - /// <summary> |
52 | | - /// Represents a class to maintain customer details. |
53 | | - /// </summary> |
54 | | - public class CustomerDetail |
55 | | - { |
56 | | - public string Name { get; set; } |
57 | | - public string Address1 { get; set; } |
58 | | - public string Address2 { get; set; } |
59 | | - public string City { get; set; } |
60 | | - public string Region { get; set; } |
61 | | - public string Country { get; set; } |
| 42 | + DataTable table = new DataTable(); |
62 | 43 |
|
63 | | - public CustomerDetail(string name, string address1, string address2, string city, string region, string country) |
64 | | - { |
65 | | - Name = name; |
66 | | - Address1 = address1; |
67 | | - Address2 = address2; |
68 | | - City = city; |
69 | | - Region = region; |
70 | | - Country = country; |
| 44 | + //Defining columns |
| 45 | + table.Columns.Add("Name"); |
| 46 | + table.Columns.Add("Street"); |
| 47 | + table.Columns.Add("City"); |
| 48 | + table.Columns.Add("ProjectNo"); |
| 49 | + |
| 50 | + //Set values |
| 51 | + DataRow row; |
| 52 | + row = table.NewRow(); |
| 53 | + row["Name"] = "Andreas Waning"; |
| 54 | + row["Street"] = "Middelwegg 2"; |
| 55 | + row["City"] = "Vreden"; |
| 56 | + row["ProjectNo"] = "4711"; |
| 57 | + table.Rows.Add(row); |
| 58 | + |
| 59 | + row = table.NewRow(); |
| 60 | + row["Name"] = "Mike Korf"; |
| 61 | + row["Street"] = "teststreet"; |
| 62 | + row["City"] = "TestCity"; |
| 63 | + row["ProjectNo"] = "4711"; |
| 64 | + table.Rows.Add(row); |
| 65 | + |
| 66 | + return table; |
71 | 67 | } |
72 | 68 | } |
73 | 69 | } |
0 commit comments