ExcelHeaderAnnoParser.java
1.51 KB
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
package com.taover.bazhuayun.analysis.common.exceldeal;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelHeaderAnnoParser implements ExcelHeaderParser {
@Override
public ExcelHeaderResult parse(List<Object> data, Class headerBeanClass) {
Map<Field, Integer> fieldNameToIndex = new HashMap<Field, Integer>();
List<Field> originDataFieldList = new ArrayList<Field>();
if(data == null || data.isEmpty()) {
return new ExcelHeaderResult();
}
Class currClass = headerBeanClass;
while(currClass != null
&& !currClass.getSimpleName().equals("Object")) {
Field[] fields = currClass.getDeclaredFields();
for(int i=0; i<fields.length; ++i) {
Field item = fields[i];
item.setAccessible(true);
//原始数据
ExcelOriginData dataAnno = item.getDeclaredAnnotation(ExcelOriginData.class);
if(dataAnno != null) {
originDataFieldList.add(item);
continue;
}
//表头列
ExcelHeaderName anno = item.getDeclaredAnnotation(ExcelHeaderName.class);
if(anno == null || "".equals(anno.value().trim())) {
continue;
}
for(int j=0; j<data.size(); ++j) {
Object tempJ = data.get(j);
if(tempJ == null) {
continue;
}
if(anno.value().equals(tempJ.toString())) {
fieldNameToIndex.put(item, j);
}
}
}
currClass = headerBeanClass.getSuperclass();
}
return new ExcelHeaderResult(fieldNameToIndex, originDataFieldList);
}
}