Posted By

JoeMaddalone on 01/31/10


Tagged


Versions (?)

Read CSV to DAtatable


 / Published in: VB.NET
 

  1. 'Returns datatable from CSV File
  2. Function ReadCSV(ByVal path As String) As System.Data.DataTable
  3. Dim sr As New StreamReader(path)
  4. Dim fullFileStr As String = sr.ReadToEnd()
  5. sr.Close()
  6. sr.Dispose()
  7. Dim lines As String() = fullFileStr.Split(ControlChars.Lf)
  8. Dim recs As New DataTable()
  9. Dim sArr As String() = lines(0).Split(","c)
  10. For Each s As String In sArr
  11. recs.Columns.Add(New DataColumn())
  12. Next
  13. Dim row As DataRow
  14. Dim finalLine As String = ""
  15. For Each line As String In lines
  16. row = recs.NewRow()
  17. finalLine = line.Replace(Convert.ToString(ControlChars.Cr), "")
  18. row.ItemArray = finalLine.Split(","c)
  19. recs.Rows.Add(row)
  20. Next
  21. Return recs
  22. End Function

Report this snippet  

You need to login to post a comment.