Excel批量導入圖片到指定單元格

一、從excel圖片批量嵌入單元格

在Excel中,我們可以將圖片嵌入單元格中,這樣可以保證圖片與單元格的對應關係,也方便對單元格和圖片的移動和複製。

Sub Insert_Picture_Into_Cell()
    Dim strPic As String
    strPic = Application.GetOpenFilename("Pictures (*.gif; *.jpg; *.bmp), *.gif;*.jpg;*.bmp", MultiSelect:=True)
    If strPic = "False" Then Exit Sub
    Dim cell As Range
    For Each cell In Selection
        If cell.Row = ActiveCell.Row Then
            cell.Select
            If Len(strPic) > 0 Then
                cell.Activate
                ActiveSheet.Pictures.Insert(strPic).Select
                With Selection.ShapeRange
                    .LockAspectRatio = msoTrue
                    .Width = cell.Width - 2
                    .Height = cell.Height - 2
                    .Top = cell.Top + 1
                    .Left = cell.Left + 1
                End With
            End If
        End If
    Next
End Sub

二、Excel批量導入多張圖片並自動排版

如果我們需要一次性導入多張圖片並自動排版,可以使用以下代碼:

Sub Insert_Multiple_Pictures()
    Dim strPath As String
    strPath = "C:\Pictures\" '圖片所在文件夾路徑
    Dim imgExtension As String
    imgExtension = "*.jpg" '圖片擴展名
    Dim rowIndex As Integer
    rowIndex = 1 '從第一行開始插入圖片
    Dim colIndex As Integer
    colIndex = 1 '從第一列開始插入圖片
    Dim picTop As Double
    picTop = Cells(rowIndex, colIndex).Top '圖片頂部位置
    Dim picLeft As Double
    picLeft = Cells(rowIndex, colIndex).Left '圖片左側位置
    Dim picWidth As Double
    picWidth = Cells(rowIndex, colIndex).Width '圖片寬度
    Dim picHeight As Double
    picHeight = Cells(rowIndex, colIndex).Height '圖片高度
    Dim imgCount As Integer
    imgCount = 0 '已插入圖片數量
    Dim pic As Picture
    For Each pic In ActiveSheet.Pictures
        pic.Delete
    Next pic
    Dim strFile As String
    strFile = Dir(strPath & imgExtension)
    Do While Len(strFile) > 0
        Set pic = ActiveSheet.Pictures.Insert(strPath & strFile)
        pic.Top = picTop
        pic.Left = picLeft
        pic.Width = picWidth
        pic.Height = picHeight
        If colIndex  20 Then Exit Do '插入圖片20張後退出
        picTop = Cells(rowIndex, colIndex).Top
        picLeft = Cells(rowIndex, colIndex).Left
        picWidth = Cells(rowIndex, colIndex).Width
        picHeight = Cells(rowIndex, colIndex).Height
        strFile = Dir
        imgCount = imgCount + 1
    Loop
    MsgBox imgCount & "張圖片已成功導入到Excel中!"
End Sub

三、Excel批量導入對應圖片

如果我們有一份表格,需要根據表格中每行數據對應的圖片來批量導入圖片,可以使用以下代碼:

Sub Insert_Picture_By_Match()
    Dim strPath As String
    strPath = "C:\Pictures\" '圖片所在文件夾路徑
    Dim imgExtension As String
    imgExtension = "*.jpg" '圖片擴展名
    Dim picTop As Double
    picTop = 0 '圖片頂部位置
    Dim picLeft As Double
    picLeft = 0 '圖片左側位置
    Dim picWidth As Double
    picWidth = 100 '圖片寬度
    Dim picHeight As Double
    picHeight = 100 '圖片高度
    Dim i As Integer
    Dim j As Integer
    Dim strFile As String
    strFile = Dir(strPath & imgExtension)
    For i = 2 To 10 '從第2行開始
        picTop = ActiveSheet.Cells(i, 3).Top '圖片所在行第3列
        picLeft = ActiveSheet.Cells(i, 4).Left '圖片所在行第4列
        strFile = Dir(strPath & Left(ActiveSheet.Cells(i, 5).Value, 3) & imgExtension) '根據數據中的名稱匹配圖片
        Set pic = ActiveSheet.Pictures.Insert(strPath & strFile)
        pic.Top = picTop
        pic.Left = picLeft
        pic.Width = picWidth
        pic.Height = picHeight
    Next i
    MsgBox "圖片已成功導入到Excel中!"
End Sub

四、Excel中快速批量導入圖片

如果我們只需要快速地將圖片批量導入到Excel中,可以使用以下方法:

在Excel中,我們可以直接通過複製粘貼的方式將圖片導入到單元格中。

步驟如下:

1.打開圖片文件夾和Excel表格文件,分別在Windows資源管理器和Excel中選中需要導入圖片的單元格;

2.在資源管理器中選中需要導入的圖片文件,使用Ctrl+C複製或者右鍵菜單複製;

3.在Excel表格中單擊需要導入圖片的單元格,使用Ctrl+V粘貼或者右鍵菜單粘貼。

五、Excel批量導入圖片對應名稱

如果我們有一批圖片,需要將圖片名稱對應到Excel表格中的姓名列,可以使用以下代碼:

