Android Question [B4X] Print a B4XTable - export to html

mike1967

Active Member
Licensed User
Longtime User
Hello, i am a licensed user of EsyCode Form Generator where there is the possibility to print a a whole B4XTable. Can someone help me with sample code in order to make a printing of a whole B4XTable also via html without use this tool ? Thank In advances
 

PaulMeuris

Active Member
Licensed User
You might want to post a sample project using a B4XTable...
If you look at the data structure(s) used for the B4XTable, you might see how to proceed...
It involves using a loop (or two) to go over the data and then creating a HTML string...
That HTML string can be loaded in a webview, saved in a html-file , uploaded somewhere...
You could use a StringBuilder class to assemble the HTML string...
...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub ExportToHtml (tbl As B4XTable) As String
    Dim HtmlCSS As String = $"
        table {width: 100%;border: 1px solid #cef;text-align: left; }
        th { font-weight: bold;    background-color: #acf;    border-bottom: 1px solid #cef; }
        td,th {    padding: 4px 5px; }
        .odd {background-color: #def; } 
        .odd td {border-bottom: 1px solid #cef; }
        a { text-decoration:none; color: #000;}"$
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("<html><body>").Append(CRLF)
    sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
    sb.Append("<table><thead><tr>").Append(CRLF)
    For Each col As B4XTableColumn In tbl.Columns
        sb.Append("<th>").Append($"$Xml{col.Title}"$).Append("</th>")
    Next
    sb.Append("</thead>")
    sb.Append("</tr>").Append(CRLF)
    Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data")
    Dim row As Int
    Do While rs.NextRow
        If row Mod 2 = 0 Then
            sb.Append("<tr>")
        Else
            sb.Append("<tr class='odd'>")
        End If
        For i = 0 To rs.ColumnCount - 1
            sb.Append("<td>")
            sb.Append($"$Xml{rs.GetString2(i)}"$)
            sb.Append("</td>")
        Next
        sb.Append("</tr>").Append(CRLF)
        row = row + 1
    Loop
    rs.Close
    sb.Append("</table></body></html>")
    Return sb.ToString
End Sub

An example output is attached.
 

Attachments

  • table.zip
    58.1 KB · Views: 110
Upvote 0

mike1967

Active Member
Licensed User
Longtime User
B4X:
Private Sub ExportToHtml (tbl As B4XTable) As String
    Dim HtmlCSS As String = $"
        table {width: 100%;border: 1px solid #cef;text-align: left; }
        th { font-weight: bold;    background-color: #acf;    border-bottom: 1px solid #cef; }
        td,th {    padding: 4px 5px; }
        .odd {background-color: #def; }
        .odd td {border-bottom: 1px solid #cef; }
        a { text-decoration:none; color: #000;}"$
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("<html><body>").Append(CRLF)
    sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
    sb.Append("<table><thead><tr>").Append(CRLF)
    For Each col As B4XTableColumn In tbl.Columns
        sb.Append("<th>").Append($"$Xml{col.Title}"$).Append("</th>")
    Next
    sb.Append("</thead>")
    sb.Append("</tr>").Append(CRLF)
    Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data")
    Dim row As Int
    Do While rs.NextRow
        If row Mod 2 = 0 Then
            sb.Append("<tr>")
        Else
            sb.Append("<tr class='odd'>")
        End If
        For i = 0 To rs.ColumnCount - 1
            sb.Append("<td>")
            sb.Append($"$Xml{rs.GetString2(i)}"$)
            sb.Append("</td>")
        Next
        sb.Append("</tr>").Append(CRLF)
        row = row + 1
    Loop
    rs.Close
    sb.Append("</table></body></html>")
    Return sb.ToString
End Sub

An example output is attached.
Very,very thanks
 
Upvote 0

mike1967

Active Member
Licensed User
Longtime User
B4X:
Private Sub ExportToHtml (tbl As B4XTable) As String
    Dim HtmlCSS As String = $"
        table {width: 100%;border: 1px solid #cef;text-align: left; }
        th { font-weight: bold;    background-color: #acf;    border-bottom: 1px solid #cef; }
        td,th {    padding: 4px 5px; }
        .odd {background-color: #def; } 
        .odd td {border-bottom: 1px solid #cef; }
        a { text-decoration:none; color: #000;}"$
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("<html><body>").Append(CRLF)
    sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
    sb.Append("<table><thead><tr>").Append(CRLF)
    For Each col As B4XTableColumn In tbl.Columns
        sb.Append("<th>").Append($"$Xml{col.Title}"$).Append("</th>")
    Next
    sb.Append("</thead>")
    sb.Append("</tr>").Append(CRLF)
    Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data")
    Dim row As Int
    Do While rs.NextRow
        If row Mod 2 = 0 Then
            sb.Append("<tr>")
        Else
            sb.Append("<tr class='odd'>")
        End If
        For i = 0 To rs.ColumnCount - 1
            sb.Append("<td>")
            sb.Append($"$Xml{rs.GetString2(i)}"$)
            sb.Append("</td>")
        Next
        sb.Append("</tr>").Append(CRLF)
        row = row + 1
    Loop
    rs.Close
    sb.Append("</table></body></html>")
    Return sb.ToString
End Sub

An example output is attached.
How to print only the visible records if is set a CreateDataView ? Wath code for cicle in a current dataview ?Thanks in Advances
 
Upvote 0

mike1967

Active Member
Licensed User
Longtime User
You need the current DataView, just change erel's example

B4X:
Private Sub ExportToHtml (tbl As B4XTable) As String
    Dim HtmlCSS As String = $"
        table {width: 100%;border: 1px solid #cef;text-align: left; }
        th { font-weight: bold;    background-color: #acf;    border-bottom: 1px solid #cef; }
        td,th {    padding: 4px 5px; }
        .odd {background-color: #def; }
        .odd td {border-bottom: 1px solid #cef; }
        a { text-decoration:none; color: #000;}"$
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("<html><body>").Append(CRLF)
    sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
    sb.Append("<table>").Append(CRLF)
    'sb.Append("<table><thead><tr>").Append(CRLF)
    'For Each col As B4XTableColumn In tbl.Columns
    ''    sb.Append("<th>").Append($"$Xml{col.Title}"$).Append("</th>")
    'Next
    'sb.Append("</thead>")
    'sb.Append("</tr>").Append(CRLF)
    Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data")
    Dim row As Int
    Do While rs.NextRow
        If row Mod 2 = 0 Then
            sb.Append("<tr>")
        Else
            sb.Append("<tr class='odd'>")
        End If
        For i = 0 To rs.ColumnCount - 1
            sb.Append("<td>")
            sb.Append($"$Xml{rs.GetString2(i)}"$)
            sb.Append("</td>")
        Next
        sb.Append("</tr>").Append(CRLF)
        row = row + 1
    Loop
    rs.Close
    sb.Append("</table></body></html>")
    Return sb.ToString
End Sub
I not able to apply current DataView i not found any properties to read current dataview
 
Upvote 0

PaulMeuris

Active Member
Licensed User
You can change the ExportToHtml subroutine like this:
limit the query records:
    Dim startrow As Int = tbl.FirstRowIndex
    Dim numrows As Int = tbl.RowsPerPage
    Log("startrow: " & startrow)
    Log("numrows: " & numrows)
    Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data limit " & startrow & "," & numrows)
This code will take the records from the first (visible) rowindex and the next number of rows per page.
Try it, it might work...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I not able to apply current DataView i not found any properties to read current dataview
The code in post #9 will not work for you if you use a search string to filter the data to save your html file. If you want to make it work, one way would be to use string builder to construct a string with the IN operator clause and cycle through the visible rows in the B4XTable. Then, include that string in your select query syntax.
 
Upvote 0

mike1967

Active Member
Licensed User
Longtime User
The code in post #9 will not work for you if you use a search string to filter the data to save your html file. If you want to make it work, one way would be to use string builder to construct a string with the IN operator clause and cycle through the visible rows in the B4XTable. Then, include that string in your select query syntax.
Solved with:
Code:
Dim o() As Object = B4XTable1.BuildQuery(False) 'True to include the page LIMIT in the query.
Dim rs As ResultSet = B4XTable1.sql1.ExecQuery2(o(0), o(1))
Do While rs.NextRow
   Log(rs.GetString(B4XTable1.GetColumn("State").SQLID))
Loop
rs.Close
 
Upvote 0

PaulMeuris

Active Member
Licensed User
1664688323976.png

B4X:
    Dim obj() As Object = tbl.BuildQuery(True)
    Dim sql As String = obj(0)
    Log(sql)
    Dim values As List = obj(1)
    Log(values)
    Dim rs As ResultSet = tbl.sql1.ExecQuery2(sql,values)
EDIT: in B4A use:
B4X:
Dim values() As String = obj(1)
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
EDIT: in B4A use:
I am very familiar with BuildQuery. I use it a lot. It is nothing but a representation of a parameterized query of the table named data making up the in memory SQLite database. What I really wanted to know is: what do you have showing for obj(0) and obj(1) to solve the OP's question in post #5.
 
Upvote 0

PaulMeuris

Active Member
Licensed User
I am very familiar with BuildQuery. I use it a lot. It is nothing but a representation of a parameterized query of the table named data making up the in memory SQLite database. What I really wanted to know is: what do you have showing for obj(0) and obj(1) to solve the OP's question in post #5.
Clarity for the other readers.
The first item in the array is the SQL statement and the second item in the array are the arguments or values for the prepared SQL statement.
 
Upvote 0
Top