正在显示
1 个修改的文件
包含
34 行增加
和
36 行删除
| @@ -268,22 +268,15 @@ public class ExcelUtil<T> | @@ -268,22 +268,15 @@ public class ExcelUtil<T> | ||
| 268 | } | 268 | } |
| 269 | } | 269 | } |
| 270 | // 有数据时才处理 得到类的所有field. | 270 | // 有数据时才处理 得到类的所有field. |
| 271 | - Field[] allFields = clazz.getDeclaredFields(); | ||
| 272 | - // 定义一个map用于存放列的序号和field. | ||
| 273 | - Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>(); | ||
| 274 | - for (int col = 0; col < allFields.length; col++) | 271 | + List<Object[]> fields = this.getFields(); |
| 272 | + Map<Integer, Object[]> fieldsMap = new HashMap<Integer, Object[]>(); | ||
| 273 | + for (Object[] objects : fields) | ||
| 275 | { | 274 | { |
| 276 | - Field field = allFields[col]; | ||
| 277 | - Excel attr = field.getAnnotation(Excel.class); | ||
| 278 | - if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) | 275 | + Excel attr = (Excel) objects[1]; |
| 276 | + Integer column = cellMap.get(attr.name()); | ||
| 277 | + if (column != null) | ||
| 279 | { | 278 | { |
| 280 | - // 设置类的私有字段属性可访问. | ||
| 281 | - field.setAccessible(true); | ||
| 282 | - Integer column = cellMap.get(attr.name()); | ||
| 283 | - if (column != null) | ||
| 284 | - { | ||
| 285 | - fieldsMap.put(column, field); | ||
| 286 | - } | 279 | + fieldsMap.put(column, objects); |
| 287 | } | 280 | } |
| 288 | } | 281 | } |
| 289 | for (int i = titleNum + 1; i <= rows; i++) | 282 | for (int i = titleNum + 1; i <= rows; i++) |
| @@ -296,14 +289,15 @@ public class ExcelUtil<T> | @@ -296,14 +289,15 @@ public class ExcelUtil<T> | ||
| 296 | continue; | 289 | continue; |
| 297 | } | 290 | } |
| 298 | T entity = null; | 291 | T entity = null; |
| 299 | - for (Map.Entry<Integer, Field> entry : fieldsMap.entrySet()) | 292 | + for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet()) |
| 300 | { | 293 | { |
| 301 | Object val = this.getCellValue(row, entry.getKey()); | 294 | Object val = this.getCellValue(row, entry.getKey()); |
| 302 | 295 | ||
| 303 | // 如果不存在实例则新建. | 296 | // 如果不存在实例则新建. |
| 304 | entity = (entity == null ? clazz.newInstance() : entity); | 297 | entity = (entity == null ? clazz.newInstance() : entity); |
| 305 | // 从map中得到对应列的field. | 298 | // 从map中得到对应列的field. |
| 306 | - Field field = fieldsMap.get(entry.getKey()); | 299 | + Field field = (Field) entry.getValue()[0]; |
| 300 | + Excel attr = (Excel) entry.getValue()[1]; | ||
| 307 | // 取得类型,并根据对象类型设置值. | 301 | // 取得类型,并根据对象类型设置值. |
| 308 | Class<?> fieldType = field.getType(); | 302 | Class<?> fieldType = field.getType(); |
| 309 | if (String.class == fieldType) | 303 | if (String.class == fieldType) |
| @@ -363,7 +357,6 @@ public class ExcelUtil<T> | @@ -363,7 +357,6 @@ public class ExcelUtil<T> | ||
| 363 | } | 357 | } |
| 364 | if (StringUtils.isNotNull(fieldType)) | 358 | if (StringUtils.isNotNull(fieldType)) |
| 365 | { | 359 | { |
| 366 | - Excel attr = field.getAnnotation(Excel.class); | ||
| 367 | String propertyName = field.getName(); | 360 | String propertyName = field.getName(); |
| 368 | if (StringUtils.isNotEmpty(attr.targetAttr())) | 361 | if (StringUtils.isNotEmpty(attr.targetAttr())) |
| 369 | { | 362 | { |
| @@ -610,8 +603,6 @@ public class ExcelUtil<T> | @@ -610,8 +603,6 @@ public class ExcelUtil<T> | ||
| 610 | { | 603 | { |
| 611 | Field field = (Field) os[0]; | 604 | Field field = (Field) os[0]; |
| 612 | Excel excel = (Excel) os[1]; | 605 | Excel excel = (Excel) os[1]; |
| 613 | - // 设置实体类私有属性可访问 | ||
| 614 | - field.setAccessible(true); | ||
| 615 | this.addCell(excel, row, vo, field, column++); | 606 | this.addCell(excel, row, vo, field, column++); |
| 616 | } | 607 | } |
| 617 | } | 608 | } |
| @@ -1164,7 +1155,17 @@ public class ExcelUtil<T> | @@ -1164,7 +1155,17 @@ public class ExcelUtil<T> | ||
| 1164 | */ | 1155 | */ |
| 1165 | private void createExcelField() | 1156 | private void createExcelField() |
| 1166 | { | 1157 | { |
| 1167 | - this.fields = new ArrayList<Object[]>(); | 1158 | + this.fields = getFields(); |
| 1159 | + this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList()); | ||
| 1160 | + this.maxHeight = getRowHeight(); | ||
| 1161 | + } | ||
| 1162 | + | ||
| 1163 | + /** | ||
| 1164 | + * 获取字段注解信息 | ||
| 1165 | + */ | ||
| 1166 | + public List<Object[]> getFields() | ||
| 1167 | + { | ||
| 1168 | + List<Object[]> fields = new ArrayList<Object[]>(); | ||
| 1168 | List<Field> tempFields = new ArrayList<>(); | 1169 | List<Field> tempFields = new ArrayList<>(); |
| 1169 | tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields())); | 1170 | tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields())); |
| 1170 | tempFields.addAll(Arrays.asList(clazz.getDeclaredFields())); | 1171 | tempFields.addAll(Arrays.asList(clazz.getDeclaredFields())); |
| @@ -1173,7 +1174,12 @@ public class ExcelUtil<T> | @@ -1173,7 +1174,12 @@ public class ExcelUtil<T> | ||
| 1173 | // 单注解 | 1174 | // 单注解 |
| 1174 | if (field.isAnnotationPresent(Excel.class)) | 1175 | if (field.isAnnotationPresent(Excel.class)) |
| 1175 | { | 1176 | { |
| 1176 | - putToField(field, field.getAnnotation(Excel.class)); | 1177 | + Excel attr = field.getAnnotation(Excel.class); |
| 1178 | + if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) | ||
| 1179 | + { | ||
| 1180 | + field.setAccessible(true); | ||
| 1181 | + fields.add(new Object[] { field, attr }); | ||
| 1182 | + } | ||
| 1177 | } | 1183 | } |
| 1178 | 1184 | ||
| 1179 | // 多注解 | 1185 | // 多注解 |
| @@ -1181,14 +1187,17 @@ public class ExcelUtil<T> | @@ -1181,14 +1187,17 @@ public class ExcelUtil<T> | ||
| 1181 | { | 1187 | { |
| 1182 | Excels attrs = field.getAnnotation(Excels.class); | 1188 | Excels attrs = field.getAnnotation(Excels.class); |
| 1183 | Excel[] excels = attrs.value(); | 1189 | Excel[] excels = attrs.value(); |
| 1184 | - for (Excel excel : excels) | 1190 | + for (Excel attr : excels) |
| 1185 | { | 1191 | { |
| 1186 | - putToField(field, excel); | 1192 | + if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) |
| 1193 | + { | ||
| 1194 | + field.setAccessible(true); | ||
| 1195 | + fields.add(new Object[] { field, attr }); | ||
| 1196 | + } | ||
| 1187 | } | 1197 | } |
| 1188 | } | 1198 | } |
| 1189 | } | 1199 | } |
| 1190 | - this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList()); | ||
| 1191 | - this.maxHeight = getRowHeight(); | 1200 | + return fields; |
| 1192 | } | 1201 | } |
| 1193 | 1202 | ||
| 1194 | /** | 1203 | /** |
| @@ -1206,17 +1215,6 @@ public class ExcelUtil<T> | @@ -1206,17 +1215,6 @@ public class ExcelUtil<T> | ||
| 1206 | } | 1215 | } |
| 1207 | 1216 | ||
| 1208 | /** | 1217 | /** |
| 1209 | - * 放到字段集合中 | ||
| 1210 | - */ | ||
| 1211 | - private void putToField(Field field, Excel attr) | ||
| 1212 | - { | ||
| 1213 | - if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) | ||
| 1214 | - { | ||
| 1215 | - this.fields.add(new Object[] { field, attr }); | ||
| 1216 | - } | ||
| 1217 | - } | ||
| 1218 | - | ||
| 1219 | - /** | ||
| 1220 | * 创建一个工作簿 | 1218 | * 创建一个工作簿 |
| 1221 | */ | 1219 | */ |
| 1222 | public void createWorkbook() | 1220 | public void createWorkbook() |
-
请 注册 或 登录 后发表评论