HtmlGenerator-HTML表格等生成器

使用方法

1
2
3
4
HTMLGenerator htmlGenerator = new HTMLGenerator(traceHeaderVOs);
htmlGenerator.put("XXX", "表头名"); //XXX属性名
...
htmlGenerator.generateTable();

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* @Description HTML生成器
* @author XX
* @Param
* @return
**/
public class HTMLGenerator extends LinkedHashMap<String, String> {

private static final long serialVersionUID = -2844894776581720941L;

/**
* 生成数据集合
*/
private Collection<?> collection;

public HTMLGenerator(Collection<?> collection) {
super();
this.collection = collection;
}

// private static HTMLGenerator htmlGenerator;
//
// public static synchronized HTMLGenerator getInstance(Collection<?> collection) {
// if(htmlGenerator == null) {
// htmlGenerator = new HTMLGenerator();
// }
// htmlGenerator.setCollection(collection);
// return htmlGenerator;
// }

public Collection<?> getCollection() {
return collection;
}

public void setCollection(Collection<?> collection) {
this.collection = collection;
}

/**
* 默认表格样式
*/
private static final String DEFAULT_TABLE_STYLE = "<style>.pure-table{text-align:center;border-collapse:collapse;border-spacing:0;empty-cells:show;border:1px solid #cbcbcb}.pure-table caption{color:#000;font:italic 85%/1 arial,sans-serif;padding:1em 0;text-align:center}.pure-table td,.pure-table th{border-left:1px solid #cbcbcb;border-width:0 0 0 1px;font-size:inherit;margin:0;overflow:visible;padding:.5em 1em}.pure-table thead{background-color:#e0e0e0;color:#000;text-align:left;vertical-align:bottom}.pure-table td{background-color:transparent}</style>";

/**
* 附加样式
*/
public String style = "";

public String generateTable() {
return generateTable(null, null, null);
}

public String generateTable(String tableStyle) {
return generateTable(tableStyle, null, null);
}

/**
* 生成HTML表格
* @param tableStyle 表格样式 example "<table style=\"text-align:center;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">"
* @param columnsStyles 指定某列样式
* @param cellStyles 指定某个单元格列样式
* @return
*/
public String generateTable(String tableStyle, Map<Integer,String> columnsStyles, Map<Integer,RowColumnPair> cellStyles) {
//TODO columnsStyle
//TODO cellStyles
StringBuilder content = new StringBuilder();
content.append(DEFAULT_TABLE_STYLE);
if(tableStyle == null) {
content.append("<table class=\"pure-table\">");
} else {
content.append(tableStyle);
}
//表头创建
content.append("<tr>");
for(String key:this.keySet()) {
content.append("<td><h4>").append(get(key)).append("</h4>");
}
content.append("</tr>");
//表体创建
int row = 0;
for(Object element : collection) {
content.append("<tr>");
for(String key:this.keySet()) {
content.append("<td>").append(getFieldValueByName(element, key)).append("</td>");
}
content.append("</tr>");
row++;
}
content.append("</table>");
return content.toString();
}

/**
* 通过反射获取参数对应的属性值
* @param o 对象
* @param fieldName 字段名
* @return
*/
private String getFieldValueByName(Object o, String fieldName) {
try {
PropertyDescriptor pd = new PropertyDescriptor(fieldName, o.getClass());
Method getMethod = pd.getReadMethod();
Object obj = getMethod.invoke(o);
return null == obj ? null : obj.toString();
} catch (Exception e) {
return null;
}

}

/**
* 用于定位表格单元格
* @author XX
*
*/
public class RowColumnPair {
/**
* 属性名
*/
private String columnField;

/**
* 样式
*/
private String style;

public RowColumnPair(String columnField, String style) {
super();
this.columnField = columnField;
this.style = style;
}

public String getColumnField() {
return columnField;
}

public void setColumnField(String columnField) {
this.columnField = columnField;
}

public String getStyle() {
return style;
}

public void setStyle(String style) {
this.style = style;
}

}

}