DataTable转成Html(可用于将表格以html邮件格式发送)

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Net.Mail;
 5 using System.Data;
 6 using System.IO;
 7 
 8 namespace WatchDog
 9 {
10     //发送带表格邮件
11     public class HtmlMailBodyBuilder
12     {
13         public static string LargeMailBody="";//初始化多表格邮件内容
14         //单一表格邮件内容
15         public static string BuildSingleTable(DataTable data)
16         {
17             string mailBody = "<p style=\"font-size: 10pt\">以下内容由系统自动发送,请勿直接回复。</p><table cellspacing=\"0\" cellpadding=\"0\" border=\"1\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 1px;\">";
18             mailBody += "<div align=\"left\">";
19             mailBody += "<tr>";
20             for (int hcol = 0; hcol < data.Columns.Count; hcol++)
21             {
22                 mailBody += "<td bgcolor=\"aa8f23\">&nbsp;";
23                 mailBody += data.Columns[hcol].ColumnName;
24                 mailBody += "&nbsp;</td>";
25             }
26             mailBody += "</tr>";
27             
28             for (int row = 0; row < data.Rows.Count; row++)
29             {
30                 mailBody += "<tr>";
31                 for (int col = 0; col < data.Columns.Count; col++)
32                 {
33                     mailBody += "<td bgcolor=\"FFFFFF\">&nbsp;";
34                     mailBody += data.Rows[row][col].ToString();
35                     mailBody += "&nbsp;</td>";
36                 }
37                 mailBody += "</tr>";
38             }
39             mailBody += "</table>";
40             mailBody += "</div>";
41             return mailBody;
42         }
43         
44         /// <summary>
45         /// 多表格邮件内容
46         /// SendMail.SendLargeMsg(t1, "测试内容1);
47         /// SendMail.SendLargeMsg(t2, "测试内容2");
48         /// SendMail.Mail.SendStrMail("<p style=\"font-size: 10pt\">以下内容由系统自动发送,请勿直接回复。</p>" + SendMail.LargeMailBody);
49         /// </summary>
50         /// <param name="data"></param>
51         /// <param name="title"></param>
52 
53         public static void BuildMultiTable(DataTable data, string title = "")
54         {
55             if (title != "")
56                 LargeMailBody += "<p style=\"font-size: 10pt\">"+title+"</p>";
57 
58             LargeMailBody += "<div align=\"center\">";
59             LargeMailBody += "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">";
60 
61             LargeMailBody += "<tr>";
62             for (int hcol = 0; hcol < data.Columns.Count; hcol++)
63             {
64                 LargeMailBody += "<td bgcolor=\"999999\">&nbsp;&nbsp;&nbsp;";
65                 LargeMailBody += data.Columns[hcol].ColumnName;
66                 LargeMailBody += "&nbsp;&nbsp;&nbsp;</td>";
67             }
68             LargeMailBody += "</tr>";
69 
70             for (int row = 0; row < data.Rows.Count; row++)
71             {
72                 LargeMailBody += "<tr>";
73                 for (int col = 0; col < data.Columns.Count; col++)
74                 {
75                     LargeMailBody += "<td bgcolor=\"dddddd\">&nbsp;&nbsp;&nbsp;";
76                     LargeMailBody += data.Rows[row][col].ToString();
77                     LargeMailBody += "&nbsp;&nbsp;&nbsp;</td>";
78                 }
79                 LargeMailBody += "</tr>";
80             }
81             LargeMailBody += "</table><br>";
82             LargeMailBody += "</div>";
83         }
84     }
85     
86 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。