how to read a csv file using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sammil
    New Member
    • Aug 2008
    • 1

    how to read a csv file using c#

    how to read a csv file using c#
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You 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.

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Originally posted by Plater
      You 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.
      For sure one way would be a regular expression, but my opinion is this:

      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
      Now you can treat Fields() as any other array, each of your fields is held within an item in the array.

      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
      Bear in mind that while the second method may be more encompassing, because it has to parse each line more thoroughly, it may be noticeably slower.

      Comment

      Working...