[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
用PS吧 应该可以的

TOP

本帖最后由 terse 于 2023-5-16 10:46 编辑

运行前 请备份文件
  1. <# :
  2. @echo off
  3. powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  4. pause$exit
  5. #>
  6. function get-data([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file1, [string]$file2) {
  7. $excel = New-Object -ComObject Excel.Application
  8. $excel.Visible = $false
  9. $wb = $excel.Workbooks.Open($file2)
  10. $ws = $wb.Sheets.Item(1)
  11. $sr = $ws.Range($ws.Cells.Item($startrow, $startcol), $ws.Cells.Item($endrow, $endcol))
  12. $sd = $sr.Value2
  13. $dwb = $excel.Workbooks.Open($file1)
  14. $dws = $dwb.Sheets.Item(1)
  15. $fc = $dws.UsedRange.Find($pat)
  16. if ($fc -ne $null) {
  17. $rown = $fc.Row
  18. for ($i = 0; $i -le $endrow-$startrow; $i++) {
  19. $dws.Rows.Item($rown + 1).EntireRow.Insert()
  20. }
  21. $dsr = $dws.Range($dws.Cells.Item($rown + 1, $startcol), $dws.Cells.Item($rown + 1 + ($endrow - $startrow), $endcol))
  22. $dsr = $dsr.Resize($sr.Rows.Count, $sr.Columns.Count)
  23. $dsr.Value2 = $sd
  24. $sr.Copy()
  25. $dsr.PasteSpecial(-4122)
  26. for ($i = 1; $i -le $dsr.Rows.Count; $i++) {
  27. $val = $dsr.Cells.Item($i, 1).Offset(-1, 0)
  28. $dsr.Cells.Item($i, 1).Value2 = $val.Value2
  29. }
  30. }
  31. $excel.DisplayAlerts = $false
  32. $wb.Close($false)
  33. $dwb.Close($true)
  34. $excel.Quit()
  35. }
  36. $path=$PWD.Path
  37. $file1 = "$path\元数据.xlsx"
  38. $file2 = "$path\插入内容.xlsx"
  39. get-data -startrow 1 -startcol 2 -endrow 4 -endcol 9  -pat 1009 -file1 $file1 -file2 $file2
  40. get-data -startrow 6 -startcol 2 -endrow 8 -endcol 9  -pat 1018 -file1 $file1 -file2 $file2
  41. get-data -startrow 10 -startcol 2 -endrow 11 -endcol 9  -pat 1023 -file1 $file1 -file2 $file2
复制代码
加一个,换为复制对象
  1. <# :
  2. @echo off
  3. powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  4. pause$exit
  5. #>
  6. function get-data([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file1, [string]$file2) {
  7. $excel = New-Object -ComObject Excel.Application
  8. $excel.Visible = $false
  9. $wb = $excel.Workbooks.Open($file2)
  10. $ws = $wb.Sheets.Item(1)
  11. $sr = $ws.Range($ws.Cells.Item($startrow, $startcol), $ws.Cells.Item($endrow, $endcol))
  12. $dwb = $excel.Workbooks.Open($file1)
  13. $dws = $dwb.Sheets.Item(1)
  14. $fc = $dws.UsedRange.Find($pat)
  15. if ($fc -ne $null) {
  16. $rown = $fc.Row
  17. for ($i = 0; $i -le $endrow-$startrow; $i++) {
  18. $dws.Rows.Item($rown + 1).EntireRow.Insert()
  19. }
  20. $dsr = $dws.Range($dws.Cells.Item($rown + 1, $startcol), $dws.Cells.Item($rown + 1 + ($endrow - $startrow), $endcol))
  21. $dsr = $dsr.Resize($sr.Rows.Count, $sr.Columns.Count)
  22. $sr.Copy()
  23. $dsr.PasteSpecial(-4104)
  24. for ($i = 1; $i -le $dsr.Rows.Count; $i++) {
  25. $val = $dsr.Cells.Item($i, 1).Offset(-1, 0)
  26. $dsr.Cells.Item($i, 1).Value2 = $val.Value2
  27. }
  28. }
  29. $excel.DisplayAlerts = $false
  30. $wb.Close($false)
  31. $dwb.Close($true)
  32. [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
  33. }
  34. $path=$PWD.Path
  35. $file1 = "$path\元数据.xlsx"
  36. $file2 = "$path\插入内容.xlsx"
  37. get-data -startrow 1 -startcol 2 -endrow 4 -endcol 9  -pat 1009 -file1 $file1 -file2 $file2
  38. get-data -startrow 6 -startcol 2 -endrow 8 -endcol 9  -pat 1018 -file1 $file1 -file2 $file2
  39. get-data -startrow 10 -startcol 2 -endrow 11 -endcol 9  -pat 1023 -file1 $file1 -file2 $file2
复制代码
1

评分人数

TOP

$insertRow !insertRow!

TOP

你这个是直接从PS里拿来的吧 变量改一下 多个Sheet就改这里是多少就改多少  $wb.Sheets.Item(2),文件不在同一个的话 就少一个文件 复制上一行数据的话 参照
....
$val = $dsr.Cells.Item($i, 1).Offset(-1, 0) 这里是上一行的值
$dsr.Cells.Item($i, 1).Value2 = $val.Value2 这里是赋值
....

TOP

回复 18# haichuan5121
循环调用函数试试,xls文件问题也许是版本问题, 不知你那边为什么不行,环境不清楚,不好说

TOP

本帖最后由 terse 于 2023-5-22 15:07 编辑

这样?
$file1 = @(file1,file2,file3)
$file2 = '.\插入内容.xlsx'

TOP

回复 22# haichuan5121
不把文件路径写入文本,就把文件路径写入数组,然后传函数,这样吗,要不你就用Get-Content提取文件

TOP

回复 25# haichuan5121
这样行不
  1.     if (!$found) {
  2.         Write-Host "Pattern $pat not found in any file."
  3.         $excel.DisplayAlerts = $false
  4.         $wb.Close($false)
  5.         $excel.Quit()
  6.         [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
  7.         return
  8.     }
复制代码

TOP

回复 27# haichuan5121

晕了,以为你就只要操作一次文件插入,先说说加的$found意图是什么

TOP

没有搜索到关键词 就不执行下面的
  1. if (!$found) {
  2.     Write-Host "Pattern $pat not found in any file."
  3. } else {
  4.     $excel.DisplayAlerts = $false
  5.     $wb.Close($false)
  6.     $excel.Quit()
  7.     [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
  8. }
复制代码

TOP

回复 30# haichuan5121
能处理就好,就是觉得有点乱,还不如直接让ps在函数外面处理好;数组在函数外面循环,然后传进函数是两个文件加关键词及行列数;
行列数从文件的单元表格的背景色来获取要增加代码,行列固定的话不如直接按数组一次性传进函数,你现在是一个文件替换三次的话,是不是要传三次;
当然,怎样方便怎样来吧

TOP

返回列表