一次复制HTML表,而不循环遍历单元格

kol*_*inx 2 html excel vba excel-vba

我想复制HTML表的内容.是否可以在不循环表中的单元格的情况下执行此操作?(我试图避免的例子:在评论'循环每个表并以适当的格式下载到Excel.
我的表不是很大,所以循环是一个选项,如果有更好的选择只是徘徊.


我现在拥有的和建议的方法我更喜欢:

'check all opened windows and find the one with PV output
For Each shlWindow In Shell.Windows
    If TypeName(shlWindow.document) = "HTMLDocument" And shlWindow.LocationName = "PV power estimate information" Then
        Set htmlPopupTables = shlWindow.document.getElementsBytagname("table")
        For Each htmlTabl In htmlPopupTables
            If htmlTabl.classname = "data_table" Then
                ' Question :
                ' htmlTabl.Copy 'or something similar
                ' then paste into my excel sheet
            End If
        Next htmlTabl
    End If
Next
Run Code Online (Sandbox Code Playgroud)

我对HTML没有太多经验,所以即使是显而易见的,也要毫不犹豫地说明.:) 谢谢.

dee*_*dee 5

没有这样的方法,htmlTable.Copy但您可以使用clipboard并粘贴表格的html内容到Excel工作表.

注意:有必要引用互联网控件,html对象库.和ms形式.数据将粘贴到活动工作表上的活动单元格中.

Option Explicit

' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
' Add reference to Microsoft Forms 2.0 Object Library

Private Const url As String = "file:///c:/temp/table.html"

Sub test()
    Dim ie As SHDocVw.InternetExplorer
    Dim doc As MSHTML.HTMLDocument
    Dim tables As MSHTML.IHTMLElementCollection
    Dim table As MSHTML.HTMLTable
    Dim clipboard As MSForms.DataObject

    Set ie = New SHDocVw.InternetExplorer

    With ie
        .Visible = True
        .navigate url

        While .Busy Or .readyState <> READYSTATE_COMPLETE
            DoEvents
        Wend

        Set doc = .document
        Set tables = doc.getElementsByTagName("table")
        Set table = tables(0)
        Set clipboard = New MSForms.DataObject

        clipboard.SetText table.outerHTML
        clipboard.PutInClipboard
        ActiveSheet.Paste

        .Quit
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)

演示HTML

<html>
    <head></head>
    <body>
        <table>
            <tr>
                <td>Cell 1</td>
                <td>Cell 2</td>
                <td>Cell 3</td>
            </tr>
            <tr>
                <td>Cell 4</td>
                <td>Cell 5</td>
                <td>Cell 6</td>
            </tr>
            <tr>
                <td>Cell 7</td>
                <td>Cell 8</td>
                <td>Cell 9</td>
            </tr>
        </table>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述