Sub Insert_Picture_By_Name()
    Dim strPath As String
    strPath = "C:\Pictures\" '圖片所在文件夾路徑
    Dim imgExtension As String
    imgExtension = "*.jpg" '圖片擴展名
    Dim imgCount As Integer
    imgCount = 0 '導入圖片數量
    Dim strFile As String
    strFile = Dir(strPath & imgExtension)
    Dim r As Range
    For Each r In ActiveSheet.Range("A1:A10") '以A1:A10範圍內的姓名為圖片名稱
        strFile = Dir(strPath & Left(r.Value, 3) & imgExtension)
        Set pic = ActiveSheet.Pictures.Insert(strPath & strFile)
        pic.Top = r.Top
        pic.Left = r.Left + r.Width + 5 '將圖片放在姓名列的右側5個單元格
        pic.Width = 100
        pic.Height = 100
        imgCount = imgCount + 1
    Next r
    MsgBox imgCount & "張圖片已成功導入到Excel中!"
End Sub

六、Excel批量導入圖片按姓名並選取

如果我們需要將圖片按姓名進行導入,並且可以手動選取圖片,可以使用以下代碼:

Sub Insert_Picture_By_Select()
    Dim strPath As String
    strPath = "C:\Pictures\" '圖片所在文件夾路徑
    Dim imgExtension As String
    imgExtension = "*.jpg" '圖片擴展名
    Dim imgCount As Integer
    imgCount = 0 '導入圖片數量
    Dim strFile As String
    strFile = Dir(strPath & imgExtension)
    Dim r As Range
    For Each r In ActiveSheet.Range("A1:A10") '以A1:A10範圍內的姓名為圖片名稱
        strFile = Dir(strPath & Left(r.Value, 3) & imgExtension)
        If Len(strFile) = 0 Then
            MsgBox "未找到名為" & Left(r.Value, 3) & "的照片。"
            Exit Sub
        End If
        Dim pic As Picture
        Set pic = ActiveSheet.Pictures.Insert(strPath & strFile)
        pic.Top = r.Top
        pic.Left = r.Left + r.Width + 5 '將圖片放在姓名列的右側5個單元格
        pic.Width = 100
        pic.Height = 100
        imgCount = imgCount + 1
        Dim picPath As String
        picPath = strPath & strFile
        Set pic = Nothing
        strFile = Dir
        ActiveSheet.Shapes.Range(Array(pic.Name)).OnAction = "Select_Picture('" & picPath & "')"
    Next r
    MsgBox imgCount & "張圖片已成功導入到Excel中!"
End Sub

Function Select_Picture(picPath)
    Dim pic As Picture
    Set pic = ActiveSheet.Pictures.Insert(picPath)
    pic.Top = ActiveCell.Top
    pic.Left = ActiveCell.Left
    pic.Width = ActiveCell.Width
    pic.Height = ActiveCell.Height
    Set pic = Nothing
End Function

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/254142.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-14 17:40
下一篇 2024-12-14 17:40

相關推薦

  • 用Python繪製酷炫圖片

    在本篇文章中,我們將展示如何使用Python繪製酷炫的圖片。 一、安裝Python繪圖庫 在使用Python繪製圖片之前,我們需要先安裝Python繪圖庫。Python有很多繪圖庫…

    編程 2025-04-29
  • 使用axios獲取返回圖片

    使用axios獲取返回圖片是Web開發中很常見的需求。本文將介紹如何使用axios獲取返回圖片,並從多個方面進行詳細闡述。 一、安裝axios 使用axios獲取返回圖片前,首先需…

    編程 2025-04-29
  • Python 圖片轉表格

    本文將詳細介紹如何使用Python將圖片轉為表格。大家平時在處理一些資料的時候難免會遇到圖片轉表格的需求。比如從PDF文檔中提取表格等場景。當然,這個功能也可以通過手動複製、粘貼,…

    編程 2025-04-29
  • 為什麼不能用Microsoft Excel進行Python編程?

    Microsoft Excel是一個廣泛使用的數據分析工具,但是它不能直接用於Python編程。這是因為Microsoft Excel並不是一個編程工具,它的主要功能是進行數據處理…

    編程 2025-04-29
  • Python緩存圖片的處理方式

    本文將從多個方面詳細闡述Python緩存圖片的處理方式,包括緩存原理、緩存框架、緩存策略、緩存更新和緩存清除等方面。 一、緩存原理 緩存是一種提高應用程序性能的技術,在網絡應用中流…

    編程 2025-04-29
  • Python如何抓取圖片數據

    Python是一門強大的編程語言,能夠輕鬆地進行各種數據抓取與處理。抓取圖片數據是一個非常常見的需求。在這篇文章中,我們將從多個方面介紹Python如何抓取圖片數據。 一、使用ur…

    編程 2025-04-29
  • Python利用Image加圖片的方法

    在Python中,利用Image庫可以快速處理圖片,並加入需要的圖片,本文將從多個方面詳細闡述這個操作。 一、Image庫的安裝和基礎操作 首先,我們需要在Python中安裝Ima…

    編程 2025-04-28
  • Avue中如何按照後端返回的鏈接顯示圖片

    Avue是一款基於Vue.js、Element-ui等技術棧的可視化開發框架,能夠輕鬆搭建前端頁面。在開發中,我們使用到的圖片通常都是存儲在後端服務器上的,那麼如何使用Avue來展…

    編程 2025-04-28
  • 使用CKSlide實現圖片輪播

    CKSlide是一個基於jQuery的插件,可以方便地為網頁添加幻燈片和圖片輪播效果。使用CKSlide可以讓網站更加生動、活潑,給用戶帶來更好的體驗。 一、CKSlide基本用法…

    編程 2025-04-28
  • Java批量執行SQL時Communications Link Failure Socket is Closed問題解決辦法

    對於Java開發人員來說,批量執行SQL是一個經常會遇到的問題。但是,有時候我們會遇到「Communications link failure socket is closed」這…

    編程 2025-04-28

發表回復

登錄後才能評論