vb.net Stringbuilder 创建 HTML 文件

问题描述

我正在使用 StringBuilder 从我的 DataTable 创建一个 HTML 文件。该文件已创建,但是当我在网络浏览器中打开它时,我必须一直向下滚动才能看到表格。换句话说,首先有一个很大的空白页,什么都没有。

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

解决方法

示例 HTML 表格(来自 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>

如果您研究各种表格元素中的“允许内容”(也深入研究,例如 <tr>),则 {{1} 之间不能有 <br><p> }、<table><tr> 元素,仅允许与表格相关的元素。

<td> 已经是表格中的一行,因此您无需使用分隔符或段落将其移至单独的行。

,

FWIW 我发现使用 XElement 构建 Html 页面比使用字符串更容易。

    Dim myHtml As XElement
    'XML literals
    ' https://docs.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://docs.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'>")