Convert HTML-table to Excel using VBA

后端 未结 9 2091
难免孤独
难免孤独 2021-02-05 02:51

Convert HTML-table to Excel

The code below fetches the HTML-table at https://rasmusrhl.github.io/stuff, and converts it to Excel-format.

The pr

9条回答
  •  迷失自我
    2021-02-05 03:20

    
    

    Putting the above global style definition for the cells (

    s) on the output you generate using R or rewriting the document on the client side like below just works.

    Sub importhtml()
        '*********** HTML document rewrite process ***************
        Const TableUrl = "https://rasmusrhl.github.io/stuff"
    
        Const adTypeBinary = 1, adSaveCreateOverWrite = 2, TemporaryFolder = 2
        Dim tempFilePath, binData() As Byte
    
        With CreateObject("Scripting.FileSystemObject")
            tempFilePath = .BuildPath(.GetSpecialFolder(TemporaryFolder), .GetTempName() & ".html")
        End With
    
        'download HTML document
        With CreateObject("MSXML2.ServerXMLHTTP")
            .Open "GET", TableUrl, False
            .Send
            If .Status <> 200 Then Err.Raise 3, "importhtml", "200 expected"
            binData = .ResponseBody
        End With
    
        With CreateObject("Adodb.Stream")
            .Charset = "x-ansi"
            .Open
            .WriteText ""
            .Position = 0 'move to start
            .Type = adTypeBinary 'change stream type
            .Position = .Size 'move to end
            .Write binData 'append binary data end of stream
            .SaveToFile tempFilePath, adSaveCreateOverWrite 'save temporary file
            .Close
        End With
        '*********** HTML document rewrite process ***************
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;" & tempFilePath, Destination:=Range("$A$1"))
            'load HTML document from rewritten local copy
    
            .Name = "stuff"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = False
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingAll
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = True
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
    
        End With
    
        Kill tempFilePath
    End Sub
    

    提交回复
    热议问题