从WebPage中的脚本创建的表中导入Excel中的数据

时间:2014-01-10 08:49:18

标签: javascript html excel vba import

我正在尝试创建一个自动连接到网页的宏,并从表中导入excel数据。我的问题是Excel查询工具无法识别表,我认为因为它是由页面中的脚本创建的,所以我无法使用标准方式。 现在,我正在使用这种方法:

  1. 将数据复制到剪贴板
  2. 运行vba宏而不是从剪贴板获取数据并将其导入Excel
  3. 但是,我每次都要导入20多个网页,我想要一个“独立”宏,根据页面的网址,可以在excel中导入数据。

    我感兴趣的网页是: http://www.investing.com/indices/us-30-historical-data 我正在使用excel 2010

    任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:4)

试试这个

Sub Dow_HistoricalData()

    Dim xmlHttp As Object
    Dim TR_col As Object, TR As Object
    Dim TD_col As Object, TD As Object
    Dim row As Long, col As Long

    Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
    xmlHttp.Open "GET", "http://www.investing.com/indices/us-30-historical-data", False
    xmlHttp.setRequestHeader "Content-Type", "text/xml"
    xmlHttp.send

    Dim html As Object
    Set html = CreateObject("htmlfile")
    html.body.innerHTML = xmlHttp.ResponseText

    Dim tbl As Object
    Set tbl = html.getElementById("curr_table")

    row = 1
    col = 1

    Set TR_col = html.getelementsbytagname("TR")
    For Each TR In TR_col
        Set TD_col = TR.getelementsbytagname("TD")
        For Each TD In TD_col
            Cells(row, col) = TD.innerText
            col = col + 1
        Next
        col = 1
        row = row + 1
    Next
End Sub

答案 1 :(得分:0)

另一种方法是发出类似

的HTTP请求
// source http://tkang.blogspot.co.at/2010/09/sending-http-post-request-with-vba.html
Dim result As String
Dim myURL As String
Dim winHttpReq As Object
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

myURL = "http://192.168.10.101:80/your_web_service?parameter=hello¶meter2=hi"

winHttpReq.Open "GET", myURL, False
winHttpReq.Send

result = winHttpReq.responseText

并解析结果。 我自己没试过。