Sub csv2()
Dim filename As String = "sample.csv"
Dim fields As New Collections.Specialized.StringCollection
Dim delimiter As String = ","
Dim fieldqualifier As String = """"
Dim infield As Boolean = False
Dim line As String, from, i As Integer
Dim fs As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)
Dim sr As New IO.StreamReader(fs)
While sr.Peek <> -1
fields.Clear()
line = sr.ReadLine
For i = 0 To line.Length - 1
If line(i) = fieldqualifier Then
infield = Not infield
ElseIf line(i) = delimiter And infield = False Then
fields.Add(clean(line.Substring(from, i - from), [SIZE=2]fieldqualifier[/SIZE]))
from = i + 1
End If
Next
fields.Add(clean(line.Substring(from, i - from), [SIZE=2]fieldqualifier[/SIZE]))
display(fields)
from = 0
End While
[SIZE=2] sr.Close()
fs.Close()
[/SIZE]End Sub
Function clean(ByVal field As String, ByVal fieldqualifier As String) As String
field = field.Trim()
If field.StartsWith([SIZE=2]fieldqualifier[/SIZE]) = True And field.EndsWith(fieldqualifier) = True Then
Return field.Substring(1, field.Length - 2)
Else
Return field
End If
End Function
Sub display(ByVal fields As Collections.Specialized.StringCollection)
Dim sb As New System.Text.StringBuilder
For Each str As String In fields
sb.AppendLine("'" & str & "'")
Next
MessageBox.Show(sb.ToString)
End Sub