Reputation: 3980
I need to change the below function to read from a tab delimited file any ideas how to do this?
Public Function ConvertCsvToDatatable(ByVal ColumnNames As Boolean) As DataTable
Try
Dim dt As New DataTable
For Each columnName In getColumns(ColumnNames)
dt.Columns.Add(columnName)
Next
Dim fileReader As New StreamReader(FileName)
If ColumnNames Then
fileReader.ReadLine()
End If
Dim line As String = fileReader.ReadLine
While Not IsNothing(line)
line = line.Replace(Chr(34), "")
dt.Rows.Add(line.Split(","))
line = fileReader.ReadLine
End While
fileReader.Close()
Return dt
Catch ex As Exception
'log to file
End Try
Return Nothing
End Function
Upvotes: 0
Views: 509
Reputation: 10899
I would probably replace the following line:
dt.Rows.Add(line.Split(","))
With:
dt.Rows.Add(line.Split(vbTab))
That should split on tab delimited values...
That said, I would really recommend a specialized library to do this, since SCV / TSV can be fairly complicated. I like KBCsv.
Upvotes: 2
Reputation: 21757
Change line.Split(",")
to line.Split(vbTab)
. This will let you use the tab character as delimiter instead of a comma.
Upvotes: 2