【问题标题】:Downloading zip file from website using excel vba (if also able to extract the csv from zip file and open it in excel, then even better)使用 excel vba 从网站下载 zip 文件(如果还能够从 zip 文件中提取 csv 并在 excel 中打开它,那就更好了)
【发布时间】:2016-06-08 02:37:33
【问题描述】:

经过相当多的搜索,我无法制作一个可以从特定网站下载 .zip 文件的宏。我的意思是我已经能够找到类似的问题,但无法应用必要的更改来解决我的问题。包含 zip 文件的网站是:https://nio.gov.si/nio/data/prvic+registrirana+vozila+v+letu+2014+po+mesecih,在表头“Priponke”下是文件。例如:2014 年 12 月 (959 kb)、2014 年 11 月 (1061 kb)、...下载 2014 年 12 月 zip 文件的 url 是“cms/download/document/a7605005b6879fe5f7dbab6d60d4ae787dbced6b-1422453741279”。预先感谢您,正在等待您的回复。

我当前的代码是:

Public Sub DownloadFile()
    Dim objWHTTP As Object
    Dim strPath As String
    Dim arrData() As Byte
    Dim lngFreeFile As Long

On Error Resume Next
    Set objWHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
    If Err.Number <> 0 Then
        Set objWHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    End If
On Error GoTo 0

    strPath = "https://nio.gov.si/nio/data/prvic+registrirana+vozila+v+letu+2014+po+mesecih"
    strPath = "https://nio.gov.si/nio/cms/download/document/a7605005b6879fe5f7dbab6d60d4ae787dbced6b-1422453741279"

    objWHTTP.Open "GET", strPath, False
    objWHTTP.send
    arrData = objWHTTP.responseBody

    If Len(Dir("C:\FootieFile", vbDirectory)) = 0 Then
        MkDir "C:\FootieFile"
    End If

    lngFreeFile = FreeFile
    Open "C:\FootieFile\MyFile.xml" For Binary Access Write As #lngFreeFile
        Put #lngFreeFile, 1, arrData
    Close #lngFreeFile

    Set objWHTTP = Nothing
    Erase arrData
End Sub

亲切的问候

【问题讨论】:

  • 不起作用。 “未指定的错误。”一旦我将 myURL 更改为“cms/download/document/a7605005b6879fe5f7dbab6d60d4ae787dbced6b-1422453741279”,就会出现 Method open of object IServerXMLHTTPRequest2 failed.

标签: vba excel csv


【解决方案1】:

您需要下载一个二进制文件并保存。这可以使用 MSXML2.XMLHTTP60 对象进行下载,并使用 ADODB.Stream 对象进行保存。

参见例如http://www.motobit.com/tips/detpg_read-write-binary-files/

我已经成功地使用它从服务器下载 JPG 文件并将它们显示在 MS Access 前端。

另外我希望你意识到你不能用“cms”开始你的网址,因为你需要完全限定的域名和资源(又名 http:// 等)

还要小心使用 unicode 数据。为此,您可能需要使用 StrConv()。下载后检查文件大小是否与服务器上的大小相同。

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2018-02-19
    • 2018-04-24
    • 2017-01-30
    相关资源
    最近更新 更多