2013-03-15 243 views
1

我想讀取html並將其寫入Excel表格中的幾列。目前我正在使用一個宏來完成它,但在VBScript中需要它。從html讀取並寫入Excel

我想統計合規性檢查和oracle表的失敗次數,並將其寫入Excel文檔。

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

Sample html and desired excel output

回答

0

你可以使用任何dom庫在HTML和OpenXML sdk讀寫出來到Excel(2007格式 - XLSX)。這是否回答你的問題,還是你有什麼具體的事情你正在努力?


編輯

對不起,我以爲你在談論在VB.Net做一些原因,現在我知道你已經在Excel中。所以我不清楚你在問什麼 - 如何打開html文件?如何計算或存儲值?

也許張貼你到目前爲止的腳本,並具體說明什麼是不工作。

3

Excel可以從VBScript來控制這樣的:

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表達來選擇人<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 
+0

+1對於這個好的和明確的答案 – JMax 2013-03-16 09:49:17