Copy Excel Worksheet from one Workbook to another with Powershell

前端 未结 1 1935
终归单人心
终归单人心 2020-12-06 13:50

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

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

相关标签:
1条回答
  • 2020-12-06 14:21

    See post from Kiron

    Changed index to copy to second sheet:

    $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
    
    0 讨论(0)
提交回复
热议问题