【发布时间】:2020-08-19 01:59:15
【问题描述】:
我想使用 powershell 突出显示 excel 单元格内部,可以更改字体颜色,但不能更改整个单元格颜色
这就是我的excel表格的样子,它检查单元格是否有“匹配”并据此改变颜色
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $False
$workbook = $excel.Workbooks.Open("C:\Users\test4.xls")
$sheet = $workbook.ActiveSheet
$xlCellTypeLastCell = 11
$used = $sheet.usedRange
$lastCell = $used.SpecialCells($xlCellTypeLastCell)
$row = $lastCell.row # goes to the last used row in the worksheet
$column = $lastCell.Column;
$i = 0
for ($J = 2; $J -le $column; $j++) {
$sheet.Cells.Item($i,$j).Interior.ColorIndex = 48
$sheet.Cells.Item($i,$j).Font.Bold=$True
}
for ($i = 2; $i -le $row; $i++) {
for($j = 5 ; $j -le $column ; $j++){
if (($sheet.cells.Item($i,$j).Value()) -like "*Matching") {
$sheet.Cells.Item($i,$j).Font.ColorIndex = 10
$sheet.Cells.Item($i,$j).Font.Bold = $true
}
if(($sheet.cells.Item($i,$j).Value()) -like "*Not Matching"){
$sheet.Cells.Item($i,$j).Font.ColorIndex = 3
$sheet.Cells.Item($i,$j).Font.Bold = $true
}
}
}
$workbook.SaveAs("C:\Users\output.xls")
$workbook.Close()
这是代码,唯一的问题是我给的时候
$sheet.Cells.Item($i,$j).Interior.ColorIndex = 48
这给了我一个例外
Exception from HRESULT: 0x800A03EC
【问题讨论】:
标签: excel powershell