1.11M Members

How to read csv file contents in VB.Net

 
0
 

Hi,
I want to read csv (Comma Separated Values) file content in VB.Net
I want read entire values in file store in the fixed blocks ( say array )
Please guide me for reading the csv file content in VB.Net. Also how to get count of the data
in csv file so that i can create blocks of data at runtime.
Thanks
Mukesh
:)

 
0
 

Hey Mukesh

Private Sub ReadAllText()

        ' Hold the Parsed Data
        Dim ary(0) As String

        ' Reader to read from the file
        Dim sr As New System.IO.StreamReader("LocalFile.csv" Or "Localfile.txt")

        ' Hold the amount of lines already read in a 'counter-variable' 
        Dim placeholder As Integer = 0

        Do While sr.Peek <> -1 ' Is -1 when no data exists on the next line of the CSV file
            ary(placeholder) = sr.ReadLine
            placeholder += 1
        Loop

        ' NOTE: If you wish to parse the data inside the array,
        ' loop through all elements, use the String.Split() method
        ' delimited by a comma (for CSV).

    End Sub

Try the above code, hope this helps :)

Leigh

 
0
 

Tried using this, but i get a "Index was outside the bounds of the array." error at the

Ary(placeholder) = sr.ReadLine

Line ???

 
0
 

Please note guys, and anybode else who comes here from searching for a solution to the problem of parsing CSV data:

The code posted in this thread does not parse CSV data in any but it's most excruciatingly simple form.

You cannot just split on commas because a) character fields may contain commas, and b) even if they don't character fields will be surrounded by double-quote characters that have to be stripped away.

You also cannot use readline because character fields may contain newline characters.

Finally, you must un-escape the double double-quoting escaping system used for fields that contain double-quote characters.

The absence of a CSV parser in the .Net library is a bit shocking in view of the fact that Microsoft invented the format in the first place, it's such a common requirement, and there seems to be so few solutions out there.

I don't have a solution either. When I find one I'll post a link or the code...

 
0
 

In insert this:

Redim Preserve ary(placeholder)

after Do while...

Should work fine now. I love posting on old threads :)

 
0
 

I know this thread is inactive BUT VB.net has a built in text parser for handling delimited files such as CSV: MSDN: Read CSV File Content

 
0
 

Build a form, add the following to it:

Listbox1
button1

With all due respect, I find Microsoft's VisualBasic streamreader lacking. For a no-holdup-does-not-rely-on-other-plugin-crap I wrote a script that will load a .csv into a two-dimensional array, and for giggles, pump it back onscreen in a listbox, showing separation in the array with a pipe "|' symbol.

The file is read in two stages. The first determines the size of the array that needs to be defined (some may prefer an unbound array, if so, you know what to do about it.) Stage 2 loads the data into the array and dumps it to a listbox. I've tested this on a decent-sized .CSV that was 158 columns x 13224 rows and it worked under a minute. Mileage may vary depending on your hardware.

The script recognizes "," as the delimiter and quotes for strings. It will even ignore commas inside the string and recognize double-quotes in the string. What it doesn't do? It doesn't break down the double-quotes inside of strings.

I wrote this because 99% of us just need to load the d@mned .CSV into an array or listbox in the real world, and care only that IT WORKS.

Technogeeks and programming addicts need not add your two cents worth. If you could a wrote this, you would have done so and I wouldn't be here to show you up.

For everyone else, I only hope this is valuable to you.

Cut and paste the following:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  Button1.Click
    Dim dx As Long = 0
    Dim dRow As Long = 0
    Dim dColumn As Long = 0
    Dim dTotalRows As Long = 0
    Dim dTotalColumns As Long = 0
    Dim dFileName As String = ""
    Dim dReadLine As String = ""
    Dim dChar As String = ""
    Dim dArray(1, 1) As String
    Dim dStart As Long = 1
    Dim dEnd As Long = 1
    Dim dLen As Long = 0
    Dim dLineLength As Long = 0
    Dim dQuoteCounter As Long = 0
    Dim dAdd2ItemList As String = ""
    dFileName = "<insert file name here>"
    FileOpen(1, dFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1)
    Do While Not EOF(1)
        dReadLine = LineInput(1)
        dRow = dRow + 1
        dTotalColumns = dColumn
        dColumn = 0
        dLineLength = Len(dReadLine)
        For dx = 1 To dLineLength
            dChar = Mid(dReadLine, dx, 1)
            If dChar = Chr(34) Then
                dQuoteCounter = dQuoteCounter + 1
                If dQuoteCounter = 2 * (Int(dQuoteCounter / 2)) Then
                    dChar = Mid(dReadLine, dx, 2)
                    If dChar = Chr(34) + "," Then
                        dColumn = dColumn + 1
                        dQuoteCounter = -1
                    End If
                End If
            End If
            If dQuoteCounter = 0 And dChar = "," Then
                dColumn = dColumn + 1
            End If
            If dx = dLineLength Then
                dColumn = dColumn + 1
            End If
            If dQuoteCounter = -1 And dChar = "," Then dQuoteCounter = 0
        Next (dx)
    Loop
    dTotalRows = dRow
    ReDim dArray(dTotalRows, dTotalColumns)
    FileClose(1)
    dRow = 0
    FileOpen(1, dFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1)
    Do While Not EOF(1)
        dAdd2ItemList = ""
        dRow = dRow + 1
        dStart = 1
        dEnd = 1
        dLen = 0
        dColumn = 0
        dReadLine = LineInput(1)
        dLineLength = Len(dReadLine)
        For dx = 1 To dLineLength
            dChar = Mid(dReadLine, dx, 1)
            If dChar = Chr(34) Then
                dQuoteCounter = dQuoteCounter + 1
                If dQuoteCounter = 1 Then dStart = dx + 1
                If dQuoteCounter = 2 * (Int(dQuoteCounter / 2)) Then
                    dChar = Mid(dReadLine, dx, 2)
                    If dChar = Chr(34) + "," Then
                        dEnd = dx
                        dLen = dEnd - dStart
                        dColumn = dColumn + 1
                        dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
                        dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
                        dQuoteCounter = -1
                        dStart = dx + 2
                    End If
                End If
            End If
            If dQuoteCounter = 0 And dChar = "," Then
                dEnd = dx
                dLen = (dEnd - dStart)
                dColumn = dColumn + 1
                If dLen < 1 Then
                    dArray(dRow, dColumn) = ""
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
                Else
                    dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
                End If
                dStart = dx + 1
            End If
            If dx = dLineLength Then
                dEnd = dx
                dLen = (dEnd - dStart) + 1
                dColumn = dColumn + 1
                If dLen < 1 Then
                    dArray(dRow, dColumn) = ""
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn)
                Else
                    dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn)
                End If
                dStart = dx + 1
            End If
            If dQuoteCounter = -1 And dChar = "," Then dQuoteCounter = 0
        Next (dx)
        ListBox1.Items.Add(dAdd2ItemList)
        REM dRow = dRow + 1
    Loop

End Sub
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: