簡體   English   中英

使用Powershell將Excel工作表從一個工作簿復制到另一個工作簿

[英]Copy Excel Worksheet from one Workbook to another with Powershell

我想使用Powershell將工作表從一個工作簿復制(或移動)到另一個工作簿。

我以前做過這個,不記得怎么做。 我想我使用了CopyTo()函數。

剛入門。

$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?

見Kiron的帖子

更改索引以復制到第二張表:

$file1 = 'C:\Users\eric\Documents\Book1.xlsx' # source's fullpath
$file2 = 'C:\Users\eric\Documents\Book2.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM