|
|
@@ -237,22 +237,15 @@ public class ExcelUtil<T>
|
|
237
|
237
|
}
|
|
238
|
238
|
}
|
|
239
|
239
|
// 有数据时才处理 得到类的所有field.
|
|
240
|
|
- Field[] allFields = clazz.getDeclaredFields();
|
|
241
|
|
- // 定义一个map用于存放列的序号和field.
|
|
242
|
|
- Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();
|
|
243
|
|
- for (int col = 0; col < allFields.length; col++)
|
|
|
240
|
+ List<Object[]> fields = this.getFields();
|
|
|
241
|
+ Map<Integer, Object[]> fieldsMap = new HashMap<Integer, Object[]>();
|
|
|
242
|
+ for (Object[] objects : fields)
|
|
244
|
243
|
{
|
|
245
|
|
- Field field = allFields[col];
|
|
246
|
|
- Excel attr = field.getAnnotation(Excel.class);
|
|
247
|
|
- if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
|
244
|
+ Excel attr = (Excel) objects[1];
|
|
|
245
|
+ Integer column = cellMap.get(attr.name());
|
|
|
246
|
+ if (column != null)
|
|
248
|
247
|
{
|
|
249
|
|
- // 设置类的私有字段属性可访问.
|
|
250
|
|
- field.setAccessible(true);
|
|
251
|
|
- Integer column = cellMap.get(attr.name());
|
|
252
|
|
- if (column != null)
|
|
253
|
|
- {
|
|
254
|
|
- fieldsMap.put(column, field);
|
|
255
|
|
- }
|
|
|
248
|
+ fieldsMap.put(column, objects);
|
|
256
|
249
|
}
|
|
257
|
250
|
}
|
|
258
|
251
|
for (int i = titleNum + 1; i <= rows; i++)
|
|
|
@@ -265,14 +258,15 @@ public class ExcelUtil<T>
|
|
265
|
258
|
continue;
|
|
266
|
259
|
}
|
|
267
|
260
|
T entity = null;
|
|
268
|
|
- for (Map.Entry<Integer, Field> entry : fieldsMap.entrySet())
|
|
|
261
|
+ for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet())
|
|
269
|
262
|
{
|
|
270
|
263
|
Object val = this.getCellValue(row, entry.getKey());
|
|
271
|
264
|
|
|
272
|
265
|
// 如果不存在实例则新建.
|
|
273
|
266
|
entity = (entity == null ? clazz.newInstance() : entity);
|
|
274
|
267
|
// 从map中得到对应列的field.
|
|
275
|
|
- Field field = fieldsMap.get(entry.getKey());
|
|
|
268
|
+ Field field = (Field) entry.getValue()[0];
|
|
|
269
|
+ Excel attr = (Excel) entry.getValue()[1];
|
|
276
|
270
|
// 取得类型,并根据对象类型设置值.
|
|
277
|
271
|
Class<?> fieldType = field.getType();
|
|
278
|
272
|
if (String.class == fieldType)
|
|
|
@@ -332,7 +326,6 @@ public class ExcelUtil<T>
|
|
332
|
326
|
}
|
|
333
|
327
|
if (StringUtils.isNotNull(fieldType))
|
|
334
|
328
|
{
|
|
335
|
|
- Excel attr = field.getAnnotation(Excel.class);
|
|
336
|
329
|
String propertyName = field.getName();
|
|
337
|
330
|
if (StringUtils.isNotEmpty(attr.targetAttr()))
|
|
338
|
331
|
{
|
|
|
@@ -490,8 +483,6 @@ public class ExcelUtil<T>
|
|
490
|
483
|
{
|
|
491
|
484
|
Field field = (Field) os[0];
|
|
492
|
485
|
Excel excel = (Excel) os[1];
|
|
493
|
|
- // 设置实体类私有属性可访问
|
|
494
|
|
- field.setAccessible(true);
|
|
495
|
486
|
this.addCell(excel, row, vo, field, column++);
|
|
496
|
487
|
}
|
|
497
|
488
|
}
|
|
|
@@ -988,7 +979,17 @@ public class ExcelUtil<T>
|
|
988
|
979
|
*/
|
|
989
|
980
|
private void createExcelField()
|
|
990
|
981
|
{
|
|
991
|
|
- this.fields = new ArrayList<Object[]>();
|
|
|
982
|
+ this.fields = getFields();
|
|
|
983
|
+ this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
|
|
984
|
+ this.maxHeight = getRowHeight();
|
|
|
985
|
+ }
|
|
|
986
|
+
|
|
|
987
|
+ /**
|
|
|
988
|
+ * 获取字段注解信息
|
|
|
989
|
+ */
|
|
|
990
|
+ public List<Object[]> getFields()
|
|
|
991
|
+ {
|
|
|
992
|
+ List<Object[]> fields = new ArrayList<Object[]>();
|
|
992
|
993
|
List<Field> tempFields = new ArrayList<>();
|
|
993
|
994
|
tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
|
|
994
|
995
|
tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
|
|
@@ -997,7 +998,12 @@ public class ExcelUtil<T>
|
|
997
|
998
|
// 单注解
|
|
998
|
999
|
if (field.isAnnotationPresent(Excel.class))
|
|
999
|
1000
|
{
|
|
1000
|
|
- putToField(field, field.getAnnotation(Excel.class));
|
|
|
1001
|
+ Excel attr = field.getAnnotation(Excel.class);
|
|
|
1002
|
+ if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
|
1003
|
+ {
|
|
|
1004
|
+ field.setAccessible(true);
|
|
|
1005
|
+ fields.add(new Object[] { field, attr });
|
|
|
1006
|
+ }
|
|
1001
|
1007
|
}
|
|
1002
|
1008
|
|
|
1003
|
1009
|
// 多注解
|
|
|
@@ -1005,14 +1011,17 @@ public class ExcelUtil<T>
|
|
1005
|
1011
|
{
|
|
1006
|
1012
|
Excels attrs = field.getAnnotation(Excels.class);
|
|
1007
|
1013
|
Excel[] excels = attrs.value();
|
|
1008
|
|
- for (Excel excel : excels)
|
|
|
1014
|
+ for (Excel attr : excels)
|
|
1009
|
1015
|
{
|
|
1010
|
|
- putToField(field, excel);
|
|
|
1016
|
+ if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
|
1017
|
+ {
|
|
|
1018
|
+ field.setAccessible(true);
|
|
|
1019
|
+ fields.add(new Object[] { field, attr });
|
|
|
1020
|
+ }
|
|
1011
|
1021
|
}
|
|
1012
|
1022
|
}
|
|
1013
|
1023
|
}
|
|
1014
|
|
- this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
|
1015
|
|
- this.maxHeight = getRowHeight();
|
|
|
1024
|
+ return fields;
|
|
1016
|
1025
|
}
|
|
1017
|
1026
|
|
|
1018
|
1027
|
/**
|
|
|
@@ -1030,17 +1039,6 @@ public class ExcelUtil<T>
|
|
1030
|
1039
|
}
|
|
1031
|
1040
|
|
|
1032
|
1041
|
/**
|
|
1033
|
|
- * 放到字段集合中
|
|
1034
|
|
- */
|
|
1035
|
|
- private void putToField(Field field, Excel attr)
|
|
1036
|
|
- {
|
|
1037
|
|
- if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
1038
|
|
- {
|
|
1039
|
|
- this.fields.add(new Object[] { field, attr });
|
|
1040
|
|
- }
|
|
1041
|
|
- }
|
|
1042
|
|
-
|
|
1043
|
|
- /**
|
|
1044
|
1042
|
* 创建一个工作簿
|
|
1045
|
1043
|
*/
|
|
1046
|
1044
|
public void createWorkbook()
|