【发布时间】:2020-07-01 20:14:09
【问题描述】:
我有这个 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
【问题讨论】:
-
$destfile描述的文件是否存在? -
不,我需要每天创建一个。
-
你正试图打开
$wb3 = $xl.workbooks.open($destfile)中不存在的文件
标签: excel powershell