html - 从 html 读取并写入 Excel

标签 html excel vbscript

我想读取 html 并将其中的几列写入 Excel 表。我目前正在使用宏来执行此操作,但在 VBScript 中需要它。

我想计算合规性检查和 oracle 表的失败发生次数并将其写入 Excel 文档。

Full size image示例 html 和所需的 Excel 文件结果。

Sample html and desired excel output

最佳答案

可以像这样从 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/

相关文章:

html - 显示 3 个 div,2 个一起显示,第三个在第一个下面

Excel Application.Windows.Count 返回 0

excel - 如何在复杂公式中添加美元符号?

unicode - Request.QueryString 和 Request.ServerVariables ("QUERY_STRING")不显示 UNICODE

internet-explorer - 导航到网站、全选、复制并粘贴到记事本中

javascript - AJAX回调函数间歇性添加GET参数失败

javascript - 如何根据用户的点击次数排序链接的顺序并使用JavaScript将其保存到本地存储?

html - jQuery Mobile 多个 HTML 文件

arrays - Excel VBA - redim arr 时类型不匹配

javascript - 如何自动化/脚本化注册等流程