Results 1 to 8 of 8

Thread: Writing from lstbox to HTML file.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Writing from lstbox to HTML file.

    Hi guys,
    New here and also fairly new to VB
    Was just wondering how you can write multiple values from a list box to a html file.
    I have 6 list boxes. 1 - 2 - 3 - 4 - 5 - 6
    and would like the date to go like this:
    1 - 2 - 3 - 4 - 5 - 6
    1 - 2 - 3 - 4 - 5 - 6
    1 - 2 - 3 - 4 - 5 - 6
    1 - 2 - 3 - 4 - 5 - 6 etc.

    Any help would be appreciated guys,
    Rob.

    Currently Have:
    Code:
     Dim sw_HTMLwrite As StreamWriter
            Dim sHtmlReport As String
            Dim sOfficeId As String
            Dim sb As New System.Text.StringBuilder
            Dim sline As String
            Dim sConsNumber As String
            Dim sShipMethod As String
            Dim sExpDepature As String
            Dim sExpArrival As String
            Dim sShipCost As String
            Dim sShipDuration As String
    
    
            For i As Integer = 0 To Me.lbxShippingConsignmentNumber.Items.Count - 1
                sConsNumber = lbxShippingConsignmentNumber.Items(i)
                sShipMethod = lbxShipmentMethod.Items(i)
                sExpDepature = lbxExpectedDepartureDate.Items(i)
                sExpArrival = lbxExpectedArrivalTime.Items(i)
                sShipCost = lbxShipmentCost.Items(i)
                sShipDuration = lbxShipmentDuration.Items(i)
    
                sline = "<TR><TD>" & sConsNumber & "</TD><TD>" & sShipMethod & "</TD></TR>" & sExpDepature & "</TD></TR>" & sExpArrival & "</TD></TR>" & sShipCost & "</TD></TR>" & sShipDuration & "</TD></TR>"
                sOfficeId = lblOfficeIDOutput.Text
    
                sHtmlReport = ("C:\Users\Rob\University\Semester 1 - 2010\Business Information Systems\TPECRS SAMPLE\Office Consignment File -" + sOfficeId + ".html")
                sw_HTMLwrite = New StreamWriter(sHtmlReport)
    
                sw_HTMLwrite.WriteLine("<HTML>")
    
                sw_HTMLwrite.WriteLine("<HEAD>")
                sw_HTMLwrite.WriteLine("<TITLE>Consignment Report - Office " + sOfficeId + "</TITLE>")
                sw_HTMLwrite.WriteLine("</HEAD>")
    
                sw_HTMLwrite.WriteLine("<BODY>")
                sw_HTMLwrite.WriteLine("<BODY bgcolor=Blue>")
                sw_HTMLwrite.WriteLine("<h1>Consignment Report - Office " + sOfficeId + "</h1>")
                sw_HTMLwrite.WriteLine("<TABLE BORDER=1>")
                sw_HTMLwrite.WriteLine("<TR><TH> Consignment Number </TH><TH> Shipment Method </TH><TH> Exp Deptature Date </TH><TH> Exp Arrival Date </TH><TH> Shipment Cost </TH><TH> Shipment Duration </TH></TR>")
                sw_HTMLwrite.WriteLine(sline)
    
                sw_HTMLwrite.WriteLine("</TABLE>")
                sw_HTMLwrite.WriteLine("</BODY>")
                sw_HTMLwrite.WriteLine("</HTML>")
    
                sw_HTMLwrite.Close()
    
            Next
    but is only saving one line (the last data of the list box)
    Last edited by Rmckenna; May 24th, 2010 at 11:46 PM. Reason: To display code/not double post.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Writing from lstbox to HTML file.

    You're writing <HTML>...<BODY> (your data1) </HTML> then again <HTML> ... your data2 </HTML> etc.
    As soon as browser hits </HTML> tag it stops reading.
    You only need putting in a cycle the table cells, nothing more. Everything else should be outside FOR...NEXT

    vb Code:
    1. Dim sw_HTMLwrite As StreamWriter
    2. Dim sHtmlReport As String
    3. Dim sOfficeId As String
    4. Dim sb As New System.Text.StringBuilder
    5. Dim sline As String
    6. Dim sConsNumber As String
    7. Dim sShipMethod As String
    8. Dim sExpDepature As String
    9. Dim sExpArrival As String
    10. Dim sShipCost As String
    11. Dim sShipDuration As String
    12.  
    13. sHtmlReport = ("C:\Users\Rob\University\Semester 1 - 2010\Business Information Systems\TPECRS SAMPLE\Office Consignment File -" + sOfficeId + ".html")
    14.  
    15. sw_HTMLwrite = New StreamWriter(sHtmlReport)
    16. sw_HTMLwrite.WriteLine("<HTML>")
    17. sw_HTMLwrite.WriteLine("<HEAD>")
    18. sw_HTMLwrite.WriteLine("<TITLE>Consignment Report - Office " + sOfficeId + "</TITLE>")
    19. sw_HTMLwrite.WriteLine("</HEAD>")
    20.  
    21. sw_HTMLwrite.WriteLine("<BODY>")
    22. sw_HTMLwrite.WriteLine("<BODY bgcolor=Blue>")
    23. sw_HTMLwrite.WriteLine("<h1>Consignment Report - Office " + sOfficeId + "</h1>")
    24. sw_HTMLwrite.WriteLine("<TABLE BORDER=1>")
    25. sw_HTMLwrite.WriteLine("<TR><TH> Consignment Number </TH><TH> Shipment Method </TH><TH> Exp Deptature Date </TH><TH> Exp Arrival Date </TH><TH> Shipment Cost </TH><TH> Shipment Duration </TH></TR>")
    26.  
    27.         '  Only this part requires a loop. The rest (see above and below the loop)
    28.         '  should be called only once.
    29.  
    30. For i As Integer = 0 To Me.lbxShippingConsignmentNumber.Items.Count - 1
    31.     sConsNumber = lbxShippingConsignmentNumber.Items(i)
    32.     sShipMethod = lbxShipmentMethod.Items(i)
    33.     sExpDepature = lbxExpectedDepartureDate.Items(i)
    34.     sExpArrival = lbxExpectedArrivalTime.Items(i)
    35.     sShipCost = lbxShipmentCost.Items(i)
    36.     sShipDuration = lbxShipmentDuration.Items(i)
    37.    
    38.     sline = "<TR><TD>" & sConsNumber & "</TD><TD>" & sShipMethod & "</TD></TR>" & sExpDepature & "</TD></TR>" & sExpArrival & "</TD></TR>" & sShipCost & "</TD></TR>" & sShipDuration & "</TD></TR>"
    39.  
    40.     sOfficeId = lblOfficeIDOutput.Text
    41.  
    42.            
    43.     sw_HTMLwrite.WriteLine(sline)
    44.  
    45. Next
    46.  
    47.  
    48. sw_HTMLwrite.WriteLine("</TABLE>")
    49. sw_HTMLwrite.WriteLine("</BODY>")
    50. sw_HTMLwrite.WriteLine("</HTML>")
    51.  
    52. sw_HTMLwrite.Close()

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Writing from lstbox to HTML file.

    Thank you so much!

  4. #4
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Writing from lstbox to HTML file.

    < off-topic question removed by moderator >
    Last edited by si_the_geek; May 25th, 2010 at 05:29 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Writing from lstbox to HTML file.

    One last thing actually, how can I make it so if the file path already exists, it ADDS to that file, rather than overwriting? Cheers.

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Writing from lstbox to HTML file.

    Quote Originally Posted by Rmckenna View Post
    One last thing actually, how can I make it so if the file path already exists, it ADDS to that file, rather than overwriting? Cheers.
    Generally, you can, but this won't help you in this particular case.
    To open file in append mode you should use this overload:

    Code:
    Dim sw_HTMLwrite As New IO.StreamWriter(New IO.FileStream(sHtmlReport, IO.FileMode.Append))
    Note IO.FileMode.Append

    But in this case merely adding something below a html file which has structure will repeat the problem you had in the very beginning.

    The only valid portion of a HTML file is the one that resides between <HTML>...</HTML> tags. Anything that is past </HTML> will be ignored.

    In your case you already have a closing </HTML> tag at the end of the file so writing past it is futile because no browser will recognize it.


    @coccoster: Make a new thread with your question.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Writing from lstbox to HTML file.

    Thank you for your help, cicatrix, truly appreciate it.

  8. #8
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Writing from lstbox to HTML file.

    done mate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width