How to Write to a CSV File on VB.NET

Updated July 19, 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:


Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Jim Campbell has been a computer engineer for over five years. He excels in hardware repair, computer programming and troubleshooting, and software design. He is currently attending Florida Atlantic University, pursuing a master's degree in computer and electrical engineering and fine-tuning his technical writing abilities.