This blog deals with loading of HTML tables into VSFlexGrid. Currently we have LoadGrid method which loads grid from a file previously saved comma-delimited text file (CSV format) such as an Excel text file, or a tab-delimited text file saved using the SaveGrid method. To load HTML table directly, we need to parse the HTML into a delimited text file and use LoadGrid to add it. Following code showcases the same, on Form _Load event:
' Load some HTML Text
Me.Text1.Text = "<table>" & _
"<thead>" & _
"<tr>" & _
"<th>Header 1</th>" & _
"<th>Header 2</th>" & _
"</tr>" & _
"<tbody>" & _
"<tr>" & _
"<td>R1C1</td>" & _
"<td>R1C2</td>" & _
"</tr>" & _
"<tr>" & _
"<td>R2C1</td>" & _
"<td>R2C2</td>" & _
"</tr>" & _
"</tbody>" & _
"</table>"
To parse the html, we will create a ParseHTML() method which will parse this string into a CSV file.
Private Sub ParseHTML(htmlText As String)
' Use VSFlexString to parse out the HTML
' and create comma delimited text
With Me.VSFlexString1
.Text = htmlText
.Pattern = "</tr>"
.Replace = "\\r\\n"
.Pattern = "</th>"
.Replace = ","
.Pattern = "</td>"
.Replace = ","
.Pattern = "<[^>]*>"
.Replace = ""
.Pattern = ",\\r\\n"
.Replace = "\\r\\n"
End With
End Sub
Now, we will use the LoadGrid method of VsFlexGrid and import the table into the grid :
Private Sub Command1_Click()
Dim mytext
mytext = FreeFile
' Convert HTML to comma delimited text
ParseHTML Me.Text1.Text
Me.Text1.Text = Me.VSFlexString1.Text
' Save the text to a .csv
Open App.Path & "\\text.csv" For Output As #mytext
Print #mytext, Me.Text1.Text
Close mytext
' Load the .csv into the grid.
Me.VSFlexGrid1.LoadGrid App.Path & "\\text.csv", flexFileCommaText
End Sub
Please refer to the attached sample for complete implementation. Download Sample