吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 590|回复: 14
收起左侧

[求助] 助表格VBA导入照片(WPS)

[复制链接]
heike1993 发表于 2025-3-31 11:09
本帖最后由 heike1993 于 2025-3-31 14:42 编辑

是这样子 ,原来使用方方格子 批量导入照片,如图


前几天刷到VBA也可以且非常漂亮,


研究半天 不会,求大佬优化,
[Asm] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Sub BatchImportImages()
    Dim fd As FileDialog
    Dim imgFolder As String
    Dim imgList As Object
    Dim ws As Worksheet
    Dim startCell As Range
    Dim imgWidth As Double, imgHeight As Double
    Dim colsPerRow As Integer, rowSpacing As Integer, colSpacing As Integer
    Dim boldSpacing As Boolean, fontSize As Integer
     
    ' 创建新工作表
    Set ws = ThisWorkbook.Sheets.Add
    ws.Name = "图片目录"
     
    ' 用户参数设置(示例值,可改为从输入框获取)
    colsPerRow = 3       ' 每行图片数
    rowSpacing = 2       ' 行间隔(行数)
    colSpacing = 2       ' 列间隔(列数)
    imgWidth = 150       ' 图片宽度
    imgHeight = 100      ' 图片高度
    boldSpacing = True   ' 间隔行加粗
    fontSize = 11        ' 字号
     
    ' 选择图片文件夹
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    If fd.Show <> -1 Then Exit Sub
    imgFolder = fd.SelectedItems(1) & "\"
     
    ' 获取图片列表
    Set imgList = CreateObject("Scripting.Dictionary")
    GetFileList imgFolder, "*.jpg;*.png;*.bmp", imgList
     
    If imgList.Count = 0 Then
        MsgBox "未找到图片文件!"
        Exit Sub
    End If
     
    ' 初始化起始位置
    Set startCell = ws.Range("A1")
    Dim currentRow As Long: currentRow = startCell.Row
    Dim currentCol As Long: currentCol = startCell.Column
     
    ' 循环插入图片
    Dim i As Long, imgPath As Variant
    For Each imgPath In imgList.Keys
        ' 插入图片
        With ws.Pictures.Insert(imgPath)
            .Top = ws.Cells(currentRow, currentCol).Top
            .Left = ws.Cells(currentRow, currentCol).Left
            .Width = imgWidth
            .Height = imgHeight
            .Placement = xlMoveAndSize
        End With
         
        ' 设置文字描述
        ws.Cells(currentRow + 1, currentCol).Value = GetFileName(imgPath)
        With ws.Cells(currentRow + 1, currentCol)
            .WrapText = True
            .VerticalAlignment = xlTop
            .HorizontalAlignment = xlJustify
            .Font.Size = fontSize
        End With
         
        ' 更新列位置
        currentCol = currentCol + colSpacing + 1
         
        ' 换行判断
        If (i + 1) Mod colsPerRow = 0 Then
            currentRow = currentRow + rowSpacing + 2 ' 图片行+文字行
            currentCol = startCell.Column
             
            ' 设置间隔行格式
            If boldSpacing Then
                ws.Rows(currentRow - rowSpacing & ":" & currentRow - 1).Font.Bold = True
            End If
        End If
        i = i + 1
    Next imgPath
     
    ' 自动调整列宽
    ws.Columns.AutoFit
    MsgBox "已成功导入 " & imgList.Count & " 张图片!"
End Sub
 
' 获取文件名列表
Sub GetFileList(folderPath As String, fileFilter As String, ByRef dict As Object)
    Dim fileName As String
    fileName = Dir(folderPath & fileFilter)
    Do While fileName <> ""
        dict.Add folderPath & fileName, fileName
        fileName = Dir
    Loop
End Sub
 
