Dealing with commas in a CSV file

后端 未结 27 2812
傲寒
傲寒 2020-11-21 06:53

I am looking for suggestions on how to handle a csv file that is being created, then uploaded by our customers, and that may have a comma in a value, like a company name.

27条回答
  •  情深已故
    2020-11-21 07:13

    Add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL).

    Use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file Here is the sample code:

     Dim parser As TextFieldParser = New TextFieldParser("C:\mar0112.csv")
     parser.TextFieldType = FieldType.Delimited
     parser.SetDelimiters(",")      
    
       While Not parser.EndOfData         
          'Processing row             
          Dim fields() As String = parser.ReadFields         
          For Each field As String In fields             
             'TODO: Process field                   
    
          Next      
          parser.Close()
       End While 
    

提交回复
热议问题