excel - 每次我找到搜索结果时为每个 excel 行着色
问题描述
打开模板.xls
搜索“确定”字符串
为找到的每个结果打印地址本地
color ColorIndex = 3 出现“ok”的每一行
保存excel表格,关闭
我的问题是,尽管我在 Write-Host 上打印出正确的搜索结果,但我无法更改找到的结果行的颜色!!
这是代码工作。
$file = 'C:\Users\bigadmin\Desktop\Projects\AutoUpdate\Template.xls'
$xl = new-object -c excel.application
$wb1 = $xl.workbooks.open($file, $null, $true)
$ws1 = $wb1.WorkSheets.item(1)
$ws1.activate()
$Range = $WS1.Range("A100:D110")
$Target = $Range.Find("OK")
$First = $Target
Do
{
Write-Host $Target.AddressLocal()
$Target.EntireRow.Interior.ColorIndex = 3
$Target = $Range.FindNext($Target)
}
While ($Target -ne $NULL -and $Target.AddressLocal() -ne
$First.AddressLocal())
$wb1.Save()
$wb1.close($true)
$xl.quit()
spps -n excel
解决方案
尝试这个:
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$file = 'C:\Users\bigadmin\Desktop\Projects\AutoUpdate\Template.xls'
$searchFor = 'Ok'
$colorIndex = 3
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.ScreenUpdating = $true
$workbook = $excel.Workbooks.Open( $file ,$null, $false )
$ws1 = $workbook.WorkSheets.item(1)
[void]$ws1.Activate()
$searchRange = $ws1.Range("A100:D110")
$searchResult = $searchRange.Find( $searchFor, [System.Type]::Missing, [System.Type]::Missing, [Microsoft.Office.Interop.Excel.XlLookAt]::xlWhole, [Microsoft.Office.Interop.Excel.XlSearchOrder]::xlByColumns )
while( $searchResult -and $ws1.Cells( $searchResult.Row, $searchResult.Column ).EntireRow.Interior.ColorIndex -ne $colorIndex ) {
$ws1.Cells( $searchResult.Row, $searchResult.Column ).EntireRow.Interior.ColorIndex = $colorIndex
$searchResult = $searchRange.FindNext( $searchResult )
}
[void]$workbook.Save()
[void]$workbook.Close()
[void]$excel.Quit()
推荐阅读
- php - 在 PHP 项目中推荐了多少 SQL 连接?理想的 SQL 连接数是多少?
- entity-framework - 带有连接、分组和求和的 EF Linq 查询
- python-3.x - Django wsgiref/handlers.py 像 python 3.9 一样抛出异常
- linux - 如何忽略 Puppet 中的额外符号
- discord - 一个删除所有消息的机器人,除了说“土豆”的消息
- loops - 使用 for 循环构造具有自回归结构的矩阵
- c++ - cpp文件中的“重新定义类”错误
- python - 打印此图像的正确方法是什么?
- excel - VBA:我得到“对象变量或未设置块变量”
- ios - 使用 Firebase 和 SwiftUI