0

I am using a StringBuilder to create a HTML file from my DataTable. The file is created but when I open it in the webbrowser I have to scroll all the way down to see the table. In other words there is a big blank page first with nothing at all.

Public Function ConvertToHtmlFile(ByVal myTable As DataTable) As String
        Dim myBuilder As New StringBuilder
        If myTable Is Nothing Then
            Throw New System.ArgumentNullException("myTable")
        Else
                
            'Open tags and write the top portion. 
            myBuilder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>")
            myBuilder.Append("<head>")
            myBuilder.Append("<title>")
            myBuilder.Append("Page-")
            myBuilder.Append("CLAS Archive")
            myBuilder.Append("</title>")
            myBuilder.Append("</head>")
            myBuilder.Append("<body>")
            myBuilder.Append("<br /><table border='1px' cellpadding='5' cellspacing='0' ")
            myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>")


            myBuilder.Append("<br /><tr align='left' valign='top'>")

            For Each myColumn As DataColumn In myTable.Columns
                myBuilder.Append("<br /><td align='left' valign='top' style='border: solid 1px blue;'>")
                myBuilder.Append(myColumn.ColumnName)
                myBuilder.Append("</td><p>")
            Next

            myBuilder.Append("</tr><p>")

            'Add the data rows.
            For Each myRow As DataRow In myTable.Rows
                myBuilder.Append("<br /><tr align='left' valign='top'>")
                For Each myColumn As DataColumn In myTable.Columns
                    myBuilder.Append("<br /><td align='left' valign='top' style='border: solid 1px blue;'>")
                    myBuilder.Append(myRow(myColumn.ColumnName).ToString())
                    myBuilder.Append("</td><p>")
                Next
            Next
            myBuilder.Append("</tr><p>")
        End If

        'Close tags. 
        myBuilder.Append("</table><p>")
        myBuilder.Append("</body>")
        myBuilder.Append("</html>")

        'Get the string for return. myHtmlFile = myBuilder.ToString();
        Dim myHtmlFile As String = myBuilder.ToString()

        Return myHtmlFile
    End Function
3
  • 1
    Not sure if it matters here, but you don't want or need linebreaks between rows or cells Commented Mar 21, 2021 at 21:29
  • @HansKesting It made a big difference. Now I only have about 10 lines of empty page at the top. Commented Mar 21, 2021 at 21:35
  • 2
    Did you think to perhaps look at the output of the StringBuilder directly or show us what that is? Telling us that you have an issue with some data but not showing us that data seems to be rather counter-intuitive, don't you think? Regardless, what are all those <p> and <br /> tags doing in there? Why would you have paragraphs and line breaks between rows and cells? Commented Mar 22, 2021 at 0:05

2 Answers 2

1

A sample HTML table (from the MDN docs):

<table>
    <thead>
        <tr>
            <th colspan="2">The table header</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>The table body</td>
            <td>with two columns</td>
        </tr>
    </tbody>
</table>

If you study the "permitted content" within the various table elements (also dive deeper, for instance <tr>), there cannot be a <br> or <p> between <table>, <tr> or <td> elements, only table-related elements are allowed.

A <tr> is already a row in the table, so you don't need breaks or paragraphs to move it to a separate row.

Sign up to request clarification or add additional context in comments.

Comments

1

FWIW I find using XElement to build Html pages easier than using strings.

    Dim myHtml As XElement
    'XML literals
    ' https://learn.microsoft.com/en-us/dotnet/standard/linq/xml-literals
    'note lang and xmlns missing.  see below

    myHtml = <html>
                 <head>
                     <meta charset="utf-8"/>
                     <title>Put title here</title>
                 </head>
                 <body>
                     <table border="1px" cellpadding="5" cellspacing="0" style="border: solid 1px Silver; font-size: x-small;">
                         <thead>
                             <tr>
                                 <th colspan="4">The table header</th>
                             </tr>
                         </thead>
                         <tbody>
                         </tbody>
                     </table>
                 </body>
             </html>

    'test. five rows, four columns
    For r As Integer = 1 To 5
        Dim tr As XElement = <tr align="left" valign="top"></tr>
        For c As Integer = 1 To 4
            Dim td As XElement

            ' XML embedded expressions
            ' https://learn.microsoft.com/en-us/dotnet/standard/linq/xml-literals#use-embedded-expressions-to-create-content
            td = <td align="left" valign="top"><%= "Row:" & r.ToString & "  Col:" & c.ToString %></td>

            tr.Add(td)
        Next
        myHtml.<body>.<table>.<tbody>.LastOrDefault.Add(tr)
    Next

    Dim s As String = myHtml.ToString
    'add lang and xmlns to string!!
    s = s.Replace("<html>", "<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>")

2 Comments

I will give it a try
@Marco - there are links in the code that might prove useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.