Results 1 to 2 of 2

Thread: Read CSV Files in Vb.net

  1. #1

    Thread Starter
    Addicted Member ryanframes's Avatar
    Join Date
    Apr 2012

    Read CSV Files in Vb.net

    Hello , i'm sorry if this is a silly question.
    But i'm new to vb.net and it's my first time working with CSV so i don't know what to do.
    i have csv files like this :
    Hello,"First Timer, welcome ",to,vb.net
    i want to split it into :
    arr(0) = "Hello"
    arr(1) = "First Timer, welcome"
    arr(2) = "to"
    arr(3) = "vb.net"
    i searched on google and found that i can use TextFieldParser to achieve what i want to do, but it still returns :
    Hello,"First Timer, welcome ",to,vb.net
    here's my code :
    Using parser As New FileIO.TextFieldParser(txtPath.Text)
                Dim j As Integer = 0, i As Integer = 0
                Dim curRec As String()
                parser.TextFieldType = FileIO.FieldType.Delimited
                parser.Delimiters = New String() {","}
                parser.HasFieldsEnclosedInQuotes = True
                While Not parser.EndOfData
                        curRec = parser.ReadFields
                        If Not curRec Is Nothing Then
                            For i = 0 To curRec.GetUpperBound(0)
                                My.Computer.FileSystem.WriteAllText("c:\test.txt", curRec(i) & vbCrLf, True)
                        End If
                        j = j + 1
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical)
                        Exit While
                    End Try
                End While
            End Using
    Sorry for bad english.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Read CSV Files in Vb.net

    You need to learn how to debug. If you'd done that then you'd see that 'curRec' contains exactly what you said you wanted. If you want to then write those array elements to separate lines in a file then just call File.WriteAllLines and pass the array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width