通过Excel打开CSV文件是一种非常快速的数据导入方式,但是CSV文件在ExceL中永远是“白底黑字”。因为CSV数据只能裸奔,没有办法给单元格着色。
还有一种办法,你可以将自己的数据包装成HTML,然后把HTML填充到Excel中,如此一来生成一个张彩色的报表不在话下,你甚至还可以指定字体大小。
下面的例子会在Excel中生成一张状态报表,启用的服务显示为绿色,停止的服务显示为红色。
#requires -Version 1
# write HTML intro code
$begin =
{
'<table>'
'<tr>'
'<th>DisplayName</th><th>Status</th><th>Required</th><th>Dependent</th>'
'</tr>'
}
# this is executed for each data object
$process =
{
if ($_.Status -eq 'Running')
{
$style = '<td style="color:green; font-family:courier">'
}
else
{
$style = '<td style="color:red">'
}
'<tr>'
'{0}{1}</td><td>{2}</td><td>{3}</td><td>{4}</td>' -f $style, $_.DisplayName, $_.Status, ($_.RequiredServices -join ','), ($_.DependentServices -join ',')
'</tr>'
}
# finish HTML fragment
$end =
{
'</table>'
}
$Path = "$env:temp\tempfile.html"
# get all services and create custom HTML report
Get-Service |
ForEach-Object -Begin $begin -Process $process -End $end |
Set-Content -Path $Path -Encoding UTF8
# find Excel, and feed HTML report into Excel
$Excel = Resolve-Path "C:\Program Files*\Microsoft Office\Office*\EXCEL.EXE" |
Select-Object -First 1 -ExpandProperty Path
& $Excel $Path
原文链接:Creating Colored Excel Reports
本文链接: https://www.pstips.net/creating-colored-excel-reports.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
