Public Sub CreateDelimitedFile(ByVal dt As DataTable, ByVal filePath As String, ByVal fileDelimiter As String, ByVal writeHeader As Boolean)
Dim sw As StreamWriter = New StreamWriter(filePath, False)
Dim ColumnCount As Integer = dt.Columns.Count
If writeHeader = True Then
For i As Integer = 0 To ColumnCount - 1
sw.Write(dt.Columns(i))
If i < ColumnCount - 1 Then
sw.Write(fileDelimiter)
Else
sw.Write(sw.NewLine)
End If
Next
End If
For Each row As DataRow In dt.Rows
For i As Integer = 0 To ColumnCount - 1
If Not row.IsNull(i) Then
sw.Write(row.Item(i).ToString())
End If
If i < ColumnCount - 1 Then
sw.Write(fileDelimiter)
Else
sw.Write(sw.NewLine)
End If
Next
Next
sw.Close()
End Sub