【Wonder原创】DataTable转成Table格式的HTML字符串

1 public static string ConvertDataTableToHTML(DataTable dt,int[] notShow)
2 {
3 StringBuilder htmlTable = new StringBuilder();
4 if (dt != null)
5 {
6 htmlTable.Append("<style>#container{1000px;text-align:left;}");
7 htmlTable.Append("#uc_box{99%;margin:5px;border:1px solid #036;text-align:left;}");
8 htmlTable.Append("#uc_forms{margin: 12px;font-size:12px;color:#036;100%;}</style>");
9 htmlTable.Append("<div id='container'><div id='uc_box'><fieldset><table id='uc_forms'>");
10
11 #region Table Header
12 htmlTable.Append("<tr style='background-color:Azure'><td></td>");
13 for (int i = 0; i < dt.Columns.Count; i++)
14 {
15 if ( notShow.Where(o=>o.Equals(i)).Count() == 0 )
16 htmlTable.Append(string.Format("<td>{0}</td>", dt.Columns[i].ColumnName));
17 }
18 htmlTable.Append("</tr>");
19 #endregion
20
21 #region Table Data
22 if (dt.Rows.Count > 0)
23 {
24 for (int i = 0; i < dt.Rows.Count; i++)
25 {
26 htmlTable.Append("<tr style='background-color:GhostWhite;height:15px;'>");
27 htmlTable.Append(string.Format("<td>{0}</td>", i+1));
28 for (int j = 0; j < dt.Columns.Count; j++)
29 {
30 if (notShow.Where(o => o.Equals(j)).Count() == 0)
31 htmlTable.Append(string.Format("<td>{0}</td>", dt.Rows[i][j].ToString()));
32 }
33 htmlTable.Append("<tr>");
34 }
35 }
36 else
37 htmlTable.Append(string.Format("<tr style='background-color:GhostWhite'><td colspan='{0}'>No Data Found</td><tr>",dt.Columns.Count+1-notShow.Count()));
38
39 #endregion
40
41 htmlTable.Append("</table></fieldset></div></div>");
42 }
43 return htmlTable.ToString();
44 }
原文地址:https://www.cnblogs.com/wonder315/p/ConvertDataTableToHTML.html