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