我想读取 html 并将其中的几列写入 Excel 表。我目前正在使用宏来执行此操作,但在 VBScript 中需要它。
我想计算合规性检查和 oracle 表的失败发生次数并将其写入 Excel 文档。
Full size image示例 html 和所需的 Excel 文件结果。
最佳答案
可以像这样从 VBScript 控制 Excel:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Add
HTML 文件可以解析为 DOM 文档:
Set doc = CreateObject("Msxml2.DOMDocument.6.0")
doc.async = True
doc.load "C:\path\to\your.html"
使用 XPath选择 al
<td>
的表达式要素:Set td = doc.selectNodes("//tr/td")
此时
td
包含所有 <td>
的集合文档中的元素。您可以像这样处理它们: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
上面将创建一个像这样的表:
2/9/2012 1
2/9/2012 1
2/9/2012 1
.
.
.
然后您可以使用 Excel 的
Consolidate
合并数据的方法:Const xlSum = -4157
wb.Sheets(2).Range("A1").Consolidate _
Array(wb.Sheets(1).Name & "!R1C1:R" & row & "C3"), xlSum
关于html - 从 html 读取并写入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15442470/