Skip to content

Commit 42cf04b

Browse files
Added sample
1 parent 5c840e8 commit 42cf04b

7 files changed

Lines changed: 131 additions & 0 deletions

File tree

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 17
4+
VisualStudioVersion = 17.14.37301.10 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert_Database_into_Dataset_and_Perform_Mailmerge", "Convert_Database_into_Dataset_and_Perform_Mailmerge\Convert_Database_into_Dataset_and_Perform_Mailmerge.csproj", "{F466E5AF-87B2-BED0-A680-8B3305676943}"
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+
{F466E5AF-87B2-BED0-A680-8B3305676943}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F466E5AF-87B2-BED0-A680-8B3305676943}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F466E5AF-87B2-BED0-A680-8B3305676943}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F466E5AF-87B2-BED0-A680-8B3305676943}.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 = {3CD5B0D7-D2A2-4D43-8C8D-3239CD3DE50D}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
<PackageReference Include="System.Data.OleDb" Version="*" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
20+
<None Update="Output\.gitkeep">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)