How to Write to a CSV File on VB.NET

Written by jim campbell | 13/05/2017

Comma delimited files (CSV) allow administrators to transfer data between different platforms. You can write to a CSV file using VB.NET so that you can export data to import to another database application. CSV files contain the plain text format, so you can write to a text file and name it with a CSV extension in VB.NET.

Click the Windows "Start" button and select "All Programs." Click ".NET Framework" and then click the "Visual Studio" shortcut to open the software.

Double-click the VB.NET project file to open the project in the development software. In "Solution Explorer," right-click the file you want to edit and select "View Code."

Create the variable for the CSV file. Type the following code in the editor:

Dim file As String = "C:\myfolder\Test.csv"

Replace the string content with the location and name of the CSV file you want to create.

Link the new file variable to the "output" stream. The output stream variable prepares the file so that you can write to it from your program. Type the following code:

Dim out As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(file, False)

Write data to the CSV file. The following code shows you how to write to the CSV file:

out.WriteLine("data 1, data 2, data 3")

Replace the content in the parenthesis with your own data. Repeat this step until you write all the data you want to save to the CSV file.

Close the file. After you finish writing to the file, you must close it in the code. Add the following code to close the file:


By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.