@echo off setlocal enabledelayedexpansion

set "outputFile=output.xlsx" set "tempFile=temp.xlsx"

rem Set the path of the folder containing the excel files set "folderPath=C:\path\to\folder"

rem Initialize the output file with the first excel file in the folder for /f "delims=" %%a in ('dir /b /a-d "%folderPath%*.xlsx"') do ( set "firstFile=%%a" copy /y "%folderPath%!firstFile!" "%outputFile%" goto continue )

:continue rem Merge the remaining excel files with the output file for /f "skip=1 delims=" %%a in ('dir /b /a-d "%folderPath%*.xlsx"') do ( set "currentFile=%%a" xcopy /y "%folderPath%!currentFile!" "%tempFile%" start excel.exe "%outputFile%" timeout /t 5 /nobreak >nul start excel.exe "%tempFile%" timeout /t 5 /nobreak >nul

rem Activate the output file
powershell -Command "$excel = New-Object -ComObject excel.application; $excel.Visible = $true; $workbook = $excel.Workbooks.Open('%outputFile%'); $workbook.Activate()"

rem Copy the data from the current file to the output file
powershell -Command "$excel = New-Object -ComObject excel.application; $excel.Visible = $true; $workbook = $excel.Workbooks.Open('%tempFile%'); $worksheet = $workbook.Worksheets.Item(1); $worksheet.UsedRange.Copy(); $outputWorkbook = $excel.Workbooks.Open('%outputFile%'); $outputWorksheet = $outputWorkbook.Worksheets.Item(1); $outputWorksheet.Activate(); $outputWorksheet.Paste(); $outputWorkbook.Save(); $outputWorkbook.Close(); $workbook.Close(); $excel.Quit()"

rem Delete the temporary file
del "%tempFile%"

)

rem Open the final output file start excel.exe "%outputFile%"

endloca

写一段BAT代码要求将多个excel文件合并为一个文件

原文地址: http://www.cveoy.top/t/topic/hV1l 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录