Save datatable as a delimited text file

  • Thread starter Thread starter CFNewbie
  • Start date Start date
C

CFNewbie

I am working through a problem and need to create a file that is compatible
with another program. I have not seen any examples that take a datatable and
create a delimited text file. I assume you use the streamwriter function, but
not sure how to access the data in the datatable to create the text file.
 
Hi,
Try following logic. You will need to assign value to datatable
before writing to text file.
Dim dt As DataTable
' Assign values to DataTable here
Dim _txt As String
Dim FieldDelimiter As String = "|"
Dim i, j As Integer
For j = 0 To (dt.Rows.Count - 1)
For i = 0 To (dt.Columns.Count - 1)
_txt = _txt + dt.Rows(j).Item(i) + FieldDelimiter
Next
_txt = _txt.Substring(1, _txt.Length)
_txt = _txt + ControlChars.NewLine
Next
Dim w As New StreamWriter("data.txt", False)
w.Write(_txt.Trim) ' Remove trailing space and write to file
w.Flush()
w.Close()

Regards,
Bipin Kesharwani
(Developer)
Palewar Techno Solutions
Pocket PC & Mobile Software Development
Nagpur, India

http://www.palewar.com
 
Back
Top