how to read a csv file using c#
how to read a csv file using c#
Collapse
X
-
Originally posted by PlaterYou will need to write a function to parse the file. I recomend looking up a regular expression that can do it, and then implementing THAT.
A CSV file is really just a plaintext file...each line represents a record, each record's fields are separated by a comma ",".
Before you can parse the file, you need to answer yourself the question, can fields contain commas, and if so, what in the file denotes that a field contains a comma. In most cases, a field can contain a comma if it's wrapped with single or double quotes.
If your fields are not going to be allowed to contain commas, then it's as simple as splitting each row based on ",".
Code:Dim oRdr As System.IO.StreamReader = System.IO.File.OpenText("C:\Test\MyTestFile.csv") Dim CrntLine As String = Nothing While CrntLine = oRdr.ReadLine Dim Fields() As String = CrntLine.Split(",") End While
If you need your fields to be able to contain commas when wrapped, then you'll have to get a bit more complex...
In order to allow for a field to contain a comma, you'd need to parse each field individually.
Code:While crntLine <> "" Dim FieldStart As Integer = 0 Dim FieldEnd As Integer = 0 Dim NextField As Integer = 0 If crntLine.IndexOf(""") = 0 Then FieldStart = 1 'We don't care to include the " 'Handle a closing " that isn't followed immediately by a , 'What we're doing here is finding the closing ", but then reading 'on to the next , and treating it all as one field. Dim CloseIndex As Integer = crntLine.IndexOf("""", 1) - 1 FieldEnd = crntLine.IndexOf(",", CloseIndex) Else FieldStart = 0 FieldEnd = crntLine.IndexOf(",") End If crntField = crntLine.SubString(FieldStart, FieldEnd) 'Pass this out to do something useful... 'Remove the current field from the string crntLine = crntLine.SubString(FieldEnd + 1) '+1 drops the trailing , End While
Comment
Comment