【问题标题】:Fill HTML template and send the result by email填写 HTML 模板并通过电子邮件发送结果
【发布时间】:2016-02-13 17:45:44
【问题描述】:

我有一个 HTML 模板,我有一个返回许多数据的查询。 我想填写我的 html 并通过电子邮件发送 在下面有我的代码。我想知道如何在datatable 中填写我的数据行

到目前为止我已经这样做了:

public void GetInfo()
{
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionStrings"].ConnectionString;
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    string query = "select X,Y,Z,E,U,S,M " +
                    "from MyTable where M = 0";
    DataTable dt = new DataTable();
    using (SqlCommand command = new SqlCommand(query, new SqlConnection(connectionString)))
    {
        command.CommandTimeout = 360;
        command.Connection.Open();
        dt.Load(command.ExecuteReader());
        command.Connection.Close();
    }
    List<XReportData> lvrd = new List<XReportData>();
    if (dt.Rows.Count > 0)
    {
        foreach (DataRow row in dt.Rows)
        {
            XReportData rd = new XReportData();

            rd.X = row["X"].ToString();
            rd.Y = row["Y"].ToString();
            rd.Z = row["Z"].ToString();
            rd.E = row["E"].ToString();
            rd.U = row["U"].ToString();
            rd.S = row["S"].ToString();
            rd.M = row["M"].ToString();

            lvrd.Add(rd);
        }


        dt.Dispose();
    }
}

这是我的 HTML 模板:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Untitled Document</title>
    <style type="text/css">
        To AVOID A LONG CODE I DELETED THIS PART HERE
    </style>
</head>
<body>
    <div class="newlayout_middlepanel_title">
        <div id="contentDescriptionDiv" class="newlayout_title">
            <span id="ctl00_ContentDescription_Label1">MY MESSAGE</span>
            <hr width="100%" style="border: solid 1px #f8f8f8">
        </div>
    </div>
    <div style="text-align: left" class="ResultBox">
        <div>
            <table cellpadding="5" cellspacing="0" border="1" style="border-collapse: collapse;" class="GridTable">
                <tbody>
                    <tr style="color: White; background-color: #5D7B9D; font-size: Small; font-weight: bold;">
                        <th scope="col" class="auto-style1">Info 1</th>
                        <th scope="col" class="auto-style2">Info 2</th>
                        <th scope="col">Info 3</th>

                    </tr>
                    [righeHtml]
                    <tr align="center" style="color: White; background-color: #284775;">
                        <td colspan="6">
                            <div style="height: 10px;"></div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

它看起来像这样: 我的留言

_______________________________________________
 |      Info 1     |    Info2    |    Info3    |
 -----------------------------------------------
 |  rd.x , rd.E,...|  rd.Y,rd.M  |rd.S,rd.Z,...| 
   and each row with my data

【问题讨论】:

    标签: c# html datatable html-email


    【解决方案1】:

    您可以使用 xslt 生成 html。将数据表(XReport 数据对象)序列化为 xml。您可以通过描述如何将此数据呈现到 xslt 中来生成 html。

    假设你有一堂课

    class XReport {
         public double X { get; set;}
         public double Y { get; set;}
         public double Z { get; set;}
    }
    

    在这个类的List of objects序列化之后......你可以将类的属性(将是列)引用到XSLT中,如下所示。

    <table>
            <xsl:for-each select="XReports">
                <tr>
                    <td>
                        <xsl:value-of select="X"/>
                    </td>
                    <td>
                        <xsl:value-of select="Y"/>
                    </td>
                    <td>
                        <xsl:value-of select="Z"/>
                    </td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
    </table>
    

    使用此 XSLT 转换序列化的 XML 后,您应该获得所需的 HTML,您可以通过电子邮件发送它:-)

    https://msdn.microsoft.com/en-us/library/bb399419(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 2018-08-19
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      相关资源
      最近更新 更多