【发布时间】:2020-08-11 21:09:31
【问题描述】:
我一直在尝试做某事。我有一个控制台应用程序,它查找一些文件,如果它们存在与否。因此,此应用程序会向我的电子邮件 ID 发送一封电子邮件。为电子邮件格式创建了一个普通的 SuccessBody.html。
现在,我正在尝试在 message.html 中动态添加一个 html 表,如下所示。
HTML
<body>
Hi All,
<br />
<br />
Please be informed that below files for this month <b>are available</b>.
<br />
<br />
<table align="center" ; width="1000" style="border-collapse:collapse; border-color:#17202A; text-align:center;">
<tr height="30" ; style="background-color:#CED8F6; color:#17202A; font-weight:bold; font-size:20px; font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<td colspan="2" ; style=" border-color:#FFFFFF; border-style:solid; border-width:thick; padding: 5px;">$Title$</td>
</tr>
<tr style="background-color:#143665; color:#FFFFFF; font-weight:bold; font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">$Columns$</tr>
$Rows$
</table>
<br />
<br />
<b>Please take necessary actions.</b>
<br />
<br />
Regards,
<br />
Ricky
</body>
我不想在代码本身中做这个表的设计,而是通过配置文件。而最关键的部分是while循环的设计。 $Title$、$Column$ 和 $Rows$ 是我想用 c# 中的一些代码替换的字符串,如下所示:
C#
public static void DataTableToHTML(DataTable Entries)
{
string mailbody = string.Empty;
if (Entries.Rows.Count.Equals(0))
{
string email_body_error = File.ReadAllText(ConfigurationManager.AppSettings["Body_Fail"]);
mailbody = email_body_error;
}
else
{
string email_body_success = string.Empty;
string html = string.Empty;
TextWriter writehtmlFile = new StreamWriter(Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName) + ".html");
string ConfigTitle = ConfigurationManager.AppSettings["Title"];
File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Title$", ConfigTitle);
for (int i = 0; i < Entries.Columns.Count; i++)
{
html += ConfigurationManager.AppSettings["ForColumns"] + Entries.Columns[i].ColumnName + "</td>";
}
File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Columns$", html);
//add rows
for (int i = 0; i < Entries.Rows.Count; i++)
{
html += ConfigurationManager.AppSettings["ForRows"];
for (int j = 0; j < Entries.Columns.Count; j++)
html += ConfigurationManager.AppSettings["RowData"] + Entries.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
writehtmlFile.WriteLine(html);
writehtmlFile.Flush();
writehtmlFile.Close();
email_body_success = File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Rows$", html);
mailbody = email_body_success;
}
SendEmail(mailbody, DateTime.Now);
}
我想在配置文件(AppConfig.xml)中配置这个表的设置,并从那里在上面的代码中使用它。
配置文件
<add key="FileName" value="File Name"/>
<add key="Date" value="Creation Date"/>
<add key ="ForColumns" value="<td style=\" border-color:#FFFFFF; border-style:solid; border-width:thick; padding: 5px;\">"/>
<add key="ForRows" value="<tr style =\"color:#17202A; font-weight:bold; font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;\" >"/>
<add key="RowData" value="<td style=\" border-color:#FFFFFF; background-color:#F3E2A9; border-style:solid; border-width:thick; padding: 5px;\">"/>
<add key="Body_Success" value="HTMLBody\SuccessBody.html"/>
Body_Success 是 SuccessBody.html 的位置 = App 的位置。
$Title$ 很容易被替换。但是,$Column$ 和 $Rows$ 并没有像我希望的那样被替换。如果有人能帮助我,那就太好了。
【问题讨论】:
-
ReadAllText返回一个字符串。你根本不存储它。 IIRC 编译器对此发出警告。
标签: c# html app-config