本帖最后由 q3125418 于 2024-9-2 16:24 编辑
不是很复杂;需要用VBA脚本;
代码:
[C#] 纯文本查看 复制代码 Sub CollectExcelData()
Dim ws As Worksheet
Dim mainFolder As String
Dim fileName As String
Dim folder As String
Dim wb As Workbook
Dim rowCount As Long
'设置主文件夹路径
mainFolder = "C:\Your\Main\Folder\Path" '修改为你的主文件夹路径
rowCount = 2 '从第二行开始填充数据,假设第一行是标题
'设置当前工作表
Set ws = ThisWorkbook.Sheets(1) '假设数据汇总在第一个工作表中
ws.Cells(1, 1).Value = "文件路径"
ws.Cells(1, 2).Value = "文件名"
ws.Cells(1, 3).Value = "总价"
'遍历主文件夹及其子文件夹中的所有Excel文件
folder = Dir(mainFolder & "\*.xls*", vbDirectory)
Do While folder <> ""
fileName = Dir(mainFolder & "\" & folder & "\*.xls*")
Do While fileName <> ""
Set wb = Workbooks.Open(mainFolder & "\" & folder & "\" & fileName)
'假设标题是文件名,总价在B2单元格
ws.Cells(rowCount, 1).Value = mainFolder & "\" & folder & "\" & fileName
ws.Cells(rowCount, 2).Value = wb.Name
ws.Cells(rowCount, 3).Value = wb.Sheets(1).Range("B2").Value
wb.Close False '关闭不保存
rowCount = rowCount + 1
fileName = Dir '获取下一个文件
Loop
folder = Dir '获取下一个文件夹
Loop
End Sub
EXCEL VBA的用法 百度下 |