如何将格式应用到我的表?

时间:2015-07-08 15:20:56

标签: html powershell

我正在尝试通过电子邮件将表格格式应用到我的表格中,但似乎无法确定如何执行此操作。我很确定问题与$ html变量的设置方式有关,或者当我尝试将$ EmailTable设置为$ html并使用$ a作为表格格式时。任何帮助表示赞赏!

$ProgramA = "A-1"
$MonikerA = "A-2"
$CountA = "1"
$ProgramB = "B-1"
$MonikerB = "B-2"
$CountB = "2"
$ProgramC = "C-1"
$MonikerC = "C-2"
$CountC = "3"

# Create a DataTable
$table = New-Object system.Data.DataTable "TestTable"
$col1 = New-Object system.Data.DataColumn Program,([string])
$col2 = New-Object system.Data.DataColumn Moniker,([string])
$col3 = New-Object system.Data.DataColumn Cases,([string])
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)

# Add content to the DataTable
$row = $table.NewRow()
$row.Program = $ProgramA
$row.Moniker = $MonikerA
$row.Cases = $CountA
$table.Rows.Add($row)
$row = $table.NewRow()
$row.Program = $ProgramB
$row.Moniker = $MonikerB
$row.Cases = $CountB
$table.Rows.Add($row)
$row = $table.NewRow()
$row.Program = $ProgramC
$row.Moniker = $MonikerC
$row.Cases = $CountC
$table.Rows.Add($row)
$row = $table.NewRow()

# Create an HTML version of the DataTable
$html = "<table><tr><td>Program</td><td>Moniker</td><td>Cases</td></tr>"
foreach ($row in $table.Rows)
{ 
    $html += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td><td>" +     $row[2] + "</td></tr>" 
}
$html += "</table>"

# Here is the formatting I'm trying to apply (which doesn't work)
$a = "<style>BODY{font-family: Verdana; font-size: 9pt;}"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; }"  
$a = $a + "TH{border-width: 2px;padding: 7px;border-style: solid;border-color: black;background-color:lightblue;padding-right: 2px;}" 
$a = $a + "TD{border-width: 2px;padding: 5px;border-style: solid;border-color: black;background-color:white; padding-right: 2px;}"

$a = $a + "</style>"

$EmailTable = $html ConvertTo-HTML -head $a 

1 个答案:

答案 0 :(得分:1)

$EmailTable = $html ConvertTo-HTML -head $a

这不是ConvertTo-Html的工作方式。语句不仅在|之前缺少ConvertTo-Html,而且$html已经包含(手动构造的)HTML表。 ConvertTo-Html的目的是将对象列表转换为HTML表格。将上述内容更改为:

$EmailTable = $table | ConvertTo-Html -Head $a