【问题标题】:Copy Excel Worksheet from one Workbook to another with Powershell使用 Powershell 将 Excel 工作表从一个工作簿复制到另一个工作簿
【发布时间】:2011-03-14 15:44:57
【问题描述】:

我想使用 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?

【问题讨论】:

    标签: com powershell excel office-automation vba


    【解决方案1】:

    请参阅 Kiron 的 post

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

    $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
    

    【讨论】:

    • 一切正常,除了我有一个问题。复制后,目标图纸仍然为复制的零件选择。我该如何停用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2014-03-05
    • 2014-12-09
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多