使用 Powershell 将 Excel 工作表从一个工作簿复制到另一个工作簿 [英] Copy Excel Worksheet from one Workbook to another with Powershell

查看:39
本文介绍了使用 Powershell 将 Excel 工作表从一个工作簿复制到另一个工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Powershell 将工作表从一个工作簿复制(或移动)到另一个工作簿.

I'd like to copy (or move) a worksheet from one workbook to another workbook with Powershell.

我以前做过,不记得怎么做了.我想我使用了 CopyTo() 函数.

I had done this before and cant remember how. I think I used CopyTo() function.

刚刚开始.

$missing = [System.Type]::missing
$excel = New-Object -Com Excel.Application

$wb1 = $excel.Workbooks.Add($missing)
$wb2 = $excel.Workbooks.Add($missing)

# Now, to copy worksheet "Sheet3" from $wb2 into $wb1 as second worksheet.
# How?

推荐答案

参见post 来自 Kiron

See post from Kiron

更改索引以复制到第二张纸:

Changed index to copy to second sheet:

$file1 = 'C:UsersericDocumentsBook1.xlsx' # source's fullpath
$file2 = 'C:UsersericDocumentsBook2.xlsx' # destination's fullpath
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user
$wb2 = $xl.workbooks.open($file1, $null, $true) # open source, readonly
$wb1 = $xl.workbooks.open($file2) # open target
$sh1_wb1 = $wb1.sheets.item(2) # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('Sheet3') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$wb2.close($false) # close source workbook w/o saving
$wb1.close($true) # close and save destination workbook
$xl.quit()
spps -n excel

这篇关于使用 Powershell 将 Excel 工作表从一个工作簿复制到另一个工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