1: private static string GenerateTable(DataTable source)
2: {
3: HtmlTable table = new HtmlTable();
4:
5: HtmlTableRow headerRow = new HtmlTableRow();
6:
7: for (int x = 0; x < source.Columns.Count; x++)
8: {
9: HtmlTableCell th = new HtmlTableCell("th");
10: th.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#337490");
11: th.Style.Add(HtmlTextWriterStyle.Color, "#FFFFFF");
12: th.InnerText = source.Columns[x].ColumnName;
13: headerRow.Cells.Add(th);
14: }
15:
16: table.Rows.Add(headerRow);
17:
18: foreach (DataRow x in source.Rows)
19: {
20: HtmlTableRow tableRow = new HtmlTableRow();
21: for (int y = 0; y < source.Columns.Count; y++)
22: {
23: System.Type rowType;
24: rowType = x[y].GetType();
25: HtmlTableCell td = new HtmlTableCell();
26: switch (rowType.ToString())
27: {
28: case "System.String":
29: string XMLstring = x[y].ToString();
30: XMLstring = XMLstring.Trim();
31: XMLstring = XMLstring.Replace("&", "&");
32: XMLstring = XMLstring.Replace(">", ">");
33: XMLstring = XMLstring.Replace("<", "<");
34: td.InnerText = XMLstring;
35: break;
36: case "System.DateTime":
37: DateTime XMLDate = (DateTime)x[y];
38: string XMLDatetoString = ""; //Excel Converted Date
39: XMLDatetoString = XMLDate.Year.ToString() +
40: "-" +
41: (XMLDate.Month < 10 ? "0" +
42: XMLDate.Month.ToString() : XMLDate.Month.ToString()) +
43: "-" +
44: (XMLDate.Day < 10 ? "0" +
45: XMLDate.Day.ToString() : XMLDate.Day.ToString()) +
46: "T" +
47: (XMLDate.Hour < 10 ? "0" +
48: XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) +
49: ":" +
50: (XMLDate.Minute < 10 ? "0" +
51: XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) +
52: ":" +
53: (XMLDate.Second < 10 ? "0" +
54: XMLDate.Second.ToString() : XMLDate.Second.ToString()) +
55: ".000";
56:
57: td.InnerText = XMLDatetoString;
58:
59: break;
60: case "System.Boolean":
61: td.InnerText = x[y].ToString();
62: break;
63: case "System.Int16":
64: case "System.Int32":
65: case "System.Int64":
66: case "System.Byte":
67: td.InnerText = x[y].ToString();
68: break;
69: case "System.Decimal":
70: case "System.Double":
71: td.InnerText = string.Format("{0:n}", x[y]);
72: break;
73: case "System.DBNull":
74: td.InnerText = string.Empty;
75: break;
76: }
77:
78: tableRow.Cells.Add(td);
79:
80: }
81:
82:
83: table.Rows.Add(tableRow);
84: }
85:
86:
87: StringWriter sw = new StringWriter();
88: HtmlTextWriter htw = new HtmlTextWriter(sw);
89:
90: table.RenderControl(htw);
91:
92:
93: return sw.ToString();
94:
95: }