Click here to Skip to main content
12,249,488 members (40,827 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

49.4K views
13 bookmarked
Posted

Convert CSV File to data table

, 27 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

EddeKreij
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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
erwinmahatma15-May-15 6:34
membererwinmahatma15-May-15 6:34 
AnswerThanks.... Pin
Nidhin.K.V19-Jun-14 1:21
memberNidhin.K.V19-Jun-14 1:21 
GeneralMy vote of 4 Pin
Abdul Quader Mamun13-Aug-12 18:00
memberAbdul Quader Mamun13-Aug-12 18:00 
GeneralMy vote of 4 Pin
Christian Amado27-Jul-12 12:55
memberChristian Amado27-Jul-12 12:55 
BugBetter splitting Pin
JoeSox27-Jul-12 10:54
memberJoeSox27-Jul-12 10:54 
GeneralThoughts Pin
PIEBALDconsult27-Jul-12 9:00
memberPIEBALDconsult27-Jul-12 9: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 27 Jul 2012
Article Copyright 2012 by EddeKreij
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid