excel - 使用 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 文件(我假设它们具有相同的行)并将合并的 CVS 导入回 excel 使用Export-CSV... 很简单。并且不需要任何 COM 操作。
推荐阅读
- docker - 可以运行 Ansible playbook 的 Docker Hub 映像
- python - 说出对非管理员禁用 @everyone 的命令
- objective-c - Objective-c 将参数传递给安装后脚本
- encryption - 什么是 Zip 强加密规范/SecureZip 密钥派生功能?
- c++ - 从字符串中读取数组元素
- google-colaboratory - colab pro + ram 是否有可能不增加
- typescript - 如何在打字稿类型中使一个属性不可选?
- r - 网页抓取返回 R 中的合并句子
- c++ - 在容器上迭代地应用 range-v3 views::filter
- flutter - Flutter 的“NeumorphicToggle”不起作用