VB.NET || How To Parse A Delimited CSV File Using VB.NET
The following is a module with functions which demonstrates how to parse a delimited CSV file using VB.NET.
The function demonstrated on this page uses FileIO.TextFieldParser to parse values in a CSV file.
This function parses a CSV file and returns its results as a List. Each List index represents a line in the CSV file, with each item in the list representing a record contained on that line.
1. Parse CSV File
The example below demonstrates the use of ‘Utils.ParseCsv‘ to parse a CSV file and return its results as a List.
The optional function parameter allows you to specify the delimiters. Default delimiter is a comma (,).
Sample CSV used in this example is the following:
123456
Kenneth,Perkins,120 jefferson st.,Riverside, NJ, 08075Jack,McGinnis,220 hobo Av.,Phila, PA,09119"Jennifer ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234,Blankman,,SomeTown, SD, 00298"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
' Parse CSV File ' Read file into byte arrayDim fileBytes As Byte() ' Parse the contents of the file into a listDim fileContents = Utils.ParseCsv(fileBytes) ' Display the contents of the fileFor lineIndex = 0 To fileContents.Count - 1 Dim line = fileContents(lineIndex) Debug.Print($"Line #{lineIndex + 1}") For Each item In line Debug.Print($" Item: {item}") NextNext ' expected output:' Line #1' Item: Kenneth' Item: Perkins' Item: 120 jefferson st.' Item: Riverside' Item: NJ' Item: 08075' Line #2' Item: Jack' Item: McGinnis' Item: 220 hobo Av.' Item: Phila' Item: PA' Item: 09119' Line #3' Item: Jennifer "Da Man"' Item: Repici' Item: 120 Jefferson St.' Item: Riverside' Item: NJ' Item: 08075' Line #4' Item: Stephen' Item: Tyler' Item: 7452 Terrace "At the Plaza" road' Item: SomeTown' Item: SD' Item: 91234' Line #5' Item: ' Item: Blankman' Item: ' Item: SomeTown' Item: SD' Item: 00298' Line #6' Item: Joan "the bone", Anne' Item: Jet' Item: 9th, at Terrace plc' Item: Desert City' Item: CO' Item: 00123
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
12345678910111213141516171819202122232425262728293031323334353637383940
' ============================================================================' Author: Kenneth Perkins' Date: Dec 2, 2020' Taken From: http://programmingnotes.org/' File: Utils.vb' Description: Handles general utility functions' ============================================================================Option Strict OnOption Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Parses a Csv file and returns its results as a List. ''' Each List index represents a line in the Csv file, with each ''' item in the list representing a record contained on that line. ''' </summary> ''' <param name="fileBytes">The Csv file as a byte array</param> ''' <param name="delimiters">The Csv data delimiter</param> ''' <returns>The file contents as a List</returns> Public Function ParseCsv(fileBytes As Byte() _ , Optional delimiters As String = ",") As List(Of List(Of String)) Dim results = New List(Of List(Of String)) Using stream = New System.IO.MemoryStream(fileBytes) Using parser = New Microsoft.VisualBasic.FileIO.TextFieldParser(stream) parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited parser.Delimiters = delimiters.Select(Function(letter) letter.ToString).ToArray parser.HasFieldsEnclosedInQuotes = True ' Parse each line in the file While Not parser.EndOfData Dim currentLine = parser.ReadFields results.Add(currentLine.ToList) End While End Using End Using Return results End Function End ModuleEnd Namespace ' http://programmingnotes.org/
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1234567891011121314151617181920212223242526272829303132333435363738394041
' ============================================================================' Author: Kenneth Perkins' Date: Dec 2, 2020' Taken From: http://programmingnotes.org/' File: Program.vb' Description: The following demonstrates the use of the Utils Namespace' ============================================================================Option Strict OnOption Explicit OnImports System Public Module Program Sub Main(args As String()) Try ' Read file into byte array Dim fileBytes As Byte() ' Parse the contents of the file into a list Dim fileContents = Utils.ParseCsv(fileBytes) ' Display the contents of the file For lineIndex = 0 To fileContents.Count - 1 Dim line = fileContents(lineIndex) Display($"Line #{lineIndex + 1}") For Each item In line Display($" Item: {item}") Next Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End SubEnd Module ' http://programmingnotes.org/
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.






Leave a Reply