【问题标题】:transfer DATA between Excel worksheets without sending the actual worksheet template在 Excel 工作表之间传输数据而不发送实际的工作表模板
【发布时间】:2014-03-26 20:27:58
【问题描述】:

如何在 Excel 工作表之间传输数据而不发送实际的工作表模板?

来源是.xls;目的地是.xlsm

我有一个 powershell 脚本,它可以将 工作表 从源传输到目标,并进行一些重命名以使其 出现 给最终用户,就好像只有数据一样转移,但实际上是将工作表复制到目标文件中,然后重命名原始工作表,然后重命名复制的工作表以代替原始工作表,然后删除现在重命名但原始的工作表。

这是一个问题,因为工作簿中的某些单元格引用了原始工作表(该工作表被重命名然后被删除),因此引用中断并变成#REF!

有没有办法简单地将源工作表的内容转移到空的目标工作表中,而无需实际复制/重命名工作表?

如果没有,我怎样才能保留我的脚本但确保 Excel 中的引用不会损坏?

如果您想看看我的脚本目前的工作原理,这里是:

$file1 = $args[0]

$file2 = $args[1]

    <#
        $file1 = 'c:\source.xls' # source's fullpath
        $file2 = 'c:\destination.xlsm' # destination's fullpath
    #>

    $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) # open target
    $sh2_wb2 = $wb2.sheets.item('SheetOfInterest') # sheet in destination workbook
    $sheetToCopy = $wb1.sheets.item('SheetOfInterest') # source sheet to copy

    $sh2_wb2.Name = "OldSheetOfInterest" #Rename original sheet in template
    $sheetToCopy.copy($sh2_wb2)  # copy source sheet to destination workbook

    $sh2_wb2 = $wb2.sheets | where {$_.name -eq "OldSheetOfInterest"}
    $sh2_wb2.delete() #Delete original sheet in template

    $wb1.close($false) # close source workbook w/o saving
    $wb2.close($true) # close and save destination workbook
    $xl.quit()
    spps -n excel

【问题讨论】:

    标签: excel powershell automation etl


    【解决方案1】:

    试试这个...

    $file1 = $args[0]
    
    $file2 = $args[1]
    
        <#
            $file1 = 'c:\source.xls' # source's fullpath
            $file2 = 'c:\destination.xlsm' # destination's fullpath
        #>
    
    $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) # open target
    $destination = $wb2.sheets.item('SheetOfInterest') # sheet in destination workbook
    $source = $wb1.sheets.item('SheetOfInterest') # source sheet to copy
    
    [void]$destination.UsedRange.Clear() # Clear cells that have data in the destination
    [void]$source.UsedRange.Copy() # Copy range of cells with data in them on source sheet
    [void]$destination.Range("A1","A1").Select() # Set first cell of destination as active cell
    [void]$destination.paste() # Paste data into destination sheet starting at active cell (A1)
    [void]$destination.Range("A1","A1").Select() # Set first cell of destination as active cell, otherwise is has everything selected
    
    $wb1.close($false) # close source workbook w/o saving
    $wb2.close($true) # close and save destination workbook
    $xl.quit()
    spps -n excel
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多