' 提取纯文件名
Function GetFileName(fullPath As String) As String
    GetFileName = Mid(fullPath, InStrRev(fullPath, "\") + 1)
End Function



修改以下参数(或添加输入框):

colsPerRow:每行显示图片数量

rowSpacing:行间隔行数

colSpacing:列间隔列数

imgWidth/imgHeight:图片尺寸

boldSpacing:间隔行是否加粗

fontSize:文字字号


要求空表,和方方格子一样,可以选着C盘目录的照片文件夹,设置C盘目录的文件夹,批量导入文件夹内的照片可以设置每行几个图片间隔几行指定高度,间隔几列指定高度,(文字设置两端对齐,间隔的几行几列可以设置加粗,字号大小)可以设置图片大小,一键嵌入照片到表格,达到和《导入后的表格.xlsx》一样的排版效果
我上传照片 方方格子处理后的表格 (照片 ):https://wwan.lanzn.com/i8vJP2s7b03c

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yemind 发表于 2025-3-31 13:46
也学习下
wuliaodelaohu 发表于 2025-3-31 14:30
 楼主| heike1993 发表于 2025-3-31 14:32
 楼主| heike1993 发表于 2025-3-31 14:33
wuliaodelaohu 发表于 2025-3-31 14:30
你这是个假的蓝奏云吧,

我又看了 绝对没有进错官网!!!
wuliaodelaohu 发表于 2025-3-31 14:43
heike1993 发表于 2025-3-31 14:33
我又看了 绝对没有进错官网!!!

可能是我浏览器的问题吧。- - 。
wuliaodelaohu 发表于 2025-3-31 14:54




是这样吗
 楼主| heike1993 发表于 2025-3-31 14:55

牛逼,有个问题,我要追加导入 会重叠,就是第一次导入A文件夹,再导入B文件夹,会把B文件夹替换到A文件夹位置,可以?
 楼主| heike1993 发表于 2025-3-31 14:56

就是 以你的第二图为例子  可以指定第10行 继续导入B文件夹的图片??
wuliaodelaohu 发表于 2025-3-31 15:01
Sub ImportPicturesToExcel()
    Dim ws As Worksheet
    Dim folderPath As String
    Dim imgFile As String
    Dim imgCount As Integer
    Dim rowNum As Integer, colNum As Integer
    Dim pic As Picture
    Dim imgWidth As Double, imgHeight As Double
   
    ' 设置工作表
    Set ws = ActiveSheet
    ws.Cells.Clear ' 清空工作表内容
   
    ' 设置图片大小(可根据需要调整)
    imgWidth = 100 ' 图片宽度(单位:点)
    imgHeight = 100 ' 图片高度(单位:点)
   
    ' 获取文件夹路径
    folderPath = InputBox("请输入包含图片的文件夹路径:", "选择文件夹")
    If folderPath = "" Then Exit Sub
   
    ' 确保文件夹路径以反斜杠结尾
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
   
    ' 检查文件夹是否存在
    If Dir(folderPath, vbDirectory) = "" Then
        MsgBox "指定的文件夹不存在!", vbExclamation
        Exit Sub
    End If
   
    Application.ScreenUpdating = False ' 关闭屏幕更新以提高速度
   
    imgCount = 0
    rowNum = 1 ' 从第1行开始
   
    ' 获取第一个图片文件
    imgFile = Dir(folderPath & "*.*")
   
    ' 循环处理文件夹中的所有图片文件
    Do While imgFile <> ""
        ' 检查文件是否是图片(简单检查扩展名)
        If LCase(Right(imgFile, 4)) Like "*.jpg" Or _
           LCase(Right(imgFile, 4)) Like "*.png" Or _
           LCase(Right(imgFile, 4)) Like "*.gif" Or _
           LCase(Right(imgFile, 4)) Like "*.bmp" Or _
           LCase(Right(imgFile, 5)) Like "*.jpeg" Or _
           LCase(Right(imgFile, 4)) Like "*.tif" Then
            
            ' 计算当前图片应该放置的列(0=A, 2=C, 4=E, 6=G)
            colNum = (imgCount Mod 4) * 2
            
            ' 插入图片
            Set pic = ws.Pictures.Insert(folderPath & imgFile)
            
            ' 调整图片位置和大小
            With pic
                .Top = ws.Cells(rowNum, 1).Top
                .Left = ws.Cells(rowNum, colNum + 1).Left
                .Width = imgWidth
                .Height = imgHeight
                .Placement = xlMoveAndSize ' 图片随单元格移动和调整大小
            End With
            
            ' 调整行高以适应图片
            ws.Rows(rowNum).RowHeight = imgHeight
            
            imgCount = imgCount + 1
            
            ' 每4张图片换行,并空一行
            If imgCount Mod 4 = 0 Then
                rowNum = rowNum + 2 ' 空一行
            End If
        End If
        
        ' 获取下一个文件
        imgFile = Dir()
    Loop
   
    Application.ScreenUpdating = True ' 恢复屏幕更新
   
    MsgBox "共导入了 " & imgCount & " 张图片。", vbInformation
End Sub



这个是导入第一个文件夹用的,




导入完修改一下,
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-23 11:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表