Writing a prn style file in vb .net

  • Thread starter Thread starter simonc
  • Start date Start date
S

simonc

I want to output an ascii file of numbers with all the
figures in ordered right justified columns, rather like an
Excel "Formatted Space Delimited" .prn file.

I can't find any reference to right justifying in any of
the format options.

Grateful for any advice on how to do this.
 
simonc said:
I want to output an ascii file of numbers with all the
figures in ordered right justified columns, rather like an
Excel "Formatted Space Delimited" .prn file.

I can't find any reference to right justifying in any of
the format options.

Grateful for any advice on how to do this.

There are several ways to accomplish this. One would be to use an ODBC
connection, or a Structure Object with fixed lenght strings, but the
quickest & dirtiest would be to just pad the numbers and write them out as a
string. You can do this by using a String or StringBuilder object
(StringBuilder is faster for large files) and the String.PadLeft( )
function. It should look something like this when you are done:

Dim FilePath as String = "C:\test.txt"
Dim FieldCount as Integer = 10
Dim RowCount as Integer = 13
Dim Data(RowCount, FieldCount) as Integer
Dim MaxFieldWidth as Integer = 10
Dim i, x as Integer
Dim s as New System.Text.StringBuilder((MaxFieldWidth * FieldCount *
RowCount) + (RowCount * 2))

For i = 0 to RowCount

For x = 0 to FieldCount
'// write each field out as a fixed length
s.Append(data(i, x).ToString.PadLeft(MaxFieldWidth)
Next

'// Add a new line to indicate the next record
s.Append(ControlChars.CrLf)
Next

'// now just write out the string to a text file
Dim ofs As New System.IO.FileStream(FilePath, IO.FileMode.OpenOrCreate,
IO.FileAccess.Write)
Dim swr As New System.IO.StreamWriter(ofs)

swr.Write(s.ToString)

Call swr.Close( )
Call ofs.Close( )

HTH,
Jeremy
 
simonc said:
I want to output an ascii file of numbers with all the
figures in ordered right justified columns, rather like an
Excel "Formatted Space Delimited" .prn file.

I can't find any reference to right justifying in any of
the format options.

You can use 'String.PadLeft' to fill the space on the left side of a
string to a certain length.
 
you can use the String.Format method
within each {} the first number is the agument number, the second number is
the column width (pos is right align, neg is left align.)

here is a sample (you might want to change you font to a fixed width like
courier to see it properly)

Dim str As String
str = String.Format("{0,-13} {1,15}", txtCol1.Text, txtCol2.Text)
Me.txtResult.Text &= vbNewLine & str

Kirk Graves
 
Kirk said:
you can use the String.Format method
within each {} the first number is the agument number, the second number is
the column width (pos is right align, neg is left align.)

here is a sample (you might want to change you font to a fixed width like
courier to see it properly)

Dim str As String
str = String.Format("{0,-13} {1,15}", txtCol1.Text, txtCol2.Text)
Me.txtResult.Text &= vbNewLine & str


nice solution.
 
Back
Top