diff --git a/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record.sln b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record.sln new file mode 100644 index 000000000..6ee52799b --- /dev/null +++ b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31911.196 +MinimumVisualStudioVersion = 10.0.40219.1 +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}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4} + EndGlobalSection +EndGlobal diff --git a/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Data/Template.docx b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Data/Template.docx new file mode 100644 index 000000000..abff558a2 Binary files /dev/null and b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Data/Template.docx differ diff --git a/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Generate-Documents-for-each-record.csproj b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Generate-Documents-for-each-record.csproj new file mode 100644 index 000000000..12a12b238 --- /dev/null +++ b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Generate-Documents-for-each-record.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Generate_Documents_for_each_record + + + + + + + + + Always + + + Always + + + + diff --git a/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Output/.gitkeep b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Program.cs b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Program.cs new file mode 100644 index 000000000..f04070bde --- /dev/null +++ b/Mail-Merge/Generate-Documents-for-each-record/.NET/Generate-Documents-for-each-record/Program.cs @@ -0,0 +1,73 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using System.Data; +using System.IO; + +namespace Generate_Documents_for_each_record +{ + class Program + { + static void Main(string[] args) + { + //Open the file as Stream. + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open)) + { + //Load file stream into Word document. + using (WordDocument template = new WordDocument(fileStream, FormatType.Docx)) + { + //Get the data for mail merge. + DataTable table = GetDataTable(); + //Iterate to the each row and generate mail merged document for each rows. + for (int i = 0; i < table.Rows.Count; i++) + { + //Clones the template document for creating new document for each record in the data source + WordDocument document = template.Clone(); + //Executes mail merge using the data row. + document.MailMerge.Execute(table.Rows[i]); + + //Create a file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + "Record_" + (i + 1) + ".docx"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the Word document to the file stream. + document.Save(outputFileStream, FormatType.Docx); + } + //Releases the resources occupied by WordDocument instance + document.Dispose(); + } + } + } + } + /// + /// Get the data for mail merge. + /// + /// + static DataTable GetDataTable() + { + DataTable table = new DataTable(); + + //Defining columns + table.Columns.Add("Name"); + table.Columns.Add("Street"); + table.Columns.Add("City"); + table.Columns.Add("ProjectNo"); + + //Set values + DataRow row; + row = table.NewRow(); + row["Name"] = "Andreas Waning"; + row["Street"] = "Middelwegg 2"; + row["City"] = "Vreden"; + row["ProjectNo"] = "4711"; + table.Rows.Add(row); + + row = table.NewRow(); + row["Name"] = "Mike Korf"; + row["Street"] = "teststreet"; + row["City"] = "TestCity"; + row["ProjectNo"] = "4711"; + table.Rows.Add(row); + + return table; + } + } +}