首页 > 解决方案 > 是否可以通过使用 Excel 宏单击下载按钮来下载 CSV 文件?

问题描述

我想在 Excel 中编写一个宏,该宏将从我用于工作的 Web 应用程序中下载 CSV。Web 应用程序的用户界面让您单击按钮以打开菜单,然后单击弹出菜单中的下载按钮。

我编写了一个应该打开 Internet Explorer 窗口的宏,然后单击下载按钮以下载我要下载的 CSV 文件。我还不能让它工作:它打开浏览器到我想要的网页,但它不下载 CSV。

我通过使用“检查元素”得到了下面的 HTML(我剪掉了看起来不相关的部分)。注意最后的下载按钮。

    <body style="overflow: hidden; padding-right: 8px;">
            <div role="presentation" class="MuiPopover-root" ... left: 0px;">
                    <div class="MuiPaper-root MuiMenu-paper MuiPaper-elevation8 MuiPopover-paper
                            MuiPaper-rounded"… transform-origin: 0px 26px;">
                    <ul class="MuiList-root MuiMenu-list MuiList-padding" role="menu" tabindex="-1">
                    ...
                    <a download="FileName.csv" class="downloadLinkButton" target="_self" href="blob:https://exampleURL.com/abcdefg1234">
                            <li class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"
                             tabindex="-1" role="menuitem" aria-disabled="false">Export to CSV</li>
                    </a>

这是我写的代码,但它不起作用:

    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element

    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorerMedium

    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True

    'navigate IE to this web page
    objIE.navigate "exampleURL.com"


    Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop

    Set elements = objIE.Document.getElementsByTagName("a")

    For Each ele In elements
        If (ele.className = "downloadLinkButton") Then
            ele.Click
            Exit For
        End If
    Next ele

正如我所提到的,这个宏不会出错,它只是将浏览器打开到我想要的网页。

有人对我如何自动下载此下载有建议吗?我不太熟悉 Blob URL 的工作原理,但我认为下载 URL 会发生变化。(我在代码/HTML 中的 URL 显然不是真正的 URL)。

谢谢!

编辑:下面是按钮的 HTML,必须单击该按钮以展开包含“导出为 CSV”选项的菜单。重要的部分开始于<button class="MuiButtonBase-root MuiIconButton-root"

<div class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-8">
        <div class="MuiGrid-root MuiGrid-container MuiGrid-direction-xs-column MuiGrid-align-items-xs-flex-end">
                <div class="icon-container">
                        <span class="lastupdated-container…&lt;/span>
                         …
                        <button class="MuiButtonBase-root MuiIconButton-root" tabindex="0" type="button" id="card-view-more" aria-label="More">
                                <span class="MuiIconButton-label">
                                        <span class="material-icons MuiIcon-root" aria-hidden="true">more_vert</span>
                                </span>
                        </button>
                </div>
        </div>
</div>

标签: htmlexcelvbaweb-scraping

解决方案


首先:始终使用Option Explicit作为每个代码模块的第一行!

第二:您不需要循环来查找下载链接。您可以使用Set elements = objIE.Document.getElementsByClassName("downloadLinkBut​​ton")(0)直接获取链接,如果它是文档中与此 CSS 类的唯一链接。

第三:可能是页面需要更多时间才能完全加载,因为也有必须加载的信息。然后你需要休息一下。您可以使用Application.Wait执行此操作

第四:如果显示的行产生错误,我认为你不处理弹出的 html 代码是链接。

尝试这个:

Option Explicit

Sub DownloadCSV()

Dim objIE As InternetExplorer 'special object variable representing the IE browser
'Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim elements As Object

  'initiating a new instance of Internet Explorer and asigning it to objIE
  Set objIE = New InternetExplorerMedium

  'make IE browser visible (False would allow IE to run in the background)
  objIE.Visible = True

  'navigate IE to this web page
  objIE.navigate "exampleURL.com"

  Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
  'Break for 5 seconds to looad more data if needed
  Application.Wait (Now + TimeSerial(0, 0, 5))

  Set elements = objIE.Document.getElementsByClassName("downloadLinkButton")(0)

  'Check whether the variable elements contains a HTML element
  'with the CSS class "downloadLinkButton"
  If Not elements Is Nothing Then
    'Wanted element found
    elements.Click
  Else
    'Wanted element not found
    MsgBox "There is no Element with the CSS class 'downloadLinkButton' in the used HTML document"
  End If
End Sub

我不知道它在您的网站上是什么样的弹出窗口。但是在这里您可以看看如何通过代码生成页面、HTML 事件和(我认为您将需要它)SendKeys(): 如何使用 excel vba 单击交互式对话框弹出窗口来解决这些问题?

如果您获得下载链接,您还可以使用 API 函数 URLDownloadToFile() 而不是 click 和 SendKeys()。SendKeys() 在大多数情况下是一个非常糟糕的解决方案。


推荐阅读