Forum Moderators: open

Message Too Old, No Replies

Write CSV File in vb

         

andrewsmd

3:39 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to write a couple of columns to a csv or possibly xls file. I cannot find any good tutorials on this. Does anyone have one? Thanks,

LifeinAsia

3:47 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



This seems like a good set of tutorials: http://msdn.microsoft.com/en-us/library/c520bdhb(VS.80).aspx [msdn.microsoft.com]

marcel

4:46 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is a simple example, using a list of custom objects (person)

The list itself is empty in the code below , but you should get the general idea. This will also work with a DataReader or DataSet.

Dim myList As New List(Of Person)
Dim sw As New StreamWriter(Server.MapPath("~/myFile.csv"))
For Each Person As Person In myList
sw.WriteLine(String.Format("{0},{1},{2}", Person.Name, Person.Surname, Person.DateOfBirth.ToShortDateString))
Next
sw.Close()

*Edit: for reading CSV files you can use the Microsoft.VisualBasic.FileIO.TextFieldParser namespace

andrewsmd

3:12 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you both for your help.