|
本帖最后由 123shusheng 于 2013-3-11 19:09 编辑
这是我在坛子里学到的例子,因为网页上的表比较复杂,所以我希望从第三列,开始导入数据,即每行的第一、第二列不导入,请问如何改,谢谢!
Private Sub W1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
On Error Resume Next
If Not (pDisp Is W1.Object) Then Exit Sub
If St = False Then Exit Sub
Lurl = W1.LocationURL
Dim txtData, TX As String
Dim txtdata1 As String
Dim ff
If Lurl = "http://www.ometal.com/price/index.asp?act=find&Prod" Then
txtData = Me.W1.Object.Document.body.innerText
If Left(txtData, 4) = " 已取消" Then
MsgBox "目标网页没有正常打开,请检查网络连接。", vbOKOnly, "系统提示"
Exit Sub
End If
End If
Dim Tables As IHTMLElementCollection
Set Tables = W1.Document.getElementsByTagName("Table")
Dim Table1 As HTMLTable
Dim i As Integer
Dim j As Integer
Dim text1 As String
'Dim TX As String
Dim N As Integer '使用此参数用于确定是否找到了数据行
txtData = Me.W1.Object.Document.body.innerText '获取页面中的去除html的数据
If Left(txtData, 4) = " 已取消" Then
MsgBox "目标网页没有正常打开,请检查网络连接。", vbOKOnly, "系统提示"
Exit Sub
End If
For Each Table1 In Tables
'M = 0 '行数置0,否则第二次运行在excel表中会增加上次运行总行数的空白行
Dim Row As HTMLTableRow, Cell As HTMLTableCell
N = 0
For i = 0 To Table1.rows.length - 1 ' 逐行处理
Set Row = Table1.rows(i)
j = 0
TX = Trim(Row.cells(0).innerText)
If Left(Trim(Row.cells(0).innerText), 4) = "产品名称" Then N = 1 ' 找到需要的行,不要表头
If N = 1 And Left(Trim(Row.cells(0).innerText), 5) <> ">> 首页" Then
If i > 1 Then M = M + 1
For Each Cell In Row.cells ' 逐列处理
If i > 1 Then newBook.worksheets(1).cells(M, 1 + j) = Trim(Row.cells(j).innerText)
If i > 2 Then text1 = text1 & Trim(Row.cells(j).innerText) & ","
j = j + 1
Next
If i = 2 Then newBook.worksheets(1).cells(M, 1 + j) = Date
If Nz(text1) <> "" Then
text1 = text1 & Format(CDate(Date) - PP, "yyyy-m-d") & vbCrLf ' 一行处理完毕后,去除行尾的逗号并加上回车
If i > 2 Then newBook.worksheets(1).cells(M, 1 + j) = Format(CDate(Date) - PP, "yyyy-m-d")
End If
End If
Next
Next
Set ff = Me.W1.Object.Document.all("__NextLink")
aa = ""
aa = ff.href
If aa = "" Then
newBook.SaveAs Filename:=CurrentProject.Path & "\价格采集.XLS" '
newBook.Close
If MsgBox("数据己传输完毕,文件己保存到:" & CurrentProject.Path & "\价格采集.XLS" & vbCrLf & "现在查看和整理数据吗?", vbYesNo + vbDefaultButton1, "系统提示") = vbYes Then
Dim newXls As Excel.Application
Set newXls = CreateObject("Excel.Application")
newXls.Visible = True
newXls.UserControl = True
newXls.Workbooks.Open CurrentProject.Path & "\价格采集.XLS"
End If
Me.命令9.Enabled = True
St = False
Else
W1.Navigate ff.href
End If
End Sub
|
|