My Personal Blog's
Posts tagged Data Table to CSV
VB.NET Set Data Table to CSV File
Jun 3rd
This function show how to Convert DataTable to CSV File
Sub SetDataTable_To_CSV(ByVal dtable As DataTable, ByVal path_filename As String, ByVal sep_char As String)
Dim writer As System.IO.StreamWriter
Try
writer = New System.IO.StreamWriter(path_filename)
Dim _sep As String = ""
Dim builder As New System.Text.StringBuilder
For Each col As DataColumn In dtable.Columns
builder.Append(_sep).Append(col.ColumnName)
_sep = sep_char
Next
writer.WriteLine(builder.ToString())
For Each row As DataRow In dtable.Rows
_sep = ""
builder = New System.Text.StringBuilder
For Each col As DataColumn In dtable.Columns
builder.Append(_sep).Append(row(col.ColumnName))
_sep = sep_char
Next
writer.WriteLine(builder.ToString())
Next
Catch ex As Exception
Finally
If Not writer Is Nothing Then writer.Close()
End Try
End Sub
This is how you use it
Dim dt As New DataTable SetDataTable_To_CSV(dt, "C:\test.csv", ",")




