Reading CSV files

  • Thread starter Thread starter RD
  • Start date Start date
R

RD

Need to read a comma separated values file, line by line and extract each
value from each line. There may be variable number of values in each of the
lines.

Anyone know of any sample code to do this?
Used to be easy in vb6 , oh well!

Thanks for any help,

Bob
 
Need to read a comma separated values file, line by line and extract each
value from each line. There may be variable number of values in each of the
lines.

Anyone know of any sample code to do this?
Used to be easy in vb6 , oh well!

Thanks for any help,

Bob

It's easy in .Net too. Look at the classes in the System.IO namespace.
Here's a sample from memory (may have typos):

'\\\\\
Imports System.IO

Public Sub ReadCSVFile(fname As String)

Dim sr As StreamReader = File.OpenText(fname)

'Array to hold all of the comma delimited items
Dim aTokens() As String

Do While sr.Peek() >= 0
aTokens = sr.ReadLine().Split(',')
For x As Integer = 0 to aTokens.Length - 1
'Do something here with the token
Console.WriteLine(aTokens(x))
Next
Loop

sr.Close()

End Sub
'/////
 
Thanks Chris
Chris Dunaway said:
It's easy in .Net too. Look at the classes in the System.IO namespace.
Here's a sample from memory (may have typos):

'\\\\\
Imports System.IO

Public Sub ReadCSVFile(fname As String)

Dim sr As StreamReader = File.OpenText(fname)

'Array to hold all of the comma delimited items
Dim aTokens() As String

Do While sr.Peek() >= 0
aTokens = sr.ReadLine().Split(',')
For x As Integer = 0 to aTokens.Length - 1
'Do something here with the token
Console.WriteLine(aTokens(x))
Next
Loop

sr.Close()

End Sub
'/////
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
 
Back
Top