Skip to content

Commit 6714170

Browse files
ES-264002-Execute-Mail-Merge-in-columnwise
1 parent 102d5f1 commit 6714170

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Execute-Mail-Merge-in-columnwise", "Execute-Mail-Merge-in-columnwise\Execute-Mail-Merge-in-columnwise.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Execute_Mail_Merge_in_columnwise</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace Execute_Mail_Merge_in_columnwise
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
13+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
14+
{
15+
//Loads an existing Word document into DocIO instance.
16+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
17+
{
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))
29+
{
30+
//Saves the Word document to file stream.
31+
document.Save(outputStream, FormatType.Docx);
32+
}
33+
}
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Create a data to execute mail merge in Word document.
39+
/// </summary>
40+
/// <returns></returns>
41+
public static List<CustomerDetail> GetCustomerDetails()
42+
{
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; }
62+
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;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)