这里把 $excel.DisplayAlerts = $false $wb.Close($true) [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null 换为这样是不是好点 $excelSave(); $wb.Close($true); $excel.Quit();
时间: 2023-06-25 08:06:47 浏览: 47
是的,你可以使用以下代码来代替之前的代码:
```
$excelSave()
$wb.Close($true)
$excel.Quit()
[Runtime.Interopservices.Marshal]::ReleaseComObject($wb) | Out-Null
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
```
这样可以更好地确保 Excel 进程被正确关闭和释放所占用的资源。同时,使用 `[System.GC]::Collect()` 和 `[System.GC]::WaitForPendingFinalizers()` 可以强制执行垃圾回收以释放所有未引用的对象。
相关问题
function copy-data([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file, [int]$sheetfrom, [int]$sheetto) { $excel = New-Object -ComObject Excel.Application $excel.Visible = $false $wb = $excel.Workbooks.Open($file) $wsfrom = $wb.Sheets.Item($sheetfrom) $wsto = $wb.Sheets.Item($sheetto) $sr = $wsfrom.Range($wsfrom.Cells.Item($startrow, $startcol), $wsfrom.Cells.Item($endrow, $endcol)) $fc = $sr.Find($pat) if ($fc -ne $null) { $rown = $fc.Row $dsr = $wsto.Range($wsto.Cells.Item($rown, $startcol), $wsto.Cells.Item($rown + ($endrow - $startrow), $endcol)) $dsr = $dsr.Resize($sr.Rows.Count, $sr.Columns.Count) $sr.Copy() $dsr.PasteSpecial(-4104) } $excel.DisplayAlerts = $false $wb.Close($true) [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null } $path = $PWD.Path $file = "$path\a.xlsx" # 将sheet1中第1行到第4行、第2列到第9列的数据复制到sheet2中 copy-data -startrow 1 -startcol 2 -endrow 4 -endcol 9 -pat 1009 -file $file -sheetfrom 1 -sheetto 2 # 将sheet1中第6行到第8行、第2列到第9列的数据复制到sheet2中 copy-data -startrow 6 -startcol 2 -endrow 8 -endcol 9 -pat 1018 -file $file -sheetfrom 1 -sheetto 2 # 将sheet1中第10行到第11行、第2列到第9列的数据复制到sheet2中 copy-data -startrow 10 -startcol 2 -endrow 11 -endcol 9 -pat 1023 -file $file -sheetfrom 1 -sheetto 2 这个是搜索sheet1中关键词吗,我现在要搜索sheet2中关键词 得到行信息后再把sheet1指定内容复制到sheet2指定区域,怎么修改呢
可以按照以下步骤修改代码:
1. 将函数名 `copy-data` 修改为 `copy-data-to-sheet2`,以表明该函数是将数据从 sheet1 复制到 sheet2。
2. 在函数中添加一个新的参数 `$sheetfromname`,用于指定要搜索的 sheet 名称。
3. 修改 `$wsfrom` 的赋值语句,使用 `$wb.Sheets.Item($sheetfromname)` 获取指定的 sheet 对象。
4. 修改 `$wsto` 的赋值语句,使用 `$wb.Sheets.Item(2)` 获取 sheet2 对象。
5. 修改 `$dsr` 的赋值语句,使用 `$wsto.Cells.Item($rown, $startcol)` 获取指定单元格的对象。
6. 将 `$dsr.Resize($sr.Rows.Count, $sr.Columns.Count)` 修改为 `$dsr.Resize($endrow-$startrow+1, $endcol-$startcol+1)`,以根据复制范围计算新区域的大小。
7. 在调用函数时,将参数 `-sheetfrom` 修改为 `-sheetfromname`,并指定要搜索的 sheet 名称。
修改后的代码如下:
```
function copy-data-to-sheet2([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file, [string]$sheetfromname, [int]$sheetto) {
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$wb = $excel.Workbooks.Open($file)
$wsfrom = $wb.Sheets.Item($sheetfromname)
$wsto = $wb.Sheets.Item(2)
$sr = $wsfrom.Range($wsfrom.Cells.Item($startrow, $startcol), $wsfrom.Cells.Item($endrow, $endcol))
$fc = $sr.Find($pat)
if ($fc -ne $null) {
$rown = $fc.Row
$dsr = $wsto.Cells.Item($rown, $startcol)
$dsr = $dsr.Resize($endrow-$startrow+1, $endcol-$startcol+1)
$sr.Copy()
$dsr.PasteSpecial(-4104)
}
$excel.DisplayAlerts = $false
$wb.Close($true)
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
}
$path = $PWD.Path
$file = "$path\a.xlsx"
# 将sheet1中包含关键词 1009 的行复制到sheet2中指定区域
copy-data-to-sheet2 -startrow 1 -startcol 2 -endrow 4 -endcol 9 -pat 1009 -file $file -sheetfromname "sheet1" -sheetto 2
# 将sheet1中包含关键词 1018 的行复制到sheet2中指定区域
copy-data-to-sheet2 -startrow 6 -startcol 2 -endrow 8 -endcol 9 -pat 1018 -file $file -sheetfromname "sheet1" -sheetto 2
# 将sheet1中包含关键词 1023 的行复制到sheet2中指定区域
copy-data-to-sheet2 -startrow 10 -startcol 2 -endrow 11 -endcol 9 -pat 1023 -file $file -sheetfromname "sheet1" -sheetto 2
```
For Each ws In Worksheets If ws.Name = banknm Then Application.DisplayAlerts = False Sheets(banknm).Delete Application.DisplayAlerts = True End If
这段代码的作用是删除名为“banknm”的工作表。首先,通过循环遍历每个工作表,如果找到名为“banknm”的工作表,就使用“Application.DisplayAlerts = False”关闭警告信息,然后删除该工作表,并使用“Application.DisplayAlerts = True”重新打开警告信息。这样做是为了避免在删除工作表时弹出确认删除的提示框,从而提高代码的执行效率。