簡體   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