使用Powershell将多个工作簿合并为一个工作簿工作表



我有这个Powershell脚本,我正试图将多个带有单页的工作簿组合到一个带有单表的工作簿中,并将它们全部组合在一张工作表中。我无法忘记它一直告诉我没有名为$destfile的文件,无法打开。正确的语法是什么?

感谢

$ExcelObject = New-Object -ComObject excel.application
$ExcelObject.visible=$true
$file1 = 'file1location'
$file2 = 'file2location'
$destfile = 'fileI want to saveas afterits compiled'
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user
$wb1 = $xl.workbooks.open($file1, $null, $true) # open source, readonly
$wb2 = $xl.workbooks.open($file2, $null, $true)
$wb3 = $xl.workbooks.open($destfile) # open target
$sh1_wb2 = $wb2.sheets.item(1) # first sheet in destination workbook
$sheetToCopy = $wb1.sheets.item('Sheet1') # source sheet to copy
$sheetToCopy.copy($sh1_wb2) # copy source sheet to destination workbook
$wb1.close($false) # close source workbook w/o saving
$wb2.close($true) # close and save destination workbook
$xl.quit()
spps -n excel

您可以尝试使用https://github.com/dfinke/ImportExcel使用Import csv将数据从多个工作表导出到csv,然后组合这些csv文件(我假设它们有相同的行(,并使用Export csv将组合的CVS导入回excel。。。非常简单。并且不需要任何COM操作。

相关内容

最新更新