Question writing fixed length text file

vinwhaay

New member
Joined
May 20, 2013
Messages
1
Programming Experience
Beginner
I am struggling to find a way to write text file with various fixed length fields from a dataset

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.ExportTableAdapter.Fill(Me.NewDataSet.Export)


'write to text file


Dim sw As New System.IO.StreamWriter("C:\temp\FixedWidth.txt")
Dim city As System.Text.StringBuilder

every method ive used simply writes all the data without spacing or i have seen methods using csv, but thats not what im after either

i need to identify each column and assign it a set width e.g. first_name (20 spaces) surname (30 spaces)

any helps appeciated
 
Check this out:

Formatting Fixed-width Text Output

You can use a StringBuilder as I have done there and then call File.WriteAllText to write out in one go, or you can create a StreamWriter and call its WriteLine method. The second option allows you to combine the formatting and adding the line break in one go. Either way, you'll want to use a For Each loop to go through the rows of the DataTable.
 

Latest posts

Back
Top