Articles in this section
Category / Section

Is this possible to Mail Merge using csv data source?

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

Sample

http://www.syncfusion.com/uploads/redirect.aspx?file=MailMerge_CSV_2b032d18.zip&team=support

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