【问题标题】:Powershell : Highlight excel cell Interior using conditionPowershell:突出显示excel单元格内部使用条件
【发布时间】: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


    【解决方案1】:

    从我的 cmets 扩展。

    PowerShell Excel and Conditional Formatting

    # Quick Find the Top 10 Months
    $xl = Open-ExcelPackage -Path $xlfile
    Add-ConditionalFormatting -Worksheet $xl.StoresTop10Sales -Address $xl.StoresTop10Sales.dimension.address -RuleType Top -ForegroundColor white -BackgroundColor green -ConditionValue 10
    Close-ExcelPackage $xl -Show
    
    # Databars
    Add-ConditionalFormatting -Worksheet $xl.StoresSalesDataBar -Address $xl.StoresSalesDataBar.dimension.address -DataBarColor Red
    
    # Color Scales
    Add-ConditionalFormatting -Worksheet $xl.StoresSalesTwoColorScale -Address $xl.StoresSalesDataBar.dimension.address -RuleType TwoColorScale
    

    使用这个模块:ImportExcel 5.4.4

    AddConditionalFormatting.ps1
    

    【讨论】:

      【解决方案2】:

      所以,如果我理解正确,您是说“我想用 Powershell 做 Excel 可以使用条件格式自动执行的操作”,您的意思是:

      • 您不了解条件格式,或者:
      • 条件格式意味着修改 Excel 文件中的某些内容,这是您无法做到的,因此使用了 Powershell。像这样,您的问题变成“如何通过 Powershell 配置条件格式?”。

      是哪一个?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 2021-12-22
      • 1970-01-01
      相关资源
      最近更新 更多