• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a CVS File in VB.Net

CoolGuy596

System Diagnostics Analyst
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
112
Likes
177
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Let’s learn how to create CSV file in Visual Basic 2015. CSV file is a Comma Separated Value that is a commonly used format in text editor such as Microsoft Excel, OpenOffice Calc, notepad and Google docs. It is a plain text file that contains a list of data record. Each of its data record consist one or more fields that are separated by commas. So, in this tutorial, I’m going to teach you how to read and write the CSV file in a simple way. Just follow the steps below to see how it works.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in visual basic.
creating_vb.png

Step 2

Add a Button into the form.
2019-01-29.png

Step 3

Create a method to read and write a CSV file using StreamWriter and StreamReader.
  1. Public

    Sub

    WriteCSVfile()
  2. Try
  3. 'A TextReader is use to read character from a byte in a particular encoding
  4. Dim

    stReader As

    StreamReader = New

    System.IO.StreamReader(File.OpenRead("c:\CSV\Testing.csv"

    ))

  5. 'It initialize the instance of List(of String) class that has the default initial capacity
  6. Dim

    strList As

    New

    List(Of String

    )()

  7. 'It validates whether the particular file is already exist
  8. If

    File.Exists("c:\CSV\TestingOut.csv"

    ) Then
  9. 'Deleting the file
  10. File.Delete("c:\CSV\TestingOut.csv"

    )
  11. End

    If

  12. 'For the specified file, you have to initialize a new instance of the StreamWriter class
  13. 'for encoding and buffering the size
  14. Dim

    stWriter As

    New

    StreamWriter("c:\CSV\TestingOut.csv"

    )
  15. 'Declare an empty string variable
  16. Dim

    str As

    String

    = String

    .Empty

  17. While

    stReader.Peek() >= 0
  18. Dim

    strline As

    String

    = stReader.ReadLine()
  19. Dim

    strvalues As

    String

    () = strline.Split(";"

    c)
  20. strList.Add(strvalues(0))
  21. str = str + strline + Chr(10)
  22. End

    While

  23. 'It will close the StreamReader object and the underlying stream
  24. 'It releases any system resources associate the reader
  25. stReader.Close

    ()
  26. 'It will write a string to the stream
  27. stWriter.Write(str)
  28. 'It will close the current object and the underlying stream
  29. stWriter.Close

    ()

  30. Catch ex As

    Exception
  31. 'Catching errors
  32. MessageBox.Show(ex.Message)
  33. End

    Try

  34. End

    Sub

Step 4

Double click a button to fire the click event handler

of it and do the following codes for reading and writing CSV file in the specified path when the button is clicked.
  1. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  2. WriteCSVfile()
  3. End

    Sub

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

 

452,292

323,526

323,535

Top