CSV converter Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

CSV converter


Objetive:

The CSV ("Comma Separated Values") is an exchange format used by many spreadsheet and database management systems. It consists of a series of comma-separated values enclosed in quotation marks, although there are variants that do not use quotes or use semicolons as separators. Often, the values are not enclosed in quotes. An example file would be:

"John", "López Pérez," "Alicante", 25
"Antonio", "Pérez López", "Madrid", 27

You should create a program that reads a CSV file as shown above, with four data blocks (the first three are text and the last one is numeric), each of which is on a separate line. The program should generate a text file where each entry contains a line like this:

John
Pérez López
Alicante
25
Antonio
Pérez López
Madrid
27


Code:

Imports System
Imports System.IO
Namespace ReaderCSV
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Console.WriteLine("CSV READER")
            Console.WriteLine("--------------------------------------")
            Console.Write("Enter name of file .csv: ")
            Dim nameFile As String = Console.ReadLine()
            Dim myfile As StreamReader
            Dim cadena As String
            Dim position As Integer

            Try
                myfile = New StreamReader(nameFile, System.Text.Encoding.[Default])
                Dim line As String

                Do
                    line = myfile.ReadLine()

                    If (line IsNot Nothing) AndAlso (line <> "") Then

                        Try
                            line = line.Replace("""", "")
                            position = line.IndexOf(";"c)
                            cadena = line.Substring(0, position)
                            line = line.Remove(0, position + 1)
                            Console.WriteLine(cadena & vbLf)
                            position = line.IndexOf(";"c)
                            cadena = line.Substring(0, position)
                            line = line.Remove(0, position + 1)
                            Console.WriteLine(cadena & vbLf)
                            position = line.IndexOf(";"c)
                            cadena = line.Substring(0, position)
                            line = line.Remove(0, position + 1)
                            Console.WriteLine(cadena & vbLf)
                            cadena = line.Substring(0)
                            Console.WriteLine(cadena & vbLf)
                        Catch e As Exception
                            Console.WriteLine("Error !!! " & e.Message)
                        End Try
                    End If
                Loop While line IsNot Nothing

            Catch e As Exception
                Console.WriteLine("Error !!!" & e.Message)
            End Try
        End Sub
    End Class
End Namespace

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.