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. However, it is possible by converting the CSV data source to a DataTable and then passing 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 yourself or let exceptions 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 the 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 arbitrary 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 our DataTable
        csvDataTable.Rows.Add(row);
    }
    // Return the CSV DataTable
    return csvDataTable;
}


VB
doc.MailMerge.Execute(csvToDataTable("..\..\test1.csv", True))
Public Shared Function csvToDataTable(file As String, isRowOneHeader As Boolean) As DataTable
    Dim csvDataTable As New DataTable()
    '' No try/catch - add these yourself or let exceptions 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 the 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 arbitrary 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 our DataTable
        csvDataTable.Rows.Add(row)
    Next i
    '' Return the CSV DataTable
    Return csvDataTable
End Function

Conclusion

I hope you enjoyed learning about the possibility of performing a mail merge using a CSV data source in WinForms DocIO.

You can refer to our WinForms Word feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with 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 explore 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 forums or feedback 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