我对 StringBuilder 类的 AppendFormat 方法有疑问。 我正在创建表格并在字符串生成器对象中附加数据以将其作为邮件发送,但是当我看到我发送的邮件时,它看起来不像表格,它的标题和相应的内容都错放了。我希望表格用行分隔,因为它通常在 Microsoft Word 的表格中。 我怎样才能实现它.. 我正在使用以下代码:Body 是一个 StringBuilder 对象
if ( dic1 != null )
{
//Body.AppendFormat("Client: " + cl + " Success. " + dic1.Count + " :");
Body.AppendFormat("<br/><table>");
Body.AppendFormat("<h1><tr><td>#</td><td>Files Name</td></tr></h1>");
int count = 1;
foreach ((KeyValuePair<string, string> pair in dic1)
{
if (!String.IsNullOrEmpty(pair.Key))
{
Body.AppendFormat("<tr><td>"+count.ToString()+"</td><td>" + pair.Key + "</td><td> " + pair.Value + "</td></tr>");
count++;
//Body.Append( );
}
}
Body.AppendFormat("</table>");
以下是我在收件箱中收到的输出。
# File Name Error
1 txt1.txt Loading File 'txt1.txt' failed: The specified File already exists in the system
2 txt2.txt Loading File 'txt2.txt' failed: The specified File already exists in the system
3 txt3.txt Loading File 'txt3.txt' failed: The specified File already exists in the system
最佳答案
希望你能通过这个例子来理解它应该是怎样的。 如果没有,请告诉我。会帮助你。
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<table>");
sb.AppendFormat("<tr><td>Request Name:</td><td>{0}</td></tr>", txtBugName.Text.Trim());
sb.AppendFormat("<tr><td>Category:</td><td>{0}</td></tr>", ddlModule.SelectedValue);
sb.AppendFormat("<tr><td>Sub-Category:</td><td>{0}</td></tr>", ddlPage.SelectedValue);
sb.AppendFormat("<tr><td>Description:</td><td>{0}</td></tr>", txtComments.Text.Trim());
sb.AppendFormat("<tr><td>Email is:</td><td>{0}</td></tr>", txtemail.Text.Trim());
sb.Append("<table>");
然后我假设 IsBodyHtml 属性为真,因为您已经在使用 HTML
关于c# - StringBuilder 的 AppendFormat 方法创建 Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12350015/