|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using System.Collections; |
| 4 | +using System.Data; |
| 5 | +using System.Data.OleDb; |
| 6 | + |
| 7 | +namespace Convert_Database_into_Dataset_and_Perform_Mailmerge |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] args) |
| 12 | + { |
| 13 | + // Load the word document |
| 14 | + using (FileStream sourceStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 15 | + { |
| 16 | + //Creating a new document. |
| 17 | + using (WordDocument document = new WordDocument(sourceStreamPath, FormatType.Docx)) |
| 18 | + { |
| 19 | + |
| 20 | + string dataBase = Path.GetFullPath(@"../../../Data/EmployeeDetails.mdb"); |
| 21 | + |
| 22 | + // Get all data |
| 23 | + DataSet ds = GetAllTables(dataBase); |
| 24 | + //ArrayList contains the list of commands |
| 25 | + ArrayList commands = GetCommands(); |
| 26 | + //Executes the mail merge |
| 27 | + document.MailMerge.ExecuteNestedGroup(ds, commands); |
| 28 | + //Saves the Word document to MemoryStream. |
| 29 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 30 | + { |
| 31 | + document.Save(outputStream, FormatType.Docx); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Get the commands to execute with database. |
| 39 | + /// </summary> |
| 40 | + static ArrayList GetCommands() |
| 41 | + { |
| 42 | + //ArrayList contains the list of commands |
| 43 | + ArrayList commands = new ArrayList(); |
| 44 | + |
| 45 | + // Parent table: Employees (no filter, so empty string) |
| 46 | + commands.Add(new DictionaryEntry("Employees", "")); |
| 47 | + |
| 48 | + // Customers filtered by EmployeeID |
| 49 | + commands.Add(new DictionaryEntry("Customers", "EmployeeID = %Employees.EmployeeID%")); |
| 50 | + |
| 51 | + // Orders filtered by CustomerID |
| 52 | + commands.Add(new DictionaryEntry("Orders", "CustomerID = %Customers.CustomerID%")); |
| 53 | + |
| 54 | + return commands; |
| 55 | + } |
| 56 | + //Retrieves all required tables from the MDB database and prepares hierarchy commands for DocIO mail merge. |
| 57 | + static DataSet GetAllTables(string dataBase) |
| 58 | + { |
| 59 | + // Connection string using ACE OLEDB provider (64-bit compatible) |
| 60 | + string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={dataBase};Persist Security Info=False;"; |
| 61 | + // DataSet to hold all tables |
| 62 | + DataSet ds = new DataSet(); |
| 63 | + |
| 64 | + using (OleDbConnection conn = new OleDbConnection(connectionString)) |
| 65 | + { |
| 66 | + conn.Open(); |
| 67 | + // Tables to fetch from MDB |
| 68 | + string[] tables = { "Employees", "Customers", "Orders" }; |
| 69 | + // Loop through each table and fill DataSet |
| 70 | + foreach (string tableName in tables) |
| 71 | + { |
| 72 | + string sqlQuery = $"SELECT * FROM {tableName}"; |
| 73 | + OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, conn); |
| 74 | + adapter.Fill(ds, tableName); |
| 75 | + } |
| 76 | + } |
| 77 | + return ds; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments