VBA importing UTF-8 CSV file from a web server

后端 未结 3 1169
花落未央
花落未央 2021-02-18 18:39

I have a UTF-8 CSV file stored on a web server. When I download the file put it on my hard drive and I then import it into an Excel sheet with this macro (from the macro recorde

3条回答
  •  忘掉有多难
    2021-02-18 19:23

    If the characters are displayed correctly when you download the csv file yourself, I'd divide the process to 2 stages:

    Downloading

    Sub DownloadFile(ByVal url As String, ByVal local As String)
    
    Dim WinHttpReq As Object
    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    WinHttpReq.Open "GET", url, False, "username", "password"
    WinHttpReq.send
    
    myURL = WinHttpReq.responseBody
    If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write WinHttpReq.responseBody
        oStream.SaveToFile local, 2 
        oStream.Close
    End If
    
    End Sub
    

    Loading CSV

    Sub OpenCsv(ByVal csvfile As String)
    Workbooks.OpenText Filename:= _ 
    csvfile,Local:=True,StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
    , Comma:=True, Space:=False, Other:=False
    End Sub
    

    Note That: The Local parameter is the key here,it makes VBA use your excel's local configuration (vietnamese), which is by default set to False.

    Putting it all together

    Sub DownloadAndLoad
      DownloadFile "http://myserver.com/myFile.csv","C:\myFile.csv"
      OpenCsv "C:\myFile.csv"
    End Sub
    

提交回复
热议问题