简体   繁体   中英

Download CSV File from GitHub

I am trying to load csv file and download it to my hard disk from a link of GitHub. Here's my try

#If Win64 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongLong, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongLong, ByVal lpfnCB As LongLong) As LongLong
#Else
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#End If

Function DownloadFile(Url As String, SavePathName As String) As Boolean
DownloadFile = URLDownloadToFile(0, Replace(Url, "\", "/"), SavePathName, 0, 0) = 0
End Function

Sub Demo()
Dim strUrl As String, strSavePath As String, strFile As String

strUrl = "https://github.com/pcm-dpc/COVID-19/blob/master/dati-regioni/dpc-covid19-ita-regioni-20200224.csv"    'SharePoint Path For The File
strSavePath = ThisWorkbook.Path & "\"
strFile = "FileName" & Format(Date, "dd.mm.yyyy") & ".csv"

If DownloadFile(strUrl, strSavePath & strFile) Then
    MsgBox "File Saved To: " & vbNewLine & strSavePath
Else
    MsgBox "Unable To Download File:" & vbNewLine & strFile & vbNewLine & "Check URL String And That Document Is Shared", vbCritical
End If
End Sub

I used the code before to download some files and it worked well but as for this link the downloaded file is as HTML page not csv file. How can I download it as CSV file?

Try hitting the raw content option on that page. The link you are hitting has HTML markup, the other is just the CSV content.

Option Explicit

Public Sub GetCSV()
    Dim response As String

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni-20200224.csv", False
        .Send
        response = .responseText
    End With

    If Trim$(response) = "" Then Exit Sub

    Open "YOURPATHHERE\SOMEFILE.csv" For Output As #1
    Print #1, response
    Close #1

End Sub

this is more simple. cange website address.

Sub test()
    Dim Whttp As WinHttp.WinHttpRequest
    Dim strFile As String, str As String
    Dim Url As String

    Set Whttp = New WinHttp.WinHttpRequest
    Url = "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni-20200224.csv"   'SharePoint Path For The File
    strSavePath = ThisWorkbook.Path & "\"
    strFile = strSavePath & "FileName" & Format(Date, "dd.mm.yyyy") & ".csv"

    With Whttp
        .Open "Get", Url
        .send
        str = .responseText
    End With

    TransToCSV strFile, str


End Sub

Sub TransToCSV(myfile As String, strTxt As String)

    Dim objStream As Object


    Set objStream = CreateObject("ADODB.Stream")

    With objStream
        .Charset = "utf-8"
        .Open
        .WriteText strTxt
        .SaveToFile myfile, 2
        .Close
    End With
    Set objStream = Nothing

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM