Results 1 to 9 of 9

Thread: How to convert HTML Table to text file?

  1. #1

    Thread Starter
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    How to convert HTML Table to text file?

    Hello Everybody,
    Can someone tell me if it is possible to convert html table to text file?

    the table in the text file will look something like that:
    VB Code:
    1. ========================================
    2. |                                                                      |
    3. |                                                                      | |                                                                      | |                                                                      | |                                                                      | ========================================
    imagine there are cells in it...

    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to convert HTML Table to text file?

    yes it is possible, but you will have to format the cells with spaces and/or tabs

    pete

  3. #3
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: How to convert HTML Table to text file?

    ok.. here is a sample

    new project
    add a reference to the Microsoft HTML Object Library
    add a textbox (Kinda big)
    make it multiline
    i created a simple table in an html doc..
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim HTML As HTMLDocument
    3.     Dim tHTML As New HTMLDocument
    4.     Dim TBL As HTMLTable
    5.     Dim ROW As HTMLTableRow
    6.     Dim COL As HTMLTableCol
    7.     Dim CELL As HTMLTableCell
    8.     Dim tmp As String
    9.     Set HTML = tHTML.createDocumentFromUrl("c:\table.html", vbNullString)
    10.     Do While HTML.readyState <> "complete"
    11.         DoEvents
    12.     Loop
    13.     Text1 = ""
    14.     For Each TBL In HTML.getElementsByTagName("table")
    15.         For Each ROW In TBL.rows
    16.             tmp = ""
    17.             For Each CELL In ROW.cells
    18.                 tmp = tmp & "| " & Space(4) & CELL.innerText & Space(4)
    19.             Next
    20.             Text1 = Text1 & tmp & "|" & vbCrLf
    21.             Text1 = Text1 & "|" & String(Len(tmp) - 1, "=") & "|" & vbCrLf
    22.         Next
    23.     Next
    24.    
    25. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: How to convert HTML Table to text file?

    Hello Static,
    Thank you for your reply,
    But look at this HTML Table:
    HTML Code:
    <html>
    <body>
    <table border=5 cellspacing=1 cellpadding=2>
    <tr align=center>
    <td>this is a simple cell width</td><td>2</td><td>3</td><td>4</td>
    </tr>
    <tr align=center>
    <td colspan=2>this cells width is like 2 simple cells</td><td>3</td><td>4</td>
    </tr>
    <tr align=center>
    <td colspan=3>this cells width is like 3 simple cells</td><td>4</td>
    </tr>
    <tr align=center>
    <td colspan=4>this cells width is like 4 simple cells</td>
    </tr>
    <tr align=center>
    <td colspan=4 bgcolor=silver><hr size=3 width=300></td>
    </tr>
    <tr align=center>
    <td>1</td><td rowspan=2>this cells height is like 2 simple cells</td>
    <td rowspan=3>this cells height is like 3 simple cells</td>
    <td rowspan=4>this cells height is like 4 simple cells</td>
    </tr>
    <tr align=center>
    <td>1</td>
    </tr>
    <tr align=center>
    <td>1</td><td>2</td>
    </tr>
    <tr align=center>
    <td>1</td><td>2</td><td>3</td>
    </tr>
    </table>
    </body>
    </html>
    in the text box it looks like this:

    | this is a simple cell width | 2 | 3 | 4 |
    |=====================================================================|
    | this cells width is like 2 simple cells | 3 | 4 |
    |======================================================================|
    | this cells width is like 3 simple cells | 4 |
    |===========================================================|
    | this cells width is like 4 simple cells |
    |================================================|
    | |
    |=========|
    | 1 | this cells height is like 2 simple cells | this cells height is like 3 simple cells | this cells height is like 4 simple cells |
    |=================================================================================================== =============================================================|
    | 1 |
    |==========|
    | 1 | 2 |
    |=====================|
    | 1 | 2 | 3 |
    |================================|
    Any Ideas?
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to convert HTML Table to text file?

    you can format the text to give you table like spacing using code, for output to file the following would change

    | this is a simple cell width | 2 | 3 | 4 |

    to

    edit: this page takes out all the extra spaces, so i can't show here


    though it does not look right on this page

    VB Code:
    1. mytest = " | this is a simple cell width | 2 | 3 | 4 |"
    2. arrfi = Split(mytest, "|")
    3.     li = Len(arrfi(1))
    4.     clist = arrfi(1)
    5.     For i = 2 To UBound(arrfi) - 1
    6.         If Len(arrfi(i)) > li Then li = Len(arrfi(i)): clist = arrfi(i)
    7.     Next
    8.         Open "test.txt" For Output As 1
    9.  
    10.     For i = 1 To UBound(arrfi) - 1
    11.         If Len(arrfi(i)) < li Then
    12.              myind = Space((Len(clist) - Len(arrfi(i))) / 2)
    13.              arrfi(i) = myind & arrfi(i) & myind
    14.         End If
    15.        
    16.    Next
    17.     Print #1, Join(arrfi, "|")
    18.     Close 1

    for the output to look correct in a text box, you would have to use textwidth to work out the number of spaces for that particular font.

    you would also need to work out how it will fit in the width of your textbox., it might be easier to print it to a form or picturebox

    pete
    Last edited by westconn1; Jul 21st, 2006 at 04:45 AM.

  6. #6
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: How to convert HTML Table to text file?

    oh, i forgot to say change the font to COurier New or another Fixed Width FOnt...

    but.. ok, so it needs some work... lemme see what I can figure out.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: How to convert HTML Table to text file?

    Hello Again,
    The problem will always be the blanks...
    if i'll know the numer of blanks then i will know how many "spaces" to add to every cell...

    I tried the courier new but no luck...

    Thank you both,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  8. #8
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: How to convert HTML Table to text file?

    well this was closer... but I cant get it quite right.. and I can get the rowspan ones to work...
    sorry..
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim HTML As HTMLDocument
    3.     Dim tHTML As New HTMLDocument
    4.     Dim TBL As HTMLTable
    5.     Dim ROW As HTMLTableRow
    6.     Dim COL As HTMLTableCol
    7.     Dim CELL As HTMLTableCell
    8.     Dim tmp As String
    9.     Dim LongLen As Integer
    10.     Dim iSPC As Integer
    11.    
    12.     Set HTML = tHTML.createDocumentFromUrl("c:\table.html", vbNullString)
    13.     Do While HTML.readyState <> "complete"
    14.         DoEvents
    15.     Loop
    16.     Text1 = ""
    17.     For Each TBL In HTML.getElementsByTagName("table")
    18.         For Each ROW In TBL.rows
    19.             For Each CELL In ROW.cells
    20.                 If LongLen < Len(CELL.innerText) Then LongLen = Len(CELL.innerText)
    21.             Next
    22.         Next
    23.     Next
    24.     For Each TBL In HTML.getElementsByTagName("table")
    25.         For Each ROW In TBL.rows
    26.             tmp = ""
    27.             For Each CELL In ROW.cells
    28.                 iSPC = ((LongLen * CELL.colSpan) - Len(CELL.innerText)) / 2
    29.                 iSPC = iSPC + ((CELL.colSpan - 1) * 1)
    30.                 tmp = tmp & "|" & Space(iSPC) & CELL.innerText & Space(iSPC)
    31.             Next
    32.             Text1 = Text1 & tmp & "|" & vbCrLf
    33.             Text1 = Text1 & "|" & String(Len(tmp) - 1, "=") & "|" & vbCrLf
    34.         Next
    35.     Next
    36.    
    37. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: How to convert HTML Table to text file?

    Hello Static,
    Thank you for your Time and Efforts,
    I think this is a very hard thing to do but it is possible - just need to figure it out.

    Thank you a LOT!!!
    If you'll have a new idea i will be happy to hear from you,

    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

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