如何将对象格式化为漂亮的HTML表? [英] How to format an object into a nice HTML table?

查看:84
本文介绍了如何将对象格式化为漂亮的HTML表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个脚本

script1具有以下内容:

script1 has the following:

$exportObject = New-Object System.Collections.ArrayList
$exportObject | Select-Object

在script2中,我正在调用script1做某事并将输出传递到-ov

in script2, i am calling script1 to do something and piping the output to an -ov

& "script1.ps1" -ov $outputValue
$outputValue

这就是我得到的

现在,我正尝试在电子邮件正文中发送该变量输出.

Now I am trying to send that variable output in an email body.

Send-MailMessage -From $FromEm -Subject $Subject -To "user@domain.com" -Body $outputValue -BodyAsHtml -SmtpServer $SmtpServer -Port $Port -Credential $Creds -UseSsl

Send_mailMessage将不接受像这样的ArrayList变量,所以我要先转换为HTML,然后转换为字符串

Send_mailMessage won't accept ArrayList variable like this, so i am converting to HTML then string

$outputValue = $outputValue | ConvertTo-HTML <# -As Table #> | Out-String

这会在电子邮件正文中发送以下内容:

this sends the following in the email body:

很好,但我想使其看起来像桌子,带有边框样式等.

its good but i would like to make it look like a table, with borders styling, etc.

我该怎么做?

我在想这样的事情:

$outputValue = $outputValue | ConvertTo-HTML | foreach-object{if($outValue.header) {<th></th>} else <td></td>} | Out-String

尽管那是我的初衷,但不确定如何准确地做到这一点

though thats a rough idea of what i have in mind and not sure how to do it exactly

推荐答案

您当然可以通过HTML样式发挥自己的创造力. 下面的代码按如下方式设置表格样式:

You can be as creative as you like with HTML styling of course. Below code styles the table like this:

# just some fake data here
$exportObject = @(
    [PSCustomObject]@{
        'Server' = 'Server1.com'
        'Cube' = 'Cube1'
        'Connection Details' = 'Connection changed!'
    },
    [PSCustomObject]@{
        'Server' = 'Server2.com'
        'Cube' = 'Cube2'
        'Connection Details' = 'Connection Unchanged!'
    },
        [PSCustomObject]@{
        'Server' = 'Server3.com'
        'Cube' = 'Cube3'
        'Connection Details' = 'Connection changed!'
    }
)

function ConvertTo-HTMLTable ($obj) {
    # Accepts a System.Data.DataTable object or an array of PSObjects and converts to styled HTML table

    # add type needed to replace HTML special characters into entities
    Add-Type -AssemblyName System.Web

    $sb = New-Object -TypeName System.Text.StringBuilder
    [void]$sb.AppendLine('<table>')
    if ($null -ne $obj) {
        if (([object]$obj).GetType().FullName -eq 'System.Data.DataTable'){
            # it is a DataTable; convert to array of PSObjects
            $obj = $obj | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors
        }
        $headers = $obj[0].PSObject.Properties | Select -ExpandProperty Name
        [void]$sb.AppendLine('<thead><tr>')
        foreach ($column in $headers) {
            [void]$sb.AppendLine(('<th>{0}</th>' -f [System.Web.HttpUtility]::HtmlEncode($column)))
        }
        [void]$sb.AppendLine('</tr></thead><tbody>')
        $row = 0
        $obj | ForEach-Object {
            # add inline style for zebra color rows
            if ($row++ -band 1) {
                $tr = '<tr style="background-color: {0};">' -f $oddRowBackColor
            } 
            else {
                $tr = '<tr>'
            }
            [void]$sb.AppendLine($tr)
            foreach ($column in $headers) {
                [string]$val = $($_.$column)
                if ([string]::IsNullOrWhiteSpace($val)) { 
                    $td = '<td>&nbsp;</td>' 
                } 
                else { 
                    $td = '<td>{0}</td>' -f [System.Web.HttpUtility]::HtmlEncode($val)
                }
                [void]$sb.Append($td)
            }
            [void]$sb.AppendLine('</tr>')
        }

        [void]$sb.AppendLine('</tbody>')
    }
    [void]$sb.AppendLine('</table>')

    return $sb.ToString()
}


$headerBackColor = '#4F81BD'  # backgroundcolor for column headers
$oddRowBackColor = '#DCE6F1'  # background color for odd rows

$style = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Report</title>
    <meta name="generator" content="PowerShell" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
    body {
        font-family: Verdana, Arial, Geneva, Helvetica, sans-serif;
        font-size: 12px;
        color: black;
    }
    table, td, th {
        border-color: black;
        border-style: solid;
        font-family: Verdana, Arial, Geneva, Helvetica, sans-serif;
        font-size: 11px;
    }
    table {
        border-width: 0 0 1px 1px;
        border-spacing: 0;
        border-collapse: collapse;
    }

    td, th {
        margin: 0;
        padding: 4px;
        border-width: 1px 1px 0 0;
        text-align: left;
    }
    th {
        color: white;
        background-color: $headerBackColor;
        font-weight: bold;
    }
    </style>
"@

$body = '{0}</head><body>{1}</body></html>' -f $style, (ConvertTo-HTMLTable $exportObject)

Send-MailMessage -From $FromEm -Subject $Subject -To "user@domain.com" -Body $body -BodyAsHtml -SmtpServer $SmtpServer -Port $Port -Credential $Creds -UseSsl

可以找到两个模仿MS Word Grid Table 5 Dark Accent 5的表样式示例,

Two examples to do table styling that mimic the MS Word Grid Table 5 Dark Accent 5 can be found here

希望有帮助

这篇关于如何将对象格式化为漂亮的HTML表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