Writing a prn style file in vb .net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • simonc

    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.


  • Jeremy Cowles

    #2
    Re: Writing a prn style file in vb .net

    "simonc" <sc@mail.com> wrote in message
    news:095401c388 4e$e34c9760$a30 1280a@phx.gbl.. .[color=blue]
    > 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.[/color]

    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.tx t"
    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.Str ingBuilder((Max FieldWidth * 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.Pad Left(MaxFieldWi dth)
    Next

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

    '// now just write out the string to a text file
    Dim ofs As New System.IO.FileS tream(FilePath, IO.FileMode.Ope nOrCreate,
    IO.FileAccess.W rite)
    Dim swr As New System.IO.Strea mWriter(ofs)

    swr.Write(s.ToS tring)

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

    HTH,
    Jeremy

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Writing a prn style file in vb .net

      "simonc" <sc@mail.com> scripsit:[color=blue]
      > 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.[/color]

      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>

      Comment

      • Kirk

        #4
        Re: Writing a prn style file in vb .net

        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.Te xt &= vbNewLine & str

        Kirk Graves

        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        news:O%23lCEeGi DHA.1684@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > "simonc" <sc@mail.com> scripsit:[color=green]
        > > 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.[/color]
        >
        > 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[/color]


        Comment

        • Jeremy Cowles

          #5
          Re: Writing a prn style file in vb .net

          "Kirk" <mcsdsmurf@hotm ail.com> wrote in message
          news:%23erJO7Qi DHA.1864@tk2msf tngp13.phx.gbl. ..[color=blue]
          > you can use the String.Format method
          > within each {} the first number is the agument number, the second number[/color]
          is[color=blue]
          > 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.Te xt &= vbNewLine & str[/color]


          nice solution.


          Comment

          Working...