Skip to content

Commit dd32c9e

Browse files
Merge pull request #382 from SyncfusionExamples/ES-264021-Generate-Documents-for-each-record
ES-264021- Add the sample Generate-Documents-for-each-record
2 parents 9c8fa2d + e4705dc commit dd32c9e

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
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}") = "Generate-Documents-for-each-record", "Generate-Documents-for-each-record\Generate-Documents-for-each-record.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
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>Generate_Documents_for_each_record</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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Data;
4+
using System.IO;
5+
6+
namespace Generate_Documents_for_each_record
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Open the file as Stream.
13+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
14+
{
15+
//Load file stream into Word document.
16+
using (WordDocument template = new WordDocument(fileStream, FormatType.Docx))
17+
{
18+
//Get the data for mail merge.
19+
DataTable table = GetDataTable();
20+
//Iterate to the each row and generate mail merged document for each rows.
21+
for (int i = 0; i < table.Rows.Count; i++)
22+
{
23+
//Clones the template document for creating new document for each record in the data source
24+
WordDocument document = template.Clone();
25+
//Executes mail merge using the data row.
26+
document.MailMerge.Execute(table.Rows[i]);
27+
28+
//Create a file stream.
29+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + "Record_" + (i + 1) + ".docx"), FileMode.Create, FileAccess.ReadWrite))
30+
{
31+
//Save the Word document to the file stream.
32+
document.Save(outputFileStream, FormatType.Docx);
33+
}
34+
//Releases the resources occupied by WordDocument instance
35+
document.Dispose();
36+
}
37+
}
38+
}
39+
}
40+
/// <summary>
41+
/// Get the data for mail merge.
42+
/// </summary>
43+
/// <returns></returns>
44+
static DataTable GetDataTable()
45+
{
46+
DataTable table = new DataTable();
47+
48+
//Defining columns
49+
table.Columns.Add("Name");
50+
table.Columns.Add("Street");
51+
table.Columns.Add("City");
52+
table.Columns.Add("ProjectNo");
53+
54+
//Set values
55+
DataRow row;
56+
row = table.NewRow();
57+
row["Name"] = "Andreas Waning";
58+
row["Street"] = "Middelwegg 2";
59+
row["City"] = "Vreden";
60+
row["ProjectNo"] = "4711";
61+
table.Rows.Add(row);
62+
63+
row = table.NewRow();
64+
row["Name"] = "Mike Korf";
65+
row["Street"] = "teststreet";
66+
row["City"] = "TestCity";
67+
row["ProjectNo"] = "4711";
68+
table.Rows.Add(row);
69+
70+
return table;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)