diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data.sln b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data.sln new file mode 100644 index 00000000..c0da118a --- /dev/null +++ b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.37614.0 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Perform-Mail-Merge-With-Excel-Data", "Perform-Mail-Merge-With-Excel-Data\Perform-Mail-Merge-With-Excel-Data.csproj", "{600588D9-4DC3-6DCF-4401-A0341863B874}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {600588D9-4DC3-6DCF-4401-A0341863B874}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {600588D9-4DC3-6DCF-4401-A0341863B874}.Debug|Any CPU.Build.0 = Debug|Any CPU + {600588D9-4DC3-6DCF-4401-A0341863B874}.Release|Any CPU.ActiveCfg = Release|Any CPU + {600588D9-4DC3-6DCF-4401-A0341863B874}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9F76B6CD-265C-429A-BAD8-0A2BC7143DF2} + EndGlobalSection +EndGlobal diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/AdventureCycle.jpg b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/AdventureCycle.jpg new file mode 100644 index 00000000..86f87c5c Binary files /dev/null and b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/AdventureCycle.jpg differ diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/InvoiceGroupDetails.xlsx b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/InvoiceGroupDetails.xlsx new file mode 100644 index 00000000..17bb9afb Binary files /dev/null and b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/InvoiceGroupDetails.xlsx differ diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/ProductDetails.xlsx b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/ProductDetails.xlsx new file mode 100644 index 00000000..b1f16017 Binary files /dev/null and b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/ProductDetails.xlsx differ diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/Template.docx b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/Template.docx new file mode 100644 index 00000000..b53c124b Binary files /dev/null and b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Data/Template.docx differ diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Output/.gitkeep b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Output/Invoice.docx b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Output/Invoice.docx new file mode 100644 index 00000000..d211e730 Binary files /dev/null and b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Output/Invoice.docx differ diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Perform-Mail-Merge-With-Excel-Data.csproj b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Perform-Mail-Merge-With-Excel-Data.csproj new file mode 100644 index 00000000..d4481ba7 --- /dev/null +++ b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Perform-Mail-Merge-With-Excel-Data.csproj @@ -0,0 +1,31 @@ + + + + Exe + net8.0 + Perform_Mail_Merge_With_Excel_Data + + + + + Always + + + Always + + + Always + + + Always + + + + + + + + + + + diff --git a/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Program.cs b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Program.cs new file mode 100644 index 00000000..c211fcfa --- /dev/null +++ b/Mail-Merge/Perform-Mail-Merge-With-Excel-Data/.NET/Perform-Mail-Merge-With-Excel-Data/Program.cs @@ -0,0 +1,115 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.XlsIO; +using System.Collections; +using System.Data; +using System.IO; + +namespace Perform_Mail_Merge_With_Excel_Data +{ + class Program + { + static void Main(string[] args) + { + + // Creates a new instance of FileStream to open the template document. + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) + { + //Opens the template document. + using (WordDocument document = new WordDocument(fileStream, FormatType.Docx)) + { + //Uses the mail merge events handler for image fields. + document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_LogoImage); + document.MailMerge.StartAtNewPage = true; + //Get the data table from the Excel file + DataTable dataTable = GetDataTable(Path.GetFullPath(@"Data/InvoiceGroupDetails.xlsx")); + //Set the table name for the data table. + //Note that table name and group name should be same. + dataTable.TableName = "Invoice"; + //Create data set and add data table into it + DataSet dataSet = new DataSet(); + dataSet.Tables.Add(dataTable); + //Get the data table from the Excel file + dataTable = GetDataTable(Path.GetFullPath(@"Data/ProductDetails.xlsx")); + dataTable.TableName = "Products"; + dataSet.Tables.Add(dataTable); + //Get the commands for mail merge execution. + ArrayList commands = GetCommands(); + //Executes the nested mail merge with the specified data set and commands. + document.MailMerge.ExecuteNestedGroup(dataSet, commands); + document.Save(Path.GetFullPath(@"Output/Invoice.docx"), FormatType.Docx); + } + } + } + + #region Helper Methods + /// + /// Read the Excel file and return the data table for Mail Merge. + /// + /// + /// + private static DataTable GetDataTable(string fileName) + { + DataTable dataTable; + //Creates a new instance for FileStream to read the Excel file. + using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)) + { + //Creates a new instance for ExcelEngine. + using (ExcelEngine excelEngine = new ExcelEngine()) + { + //Loads or open an existing workbook through Open method of IWorkbooks. + IWorkbook workbook = excelEngine.Excel.Workbooks.Open(fileStream); + //The first worksheet object in the worksheets collection is accessed. + IWorksheet sheet = workbook.Worksheets[0]; + //Get as DataTable. + dataTable = sheet.ExportDataTable(sheet.UsedRange, ExcelExportDataTableOptions.ColumnNames); + } + } + return dataTable; + } + /// + /// Get the commands for Mail Merge Execution. + /// + /// + private static ArrayList GetCommands() + { + //Define commands with the table name and expression for linking the multiple data tables + //during nested Mail merge process. + //You can use the “%TableName.ColumnName%” expression for getting the current value of specified column or field from the table. + + //ArrayList contains the list of commands + ArrayList commands = new ArrayList(); + //DictionaryEntry contains "Source table" (key) and "Command" (value) + //Retrieves the invoice details + DictionaryEntry entry = new DictionaryEntry("Invoice", string.Empty); + commands.Add(entry); + //Retrieves the products details + entry = new DictionaryEntry("Products", "InvoiceNo = %Invoice.InvoiceNo%"); + commands.Add(entry); + return commands; + } + /// + /// Mail merge events handler for image fields. + /// + /// + /// + private static void MergeField_LogoImage(object sender, MergeImageFieldEventArgs args) + { + //Binds image from file system during mail merge. + if (args.FieldName == "Logo") + { + string logoFileName = args.FieldValue.ToString(); + //Gets the image from file system + FileStream imageStream = new FileStream(@"../../../Data/" + logoFileName, FileMode.Open, FileAccess.Read); + args.ImageStream = imageStream; + //Gets the picture, to be merged for image merge field. + WPicture picture = args.Picture; + //Resizes the picture + picture.Height = 40; + picture.Width = 90; + } + } + #endregion + + } +}