本帖最后由 terse 于 2023-5-16 10:46 编辑
运行前 请备份文件- <# :
- @echo off
- powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
- pause$exit
- #>
- function get-data([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file1, [string]$file2) {
- $excel = New-Object -ComObject Excel.Application
- $excel.Visible = $false
- $wb = $excel.Workbooks.Open($file2)
- $ws = $wb.Sheets.Item(1)
- $sr = $ws.Range($ws.Cells.Item($startrow, $startcol), $ws.Cells.Item($endrow, $endcol))
- $sd = $sr.Value2
- $dwb = $excel.Workbooks.Open($file1)
- $dws = $dwb.Sheets.Item(1)
- $fc = $dws.UsedRange.Find($pat)
- if ($fc -ne $null) {
- $rown = $fc.Row
- for ($i = 0; $i -le $endrow-$startrow; $i++) {
- $dws.Rows.Item($rown + 1).EntireRow.Insert()
- }
- $dsr = $dws.Range($dws.Cells.Item($rown + 1, $startcol), $dws.Cells.Item($rown + 1 + ($endrow - $startrow), $endcol))
- $dsr = $dsr.Resize($sr.Rows.Count, $sr.Columns.Count)
- $dsr.Value2 = $sd
- $sr.Copy()
- $dsr.PasteSpecial(-4122)
- for ($i = 1; $i -le $dsr.Rows.Count; $i++) {
- $val = $dsr.Cells.Item($i, 1).Offset(-1, 0)
- $dsr.Cells.Item($i, 1).Value2 = $val.Value2
- }
- }
- $excel.DisplayAlerts = $false
- $wb.Close($false)
- $dwb.Close($true)
- $excel.Quit()
- }
- $path=$PWD.Path
- $file1 = "$path\元数据.xlsx"
- $file2 = "$path\插入内容.xlsx"
- get-data -startrow 1 -startcol 2 -endrow 4 -endcol 9 -pat 1009 -file1 $file1 -file2 $file2
- get-data -startrow 6 -startcol 2 -endrow 8 -endcol 9 -pat 1018 -file1 $file1 -file2 $file2
- get-data -startrow 10 -startcol 2 -endrow 11 -endcol 9 -pat 1023 -file1 $file1 -file2 $file2
复制代码 加一个,换为复制对象- <# :
- @echo off
- powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
- pause$exit
- #>
- function get-data([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file1, [string]$file2) {
- $excel = New-Object -ComObject Excel.Application
- $excel.Visible = $false
- $wb = $excel.Workbooks.Open($file2)
- $ws = $wb.Sheets.Item(1)
- $sr = $ws.Range($ws.Cells.Item($startrow, $startcol), $ws.Cells.Item($endrow, $endcol))
- $dwb = $excel.Workbooks.Open($file1)
- $dws = $dwb.Sheets.Item(1)
- $fc = $dws.UsedRange.Find($pat)
- if ($fc -ne $null) {
- $rown = $fc.Row
- for ($i = 0; $i -le $endrow-$startrow; $i++) {
- $dws.Rows.Item($rown + 1).EntireRow.Insert()
- }
- $dsr = $dws.Range($dws.Cells.Item($rown + 1, $startcol), $dws.Cells.Item($rown + 1 + ($endrow - $startrow), $endcol))
- $dsr = $dsr.Resize($sr.Rows.Count, $sr.Columns.Count)
- $sr.Copy()
- $dsr.PasteSpecial(-4104)
- for ($i = 1; $i -le $dsr.Rows.Count; $i++) {
- $val = $dsr.Cells.Item($i, 1).Offset(-1, 0)
- $dsr.Cells.Item($i, 1).Value2 = $val.Value2
- }
- }
- $excel.DisplayAlerts = $false
- $wb.Close($false)
- $dwb.Close($true)
- [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
- }
- $path=$PWD.Path
- $file1 = "$path\元数据.xlsx"
- $file2 = "$path\插入内容.xlsx"
- get-data -startrow 1 -startcol 2 -endrow 4 -endcol 9 -pat 1009 -file1 $file1 -file2 $file2
- get-data -startrow 6 -startcol 2 -endrow 8 -endcol 9 -pat 1018 -file1 $file1 -file2 $file2
- get-data -startrow 10 -startcol 2 -endrow 11 -endcol 9 -pat 1023 -file1 $file1 -file2 $file2
复制代码
|