昨天我们做的Table具有了Print方法,可以简单的打印数据。
现在我需要一个Table,可以把Table中的数据以Html的Table的格式输出。
而我只需要重写Print方法
现在我需要一个Table,可以把Table中的数据以Html的Table的格式输出。
而我只需要重写Print方法
1
public class HtmlTable : Table
2
{
3
public HtmlTable(string reportName, ColumnCollection cols)
4
: base(reportName, cols)
5
{
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
}
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
public override void Print()
10
{
11
System.Text.StringBuilder sb = new StringBuilder();
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
//输出Table的开始标记
14
sb.Append("<table>\n");
15
//输出表头
16
sb.Append("<thead>\n<tr>\n");
17
foreach (Column col in this.Columns)
18
{
19
sb.AppendFormat("<th>{0}</th>\n", col.ColumnName);
20
}
21
sb.AppendLine("</tr>\n</thead>");
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
//输出数据行
25
foreach (Row row in this.Rows)
26
{
27
sb.AppendLine("<tr>");
28
foreach (object obj in row.ItemArray)
29
{
30
sb.AppendFormat("<td>{0}</td>\n", obj);
31
//System.Console.Write("{0}\t", obj);
32
}
33
sb.AppendLine("</tr>");
34
System.Console.WriteLine();
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
//输出Table的结束标记
38
sb.Append("</table>\n");
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
System.Console.WriteLine(sb.ToString());
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
}
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
现在我将SaleReport改继承HtmlTable(参见Table是怎样炼成的:SaleReport Table的继承者 ),其他都不要改动。
然后我们运行该SaleReport
1
SaleReport report = new SaleReport("销售台帐");
2
report.AddRecord("Alex", "Phone", 2600);
3
report.AddRecord("Alex", "PC", 4560);
4
report.AddRecord("Alex", "Table", 234);
5
report.AddRecord("Sidney", "Phone", 2100);
6
report.AddRecord("Sidney", "TV", 4500);
7
report.AddRecord("Tom", "oven", 300);
8
report.AddRecord("Leo", "oven", 240);
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
report.Print();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行的结果是
1
<table>
2
<thead>
3
<tr>
4
<th>序号</th>
5
<th>姓名</th>
6
<th>商品名称</th>
7
<th>日期</th>
8
<th>数量</th>
9
</tr>
10
</thead>
11
<tr>
12
<td>1</td>
13
<td>Alex</td>
14
<td>Phone</td>
15
<td>2007-2-14 20:39:31</td>
16
<td>2600</td>
17
</tr>
18
<tr>
19
<td>2</td>
20
<td>Alex</td>
21
<td>PC</td>
22
<td>2007-2-14 20:39:31</td>
23
<td>4560</td>
24
</tr>
25
<tr>
26
<td>3</td>
27
<td>Alex</td>
28
<td>Table</td>
29
<td>2007-2-14 20:39:31</td>
30
<td>234</td>
31
</tr>
32
<tr>
33
<td>4</td>
34
<td>Sidney</td>
35
<td>Phone</td>
36
<td>2007-2-14 20:39:31</td>
37
<td>2100</td>
38
</tr>
39
<tr>
40
<td>5</td>
41
<td>Sidney</td>
42
<td>TV</td>
43
<td>2007-2-14 20:39:31</td>
44
<td>4500</td>
45
</tr>
46
<tr>
47
<td>6</td>
48
<td>Tom</td>
49
<td>oven</td>
50
<td>2007-2-14 20:39:31</td>
51
<td>300</td>
52
</tr>
53
<tr>
54
<td>7</td>
55
<td>Leo</td>
56
<td>oven</td>
57
<td>2007-2-14 20:39:31</td>
58
<td>240</td>
59
</tr>
60
</table>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
现在我们得到了一个可以在HTML上产生表格的结果了。
谁说这个Table没有必要写的?