Home » excel » Read from html and write to Excel

# Read from html and write to Excel

Questions:

I want read html and write few columns from it into an Excel table.I am currently using a macro to do it, but need it in VBScript.

I want to count the number of fail occurrences for compliance check and oracle table and write it into an Excel doc.

Full size image of sample html and desired Excel file result.

Excel can be controlled from VBScript like this:

Set xl = CreateObject("Excel.Application")
xl.Visible = True



The HTML file could be parsed into a DOM document:

Set doc = CreateObject("Msxml2.DOMDocument.6.0")
doc.async = True


Use an XPath expression to select al <td> elements:

Set td = doc.selectNodes("//tr/td")


At this point td contains a collection of all <td> elements in the document. You can process them like this:

numrows = doc.selectNodes("//tr").Length
numcols = td.Length / numrows

row = 0
For i = 0 To td.Length - 1 Step numcols
If td(i).Text = "Fail" Then
row = row + 1
wb.Sheets(1).Cells(row, 1).Value = CDate(Split(td(i+2).Text)(0))
If InStr(td(i+1).Text, "compliance") > 0 Then
wb.Sheets(1).Cells(row, 2).Value = 1
ElseIf InStr(td(i+1).Text, "Oracletable") > 0 Then
wb.Sheets(1).Cells(row, 3).Value = 1
End If
End If
Next


The above will create a table like this:

2/9/2012    1
2/9/2012         1
2/9/2012         1
.
.
.


You can then use Excel’s Consolidate method to consolidate the data:

Const xlSum = -4157

wb.Sheets(2).Range("A1").Consolidate _
Array(wb.Sheets(1).Name & "!R1C1:R" & row & "C3"), xlSum