我正在尝试使用以下代码从我的数据集创建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;
}
}
我想用一种
所有不同的桌子都应该水平摆放。我试过float:left,但是当我使用这个html发送邮件时,float left不起作用
是否可以在代码中更改某些内容,以便表以hozizontally形式出现
最佳答案
我完成了我的回答。下面是将数据集转换为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;
}
}
关于c# - 在C#控制台应用程序中将数据表作为邮件发送,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24675099/