473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Writing a prn style file in vb .net

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.
Nov 20 '05 #1
4 9548
"simonc" <sc@mail.com> wrote in message
news:09****************************@phx.gbl...
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

Nov 20 '05 #2
"simonc" <sc@mail.com> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
"simonc" <sc@mail.com> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #4
"Kirk" <mc*******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daniel Cloutier | last post by:
Hi, is it possible to edit or write Word-files out of a Python-Program? thx in advance daniel
6
by: Jeff Thies | last post by:
I've been thinking about writing CSS that's easy to read and adjust at a later date (often by someone else). Here's where I seem to be headed: Breaking the page down into container divs,...
3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
27
by: hpy_awad | last post by:
I wrote that program from a book that my compile that program correctly under C++ compiler but I got those errors for compiling under unix !! Errors- --------...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
3
by: VMI | last post by:
I'm having trouble writing several records from an XML file into an html file. The problem is that it only outputs one record to the html file. I need to export all the records in the xml file to...
2
by: Kenneth P | last post by:
Hi, I'm developing asp.net (vb) apps in VS.NET 2003, and I'd like to know how you can write style sheet code in a sub in an .aspx page that when it's rendered as html/text/css code in the .aspx...
5
by: Iceman | last post by:
I am logging things to a file but when I kill the app and restart it, all the other logging is gone. How can I prevent this form happening?
6
by: Mizipzor | last post by:
Hi, this is my first mail to the list (and any list for that matter) so any pointers on errors from my part would be appreciated. Im more used to forums. To start off, Ive read some python...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.