2.Converting data in a table into .csv (comma separated) file

sathya_k_83

Member
Joined
Jan 12, 2007
Messages
5
Programming Experience
Beginner
Converting data in a table into .csv (comma separated) file

Converting data in a table into .csv (comma separated) file


How to create an .csv (comma separated) file using an VB.net programming
ie
converting data in a table into csv (comma separated) file
the by getting a data set & converting the values in a table into a .csv file using an VB.net coding
 
here is a way by looping the rows

It takes care of handling the fields so you don't have to. Watch out for embedded commas in your source data though. Maybe use TAB delimiter instead

Sub Main()
Dim oFileStream As IO.StreamWriter = IO.File.CreateText("C:\TheFile.csv")
Dim oTable AsNew DataTable
oTable.Columns.Add("ID", Type.GetType("System.String"))
oTable.Columns.Add("DESC", Type.GetType("System.String"))
oTable.Columns.Add("NOTES", Type.GetType("System.String"))

Dim oRow As DataRow
oRow = oTable.NewRow
oRow("ID") = 9
oRow("DESC") = "The 9 Description"
oRow("NOTES") = "The 9 notes"
oTable.Rows.Add(oRow)

oRow = oTable.NewRow
oRow("ID") = 10
oRow("DESC") = "The 10 Description"
oRow("NOTES") = "The 10 notes"
oTable.Rows.Add(oRow)

ForEach oDataRow As DataRow In oTable.Rows
...Dim vList(oDataRow.ItemArray.Length - 1) AsString
...oDataRow.ItemArray.CopyTo(vList, 0)
...oFileStream.WriteLine(String.Join(",", vList))
Next

oFileStream.Close()
oFileStream = Nothing
EndSub
 
Back
Top