每次我找到搜索结果时,为每个excel行着色

每次我找到搜索结果时,为每个excel行着色,excel,powershell,Excel,Powershell,打开template.xls 搜索“ok”字符串 打印找到的每个结果的本地地址 color COLORDINDEX=3显示“ok”的每一行 保存excel工作表,关闭 我的问题是,虽然我在Write Host上打印了正确的搜索结果,但我无法更改找到的结果行的颜色 这就是我正在使用的代码 $file = 'C:\Users\bigadmin\Desktop\Projects\AutoUpdate\Template.xls' $xl = new-object -c excel.applicati

打开template.xls

搜索“ok”字符串

打印找到的每个结果的本地地址

color COLORDINDEX=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()

你能更改Excel文件中的任何内容吗?例如,将“hello world”放在目标单元格中?