Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Convert CSV File to data table

Rate me:
Please Sign up or sign in to vote.
4.41/5 (9 votes)
27 Jul 2012CPOL 74.7K   16   6
Convert csv file to a data table

Introduction

This class allows you to convert a csv file to a data table. Useful when coding database import applications.

Background

After writing numerous database import applications I decided to create a standard class for reading CSV files and converting them into a Data Table

Using the code

The CSV Reader Class. Firstly we need to know the file location so we create a string attribute to store the file location.

C++
Private FileName As String  

The constructor requires the file location.

Public Sub New(ByVal FileName As String)
       Me.FileName = FileName
End Sub

The getcolumns method returns an array of string values. These string values are the column names. these can either be  Names for the first row in the csv file, achieved by setting the columnNames  attribute true or the names are Column1, Column2,.....

Public Function getColumns(ByVal ColumnNames As Boolean) As String()
      Try
          Dim fileReader As New StreamReader(FileName)
          Dim line As String = fileReader.ReadLine
          fileReader.Close()
          Dim Columns() As String = line.Split(",")
          If ColumnNames Then
              Return Columns
          End If
          Dim i As Integer = 1
          Dim c As Integer = 0
          Dim columnsNames(Columns.Count - 1) As String
          For Each column As String In Columns
              columnsNames(c) = "column" & i
              i += 1
              c += 1
          Next
          Return columnsNames
      Catch ex As Exception
          'log to file
      End Try
      Return Nothing
  End Function

The returnData method will return a data table.

Public Function ReturnData(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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Solutions Software
United Kingdom United Kingdom
I write vb applications in my spare time to generate some extra cash. Generally I would love to do this full time, so if you are looking for a developer with VB.net, C# experience and SQL. I could be your man.

Hobbies:
Coding, Coding, Coding
Swimming
Singing

Comments and Discussions

 
GeneralMy vote of 5 Pin
erwinmahatma15-May-15 5:34
erwinmahatma15-May-15 5:34 
This was very helpful and it is exactly what I needed. Great work!
AnswerThanks.... Pin
Nidhin.K.V19-Jun-14 0:21
Nidhin.K.V19-Jun-14 0:21 
GeneralMy vote of 4 Pin
Abdul Quader Mamun13-Aug-12 17:00
Abdul Quader Mamun13-Aug-12 17:00 
GeneralMy vote of 4 Pin
Christian Amado27-Jul-12 11:55
professionalChristian Amado27-Jul-12 11:55 
BugBetter splitting Pin
JoeSox27-Jul-12 9:54
JoeSox27-Jul-12 9:54 
GeneralThoughts Pin
PIEBALDconsult27-Jul-12 8:00
mvePIEBALDconsult27-Jul-12 8:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.