logo  VB.Net - CSV Files
This Chapter
File Attributes
Binary File
Column Header
Copy Delete ...
CSV File
File Path
Text File
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
Functions for CSV Files
Combine List of fields into CSV record

Public Function JoinCsv(ListFields As List(Of String)) As String
 For i As Integer = 0 To ListFields.Count - 1
  Dim s As String = ListFields.Item(i)
  s = s.Replace(""""c, """""")
  If s.Contains(""""c) OrElse s.Contains(","c) OrElse s <> s.Trim OrElse ContainsSpecial(s) Then
   ListFields.Item(i) = """" & s & """"
  End If
 Next
 Return String.Join(","c, ListFields)
End Function

Private Function ContainsSpecial(s As String) As Boolean
 If s.Contains(""""c) OrElse s.Contains("'"c) OrElse s.Contains(","c) Then Return True
 Dim regex1 As New System.Text.RegularExpressions.Regex("[^\x20-\x7E]")
 Dim match1 As System.Text.RegularExpressions.Match = regex1.Match(s)
 Return match1.Success
End Function
  
Split a CSV record into List(Of String)
Caution - if you try to read a CSV file as lines of text you will experience problems caused by "linefeed" characters embedded in fields. You must ensure that any CSV records sent to function are complete. (i.e. not split over two or more lines.)
 
Reading CSV files Record at a Time using CSV reader (described next) is usually offers a better solution.

Public Function SplitCSV(ByVal entireRecord As String, ByVal parts As Integer) As List(Of String)
 entireRecord = entireRecord.Replace("""""", """")
 Dim ret As New List(Of String)
 Dim cPos, qPos As Integer
 entireRecord = Trim(entireRecord)
 While entireRecord <> ""
  If Mid(entireRecord, 1, 1) = """" Then
   entireRecord = Mid(entireRecord, 2, Len(entireRecord) - 1)
   qPos = InStr(entireRecord, """")
   If qPos < 1 Then entireRecord &= """" : qPos = Len(entireRecord)
   ret.Add(Mid(entireRecord, 1, qPos - 1).Replace("""", """"))
   Mid(entireRecord, 1, qPos) = Space(qPos)
   entireRecord = Trim(entireRecord)
   ' skip any white space before next comma
   If entireRecord <> String.Empty Then
    cPos = InStr(entireRecord, ",")
    If cPos < 1 Then
     entireRecord = String.Empty
    Else
     Mid(entireRecord, 1, cPos) = Space(cPos)
    End If
    entireRecord = Trim(entireRecord)
   End If
  Else
   cPos = InStr(entireRecord, ",")
   If cPos < 1 Then entireRecord &= "," : cPos = Len(entireRecord)
   ret.Add(Trim(Mid(entireRecord, 1, cPos - 1).Replace("""", """""")))
   Mid(entireRecord, 1, cPos) = Space(cPos)
   entireRecord = Trim(entireRecord)
  End If
  If sections >= parts Then Exit While
 End While
 Return ret
End Function
  
Read CSV Record from a file
Trying to read CSV files line by line can cause a lot of problems e.g. How do you handle embedded line feeds? Wheneber possible, use of the built-in CSVreader

Private Shared ReadOnly delimiters As String() = {","}
Public Sub SplitCSV(filename As String)
    Dim ListFields As List(Of String)
    Using csvParser As New TextFieldParser(filename)
        csvParser.CommentTokens = {}
        csvParser.SetDelimiters(delimiters)
        csvParser.HasFieldsEnclosedInQuotes = True
        While Not csvParser.EndOfData
            ListFields = csvParser.ReadFields().ToList


' Your logic for handling fields in a record goes here
' the fields of the current record are in ListFields


        End While
    End Using
End Sub
  

DigitalDan.co.uk