StringBuilder的AppendFormat方法创建表

时间:2012-09-10 10:42:36

标签: c# asp.net

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

3 个答案:

答案 0 :(得分:4)

希望您可以使用此示例来了解它应该如何。 如果没有,请告诉我。会帮助你。

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属性为true,因为你已经在使用HTML了

答案 1 :(得分:0)

做这样的事情

    String to, subject, message;
    bool isHtml;
    isHtml = true;
    StringBuilder sbEmail = new StringBuilder();

    to = "abc@abc.com";
   // string url = "xxxxx";

    //  string refurl = Request.UrlReferrer.ToString();
    subject = "Some subject";

    // Keep Option for Otehr Language also by defining predefined 
    // Hidden Labels for Message in L1, L2, L3, L4, L5 --
    // Also add dir tag to the Table
    sbEmail.Append("<table><tr><td style='font-family: Arial; font-size: 10pt;'>");
    sbEmail.Append("Hello,");
    sbEmail.Append("<br><br>");
    sbEmail.Append("");
    sbEmail.Append("Following Comments has been posted");
    sbEmail.Append("<br><br>");
    sbEmail.Append(strComment);
    sbEmail.Append("<br><br>");
    sbEmail.Append("Article Title :");
    sbEmail.Append("<br>");
    sbEmail.Append(strArticleTitle);
    sbEmail.Append("<br><br>");
    sbEmail.Append("Name : " + strName);
    sbEmail.Append("<br>");
    sbEmail.Append("Country : " + strCountry);
    sbEmail.Append("<br><br>");
    sbEmail.Append("Please logon to admin section of website to activate this comments");
    sbEmail.Append("<br>");
    sbEmail.Append("http://xxxxx");
    sbEmail.Append("</td></tr></table>");
    message = sbEmail.ToString();

答案 2 :(得分:0)

要获得所需的边框,您应该在表格和标签上都加入内联样式。 <table cellpadding='5' cellspacing='2' style='border:1px solid black;'> <td style='border: 1px solid black;'> 这应该给出预期的样式。