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/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

发表回复

登录后才能评论