Results 1 to 5 of 5

Thread: Reading .CSV file in Visual Basic

  1. #1
    Join Date
    Jan 2009
    Posts
    6

    Reading .CSV file in Visual Basic

    Hello ,
    I Wanted to know how can we read in a .CSV file into Visual Basic.i wanted to use the data using field seperator a comma. I wanted read data from the .CSV file in visual basic row by row as each is a record of an event. as i am novive in this field so would be really thankful regarding any help in this this topic. An example of the type of data being used is shown below:

    Code:
    Date Name Age Height
    04/06/08 joe hack 28 192
    04/06/08 miss sammy 32 162

  2. #2
    Join Date
    May 2008
    Posts
    115

    Re: Reading .CSV file in Visual Basic

    Grab the whole file into a string. Then use Split to break it into lines. For each line, use Split again to break it into fields.

    Code:
    Private Sub cmdGo_Click()
    Dim file_name As String
    Dim fnum As Integer
    Dim whole_file As String
    Dim lines As Variant
    Dim one_line As Variant
    Dim num_rows As Long
    Dim num_cols As Long
    Dim the_array() As String
    Dim R As Long
    Dim C As Long
    
        file_name = App.Path
        If Right$(file_name, 1) <> "\" Then file_name = _
            file_name & "\"
        file_name = file_name & "test.csv"
    
        ' Load the file.
        fnum = FreeFile
        Open file_name For Input As fnum
        whole_file = Input$(LOF(fnum), #fnum)
        Close fnum
    
        ' Break the file into lines.
        lines = Split(whole_file, vbCrLf)
    
        ' Dimension the array.
        num_rows = UBound(lines)
        one_line = Split(lines(0), ",")
        num_cols = UBound(one_line)
        ReDim the_array(num_rows, num_cols)
    
        ' Copy the data into the array.
        For R = 0 To num_rows
            one_line = Split(lines(R), ",")
            For C = 0 To num_cols
                the_array(R, C) = one_line(C)
            Next C
        Next R
    
        ' Prove we have the data loaded.
        For R = 0 To num_rows
            For C = 0 To num_cols
                Debug.Print the_array(R, C) & "|";
            Next C
            Debug.Print
        Next R
        Debug.Print "======="
    End Sub
    vbhelper

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Reading .CSV file in Visual Basic

    This code will turn CSV into a DataTable, you can then use this to populate the DataGridView



    Code:
       
     Public Function ReadCsvToTable(ByVal Filename As String) As DataTable
            Dim sr As StreamReader = File.OpenText(Filename)
    
            Dim values As New Collections.Specialized.StringCollection
            values.AddRange(sr.ReadLine.Split(","))
    
            Dim table As New DataTable("table")
            Dim enumerator As Collections.Specialized.StringEnumerator
            enumerator = values.GetEnumerator()
            While enumerator.MoveNext
                table.Columns.Add(New DataColumn(enumerator.Current, GetType(String)))
            End While
    
            Dim row As DataRow
            Dim ordinal As Byte
            Dim buffer As New StringBuilder()
            Do While sr.EndOfStream = False
                If buffer.Length > 0 Then
                    buffer.Remove(0, buffer.Length - 1) 'clear the buffer
                End If
                values.Clear() 'clear the values
                buffer.Append(sr.ReadLine()) 'read a line 
                values.AddRange(buffer.ToString().Split(","))
    
                row = table.NewRow()
                enumerator = values.GetEnumerator()
                ordinal = 0
                While enumerator.MoveNext
                    row(ordinal) = enumerator.Current
                    ordinal += 1
                End While
                table.Rows.Add(row)
            Loop
    
            Return table
        End Function


    The first row in the csv file must contain the column headings.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Reading .CSV file in Visual Basic

    The solution shown in the above code is not what you want exactly , but you know for most things you will never exactly find 'the solution'. You need to try it out and adapt it to suit your needs. This is part of software development.


    Code:
    Using parser As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\temp\myFile.csv")
       parser.SetDelimiters(",")
       While Not parser.EndOfData
          'do console stuff in here
       End While
    End Using
    Do a search for Microsoft.VisualBasic.FileIO.TextFieldParser and you'll find an answer.

  5. #5
    Join Date
    Jan 2009
    Posts
    6

    Re: Reading .CSV file in Visual Basic

    Hi , Thanks for your suggestion

    I have solved my problem using the following code


    Code:
    Using parser As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\temp\myFile.csv")
       parser.SetDelimiters(",")
       While Not parser.EndOfData
          Me.DataGridView1.Rows.Add(parser.ReadFields())
       End While
    End Using

    thanks a lot bye

Similar Threads

  1. What is File Handling in Visual Basic 6.0?
    By Dëfrim in forum Software Development
    Replies: 4
    Last Post: 27-12-2010, 05:54 AM
  2. Changing File Attributes Using Visual Basic 6.0
    By technika in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 12:19 PM
  3. How to create an .exe file in visual basic
    By kasha_i in forum Software Development
    Replies: 3
    Last Post: 21-04-2009, 11:53 PM
  4. what are Visual Basic IDE?
    By Naresh Modi in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 09:49 AM
  5. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,544,143.24851 seconds with 17 queries