I’m creating an excel spreadsheet using IMPORTEXCEL module. I’m adding a data bar on a column but I cannot find the correct syntax to apply a “solid border” (and ideally, the colour of the solid border).
I’ve searched everywhere and can’t find a command that works.
$rule.Databar.Border.BorderType = "Solid" # Equivalent to Excel's Solid Border
$rule.Databar.Border.Color.Color = [System.Drawing.Color]::Black # Set the border color to Black (or any other color)
Nothing seems to work. Could you offer any remedy to my issue?.
Below is a simple script to output a list of svchost processes to an Excel workbook with green data bars applied to column B, which I’ve been using as a test:
cls
Import-Module ImportExcel # running version 7.8.10
$ExportFilePath="C:\temp\DataBarTestFile1.xlsx"
If(Test-path $ExportFilePath){Remove-Item $ExportFilePath}
$processes = get-process | where-object {$_.ProcessName -eq "svchost"} | Select-Object Name, Handles
write-host ("There were "+$processes.count+ " svchost processes found")
write-host "Exporting data to Excel workbook, please wait..."
$excel = $Processes | Export-Excel -Path $ExportFilePath -WorksheetName "svchost processes" -AutoSize -BoldTopRow -FreezeTopRow -AutoFilter -TableStyle Medium2 -append -PassThru
$Range=("B2:B"+($Processes.count+1))
$rule=Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets["svchost processes"] -Range $Range -DataBarColor Green
$excel.Save() # Save the final changes to the workbook
Close-ExcelPackage $excel
That at least explains why I’ve had so much trouble finding a working solution.
I’ve so far managed to avoid using a ComObject, and using ImportExcel instead for flexibility of use but thanks for the alternative method. It may now come to that.
Could you tell me what I need to add to your ComObject script if I wish to apply a minimum number value of 2017 and a maximum number value of 2023 to the conditional formatting rather than the default of minimum automatic and maximum automatic.
I’d also like to wrap an IF statement around it to detect whether Excel is installed before executing the code but I don’t know if that is possible.