Articles in this section
Category / Section

Is it Possible to Mail Merge using CSV Data Source in WinForms DoclO?

2 mins read


There is no direct option to pass the csv data source and MailMerge it. But it can be possible by converting the csv datasource to Datatable, and then pass this table to execute the MailMerge.
C#
doc.MailMerge.Execute(csvToDataTable(@"..\..\test1.csv",true ));
 public static DataTable csvToDataTable(string file, bool isRowOneHeader)
{
 DataTable csvDataTable = new DataTable();
 //no try/catch - add these in yourselfs or let exception happen
String[] csvData = File.ReadAllLines(file);
 //if no data in file ‘manually’ throw an exception
if (csvData.Length == 0)
{
throw new Exception("csv File Appears to be Empty");
}
 String[] headings = csvData[0].Split('','');
int index = 0; //will be zero or one depending on isRowOneHeader
 if (isRowOneHeader) //if first record lists headers
{
index = 1; //so we won’t take headings as data
 //for each heading
for (int i = 0; i < headings.Length; i++)
{
//replace spaces with underscores for column names
headings[i] = headings[i].Replace(" ", "_");
 //add a column for each heading
csvDataTable.Columns.Add(headings[i], typeof(string));
}
}
else //if no headers just go for col1, col2 etc.
{
for (int i = 0; i < headings.Length; i++)
{
//create arbitary column names
csvDataTable.Columns.Add("col" + (i + 1).ToString(), typeof(string));
}
}
 //populate the DataTable
for (int i = index; i < csvData.Length; i++)
{
//create new rows
DataRow row = csvDataTable.NewRow();
 for (int j = 0; j < headings.Length; j++)
{
//fill them
row[j] = csvData[i].Split('','')[j];
}
 //add rows to over DataTable
csvDataTable.Rows.Add(row);
}
 //return the csv DataTable
return csvDataTable;
 }
VB
doc.MailMerge.Execute(csvToDataTable("..\..\test1.csv",True))
 public static DataTable csvToDataTable(String file, Boolean isRowOneHeader)
 Dim csvDataTable As DataTable = New DataTable()
 ''no try/catch - add these in yourselfs or let exception happen
Dim csvData() As String = File.ReadAllLines(file)
 ''if no data in file ‘manually’ throw an exception
If csvData.Length = 0 Then
Throw New Exception("csv File Appears to be Empty")
End If
 Dim headings() As String = csvData(0).Split(","c)
Dim index As Integer = 0 ''will be zero or one depending on isRowOneHeader
 If isRowOneHeader Then ''if first record lists headers
index = 1 ''so we won’t take headings as data
 ''for each heading
For i As Integer = 0 To headings.Length - 1
''replace spaces with underscores for column names
headings(i) = headings(i).Replace(" ", "_")
 ''add a column for each heading
csvDataTable.Columns.Add(headings(i), GetType(String))
Next i
Else ''if no headers just go for col1, col2 etc.
For i As Integer = 0 To headings.Length - 1
''create arbitary column names
csvDataTable.Columns.Add("col" & (i + 1).ToString(), GetType(String))
Next i
End If
 ''populate the DataTable
For i As Integer = index To csvData.Length - 1
''create new rows
Dim row As DataRow = csvDataTable.NewRow()
 For j As Integer = 0 To headings.Length - 1
''fill them
row(j) = csvData(i).Split(","c)(j)
Next j
 ''add rows to over DataTable
csvDataTable.Rows.Add(row)
Next i
 ''return the csv DataTable
Return csvDataTable

Conclusion

I hope you enjoyed learning about the possibility to mail merge using csv data source in WinForms DoclO.

You can refer to our WinForms word feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our 
WinForms word example
 to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, orfeedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied