MacRaider4
Member
- Joined
- Jan 8, 2010
- Messages
- 13
- Programming Experience
- Beginner
I'm new to using CSV Files, and I've been looking around for a few hours now for a good example on them and no luck. This is what I have so far, which does work... I'm just displaying it in a text box while I work the kinks out and learn more about this...
What I need is to figure out how to ignore commas within the file... for example lets stay Data Entry Guru entered 78 Main St, Apt 1 and the raw file reads ,"78 Main St, Apt 1",Anytown, CA
I would like 78 Main St, Apt 1 to be one "field", Anytown another and CA another.
I'm using VB.Net 2005 for this.
Thank you in advance.
VB.NET:
Private Sub ReadCSVFileToArray()
Dim strfilename As String
Dim num_rows As Long
Dim num_cols As Long
Dim x As Integer
Dim y As Integer
Dim strarray(1, 1) As String
' Load the file.
strfilename = "C:\myTest.csv"
'Check if file exist
If File.Exists(strfilename) Then
Dim tmpstream As StreamReader = File.OpenText(strfilename)
Dim strlines() As String
Dim strline() As String
'Load content of file to strLines array
strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)
' Redimension the array.
num_rows = UBound(strlines)
strline = strlines(0).Split(",")
num_cols = UBound(strline)
ReDim strarray(num_rows, num_cols)
' Copy the data into the array.
For x = 0 To num_rows - 1
strline = strlines(x).Split(",")
For y = 0 To num_cols
strarray(x, y) = strline(y)
Next
Next
' Display the data in textbox
For x = 0 To num_rows
For y = 0 To num_cols
TextBox1.Text = TextBox1.Text & strarray(x, y) & ","
Next
TextBox1.Text = TextBox1.Text & Environment.NewLine
Next
End If
End Sub
What I need is to figure out how to ignore commas within the file... for example lets stay Data Entry Guru entered 78 Main St, Apt 1 and the raw file reads ,"78 Main St, Apt 1",Anytown, CA
I would like 78 Main St, Apt 1 to be one "field", Anytown another and CA another.
I'm using VB.Net 2005 for this.
Thank you in advance.