繁体   English   中英

是否可以通过使用 Excel 宏单击下载按钮来下载 CSV 文件?

[英]Is it possible to download a CSV file by using an Excel macro to click on a download button?

我想在 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…</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>

首先:每个代码模块中始终使用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() 在大多数情况下是一个非常糟糕的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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