当前位置:迷你笔记 » 效率 » 如何用Excel将图片链接批量下载至本地?

如何用Excel将图片链接批量下载至本地?

VBA实现

打开VBA代码编辑器,将以下的代码复制粘贴至VBA代码编辑器

Sub downloadJPGImages(source As Range, targetFolder As String)
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
    Set oBinaryStream = CreateObject("ADODB.Stream")
    adTypeBinary = 1
    oBinaryStream.Type = adTypeBinary
    For i = 1 To source.Rows.Count
        imagePath = targetFolder & source.Cells(i, 1)
        imageUrl = source.Cells(i, 2)
    
        On Error GoTo HTTPError
          oXMLHTTP.Open "GET", imageUrl, False
          oXMLHTTP.Send
          aBytes = oXMLHTTP.responsebody
        On Error GoTo 0
        
          oBinaryStream.Open
          oBinaryStream.Write aBytes
          adSaveCreateOverWrite = 2
          oBinaryStream.SaveToFile imagePath, adSaveCreateOverWrite
          oBinaryStream.Close
          source.Cells(i, 3).Value = "图片成功下载"
NextRow:
    Next
    MsgBox "完成"
    Exit Sub
HTTPError:
     source.Worksheet.Cells(i, source.Worksheet.UsedRange.Columns.Count + 1).Value = "图片下载失败"
     Resume NextRow
End Sub

Sub downloadJPGImagesTest()
    downloadJPGImages Range("A2:C3"), "D:\保存文件夹\"
End Sub

Script Lab实现
打开Script Lab代码编辑器,导入以下代码

name: Excel批量将链接转化为图片
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
  content: |
    $("#run").click(() => tryCatch(run));
    async function run() {
      await Excel.run(async (context) => {
        const selectedRange = context.workbook.getSelectedRange();
        selectedRange.load("values");
        await context.sync();
        var zip = new JSZip();
        for (let i = 0; i < selectedRange.values.length; i++) {
          let imageName = selectedRange.values[i][0];
          let imageUrl = selectedRange.values[i][1];
          zip.file(imageName, urlToPromise(imageUrl), { binary: true });
        }
        var zipContent = await zip.generateAsync({ type: "blob" });
        await saveAs(zipContent, "保存.zip");
        await context.sync();
      });
    }
    function urlToPromise(url) {
      return new Promise(function(resolve, reject) {
        JSZipUtils.getBinaryContent(url, function(err, data) {
          if (err) {
            reject(err);
          } else {
            resolve(data);
          }
        });
      });
    }
    /** Default helper for invoking an action and handling errors. */
    async function tryCatch(callback) {
      try {
        await callback();
      } catch (error) {
        // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
        console.error(error);
      }
    }
  language: typescript
template:
  content: |-
    <button id="run" class="ms-Button">
        <span class="ms-Button-label">下载选中区域内的链接</span>
    </button>
  language: html
style:
  content: |-
    section.samples {
        margin-top: 20px;
    }

    section.samples .ms-Button, section.setup .ms-Button {
        display: block;
        margin-bottom: 5px;
        margin-left: 20px;
        min-width: 80px;
    }
  language: css
libraries: |-
  https://appsforoffice.microsoft.com/lib/1/hosted/office.js
  @types/office-js

  office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
  office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css

  core-js@2.4.1/client/core.min.js
  @types/core-js

  jquery@3.1.1
  @types/jquery@3.3.1

  //zip files
  https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js
  //convert url to binary
  https://cdn.jsdelivr.net/npm/jszip-utils@0.1.0/dist/jszip-utils.min.js
  // download files
  https://cdn.jsdelivr.net/npm/filesaver.js-npm@1.0.1/FileSaver.min.js
未经允许不得转载:迷你笔记 » 如何用Excel将图片链接批量下载至本地?

相关文章

评论 (0)

7 + 5 =