Code snippet for converting datatable to html in vb.net

Following code snippet can be used for finding html from a datatable in vb.net. this code is very useful in many situation such as if you need to send a datatable through email then we can find html of that datatable and can send html body.

Public Shared Function ConvertToHtmlFile(ByVal myTable As DataTable) As String
        Dim myHtmlFile As String = ""
        Dim myBuilder As New StringBuilder
        If myTable Is Nothing Then
            Throw New System.ArgumentNullException("myTable")
        Else
            'Continue. }
            'Get a worker object. StringBuilder myBuilder = new StringBuilder();
 
            '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(Guid.NewGuid().ToString())
            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;'>")
 
            'Add the headings row.
 
            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. foreach (DataRow myRow in myTable.Rows) {  myBuilder.Append("<br />
<tr align='left' valign='top'>");
            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();
        myHtmlFile = myBuilder.ToString()
        Return myHtmlFile
    End Function

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.