excelshell合成_PowerShell合并excel #设计思路,新建⼀个excel,将打开的excel表复制到新excel并关闭,继续下⼀个,⼀直到结束。
#版本要求 最低Excel 2013。10版本的默认需要删除3个表
$xl=New-Object -ComObject excel.application
$xl.visible=$false
$xl.AlertBeforeOverwriting=$false
$xl.DisplayAlerts=$false
$files=Get-ChildItem -Path d:\Report #存放excel⽂件的⽬录,可以做成弹出⽂件夹选择的模式。
$wb_dst=$xl.Workbooks.add()
$ws_dst=$wb_dst.Sheets.Item("Sheet1")powershell创建目录
$Type_Missing=[System.Type]::Missing #若copy需要在某个之后,则copy第⼀个参数需要type missing
Write-Host "开始合并-----------------------------------------------" -ForegroundColor DarkCyan
foreach($file in $files){
$wb=$xl.Workbooks.Open($file.FullName)
$ws=$wb.sheets.item(1)
$ws.Copy($ws_dst)
$wb.Close()
}
$ws_dst.Delete() #删除默认的excel表
$wb_dst.SaveAs("D:\report.xlsx") #汇总存放的⽬录及⽂件名
$xl.Quit()
Write-Host "合并结束------------------------------------------" -ForegroundColor DarkYellow
×⽤扫描并分享