数据表作为C#控制台应用程序中的邮件

时间:2014-07-10 11:15:03

标签: c# html css .net visual-studio-2010

我正在尝试使用以下代码从我的数据集创建html

    public static string getHtml(DataTable dataSet)
    {
        try
        {
            string messageBody = "<font>The following are the records: </font><br><br>";

            if (dataSet.Rows.Count == 0)
                return messageBody;
            string htmlTableStart = "<table style=\"float:left; border-collapse:collapse; text-align:center;\" >";
            string htmlTableEnd = "</table>";
            string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
            string htmlHeaderRowEnd = "</tr>";
            string htmlTrStart = "<tr style =\"color:#555555;\">";
            string htmlTrEnd = "</tr>";
            string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
            string htmlTdEnd = "</td>";


            foreach (DataColumn dc in dataSet.Columns)
            {

                messageBody += htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + dc + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;

                foreach (DataRow Row in dataSet.Rows)
                {
                    messageBody = messageBody + htmlTrStart;
                    messageBody = messageBody + htmlTdStart + Row["" + dc + ""] + htmlTdEnd;
                    messageBody = messageBody + htmlTrEnd;
                }
            }
            messageBody = messageBody + htmlTableEnd;


            return messageBody;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

enter image description here

我想以某种方式呈现 所有不同的表应该水平放置。我试过 float:left 但是当我使用这个html发送邮件时, float-left 不起作用

可以在代码中更改某些内容,以便表格以enter image description here

的形式出现

1 个答案:

答案 0 :(得分:1)

我完成了答案。这是将数据集转换为html的解决方案

public static string DataTableToHTML(DataTable dataSet)
        {
            mLogger.Error("DataTableToHTML -------> Start");
            try
            {
                string messageBody = "<font>Following corporate actions are not updated in Security Master: </font><br><br>";

                if (dataSet.Rows.Count == 0)
                    return messageBody;
                string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
                string htmlTableEnd = "</table>";
                string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
                string htmlHeaderRowEnd = "</tr>";
                string htmlTrStart = "<tr style =\"color:#555555;\">";
                string htmlTrEnd = "</tr>";
                string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
                string htmlTdEnd = "</td>";

                messageBody += htmlTableStart;
                messageBody += htmlHeaderRowStart;
                foreach (DataColumn dc in dataSet.Columns)
                {
                    messageBody += htmlTdStart + dc + htmlTdEnd;

                } messageBody += htmlHeaderRowEnd;

                foreach (DataRow dr in dataSet.Rows)
                {
                    messageBody = messageBody + htmlTrStart;
                    foreach (DataColumn dc in dataSet.Columns)
                    {

                        messageBody = messageBody + htmlTdStart + dr["" + dc + ""] + htmlTdEnd;
                    }
                    messageBody = messageBody + htmlTrEnd;
                }

                messageBody = messageBody + htmlTableEnd;

                mLogger.Error("DataTableToHTML -------> End");
                return messageBody;
            }
            catch (Exception ex)
            {
                mLogger.Error("Exception in DatatableToHTML" + ex);
                return null;
            }
        }