【问题标题】:Combining Multiple Workbooks Into One Workbook Worksheet With Powershell使用 Powershell 将多个工作簿合并为一个工作簿工作表
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-24
    • 2014-12-14
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多