Fill HTML template and send the result by email

时间:2015-11-12 11:52:19

标签: c# html datatable html-email

I have an HTML template and I have a query that returns many data. I want to fill my html and send it by email in below there are my code. I want to know how can I fill the rows my my data in my datatable

I have done this so far:

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();
    }
}

and this is my HTML template :

<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>

and it would look like somehow like this : MY MESSAGE

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

1 个答案:

答案 0 :(得分:0)

You can use an xslt to generate html. Serialise the data table (XReport data objects) into xml. You can generate an html by describing how to render this data into the xslt.

Lets say you have a class

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

After the serialisation of the List of objects of this class... You can refer to the properties (which will be the columns) of the class into the XSLT as below.

<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>

After you transform the serialsed XML using this XSLT, you should get the desired HTML, which you can email :-)

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