|
|
@@ -168,6 +168,11 @@ public class ExcelUtil<T>
|
|
168
|
168
|
public Class<T> clazz;
|
|
169
|
169
|
|
|
170
|
170
|
/**
|
|
|
171
|
+ * 需要显示列属性
|
|
|
172
|
+ */
|
|
|
173
|
+ public String[] includeFields;
|
|
|
174
|
+
|
|
|
175
|
+ /**
|
|
171
|
176
|
* 需要排除列属性
|
|
172
|
177
|
*/
|
|
173
|
178
|
public String[] excludeFields;
|
|
|
@@ -178,10 +183,19 @@ public class ExcelUtil<T>
|
|
178
|
183
|
}
|
|
179
|
184
|
|
|
180
|
185
|
/**
|
|
|
186
|
+ * 仅在Excel中显示列属性
|
|
|
187
|
+ *
|
|
|
188
|
+ * @param fields 列属性名 示例[单个"name"/多个"id","name"]
|
|
|
189
|
+ */
|
|
|
190
|
+ public void showColumn(String... fields)
|
|
|
191
|
+ {
|
|
|
192
|
+ this.includeFields = fields;
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ /**
|
|
181
|
196
|
* 隐藏Excel中列属性
|
|
182
|
197
|
*
|
|
183
|
198
|
* @param fields 列属性名 示例[单个"name"/多个"id","name"]
|
|
184
|
|
- * @throws Exception
|
|
185
|
199
|
*/
|
|
186
|
200
|
public void hideColumn(String... fields)
|
|
187
|
201
|
{
|
|
|
@@ -1280,46 +1294,86 @@ public class ExcelUtil<T>
|
|
1280
|
1294
|
List<Field> tempFields = new ArrayList<>();
|
|
1281
|
1295
|
tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
|
|
1282
|
1296
|
tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
|
1283
|
|
- for (Field field : tempFields)
|
|
|
1297
|
+ if (StringUtils.isNotEmpty(includeFields))
|
|
1284
|
1298
|
{
|
|
1285
|
|
- if (!ArrayUtils.contains(this.excludeFields, field.getName()))
|
|
|
1299
|
+ for (Field field : tempFields)
|
|
1286
|
1300
|
{
|
|
1287
|
|
- // 单注解
|
|
1288
|
|
- if (field.isAnnotationPresent(Excel.class))
|
|
|
1301
|
+ if (ArrayUtils.contains(this.includeFields, field.getName()) || field.isAnnotationPresent(Excels.class))
|
|
1289
|
1302
|
{
|
|
1290
|
|
- Excel attr = field.getAnnotation(Excel.class);
|
|
1291
|
|
- if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
|
1303
|
+ addField(fields, field);
|
|
|
1304
|
+ }
|
|
|
1305
|
+ }
|
|
|
1306
|
+ }
|
|
|
1307
|
+ else if (StringUtils.isNotEmpty(excludeFields))
|
|
|
1308
|
+ {
|
|
|
1309
|
+ for (Field field : tempFields)
|
|
|
1310
|
+ {
|
|
|
1311
|
+ if (!ArrayUtils.contains(this.excludeFields, field.getName()))
|
|
|
1312
|
+ {
|
|
|
1313
|
+ addField(fields, field);
|
|
|
1314
|
+ }
|
|
|
1315
|
+ }
|
|
|
1316
|
+ }
|
|
|
1317
|
+ else
|
|
|
1318
|
+ {
|
|
|
1319
|
+ for (Field field : tempFields)
|
|
|
1320
|
+ {
|
|
|
1321
|
+ addField(fields, field);
|
|
|
1322
|
+ }
|
|
|
1323
|
+ }
|
|
|
1324
|
+ return fields;
|
|
|
1325
|
+ }
|
|
|
1326
|
+
|
|
|
1327
|
+ /**
|
|
|
1328
|
+ * 添加字段信息
|
|
|
1329
|
+ */
|
|
|
1330
|
+ public void addField(List<Object[]> fields, Field field)
|
|
|
1331
|
+ {
|
|
|
1332
|
+ // 单注解
|
|
|
1333
|
+ if (field.isAnnotationPresent(Excel.class))
|
|
|
1334
|
+ {
|
|
|
1335
|
+ Excel attr = field.getAnnotation(Excel.class);
|
|
|
1336
|
+ if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
|
1337
|
+ {
|
|
|
1338
|
+ field.setAccessible(true);
|
|
|
1339
|
+ fields.add(new Object[] { field, attr });
|
|
|
1340
|
+ }
|
|
|
1341
|
+ if (Collection.class.isAssignableFrom(field.getType()))
|
|
|
1342
|
+ {
|
|
|
1343
|
+ subMethod = getSubMethod(field.getName(), clazz);
|
|
|
1344
|
+ ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
|
|
1345
|
+ Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
|
|
|
1346
|
+ this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
|
|
|
1347
|
+ }
|
|
|
1348
|
+ }
|
|
|
1349
|
+
|
|
|
1350
|
+ // 多注解
|
|
|
1351
|
+ if (field.isAnnotationPresent(Excels.class))
|
|
|
1352
|
+ {
|
|
|
1353
|
+ Excels attrs = field.getAnnotation(Excels.class);
|
|
|
1354
|
+ Excel[] excels = attrs.value();
|
|
|
1355
|
+ for (Excel attr : excels)
|
|
|
1356
|
+ {
|
|
|
1357
|
+ if (StringUtils.isNotEmpty(includeFields))
|
|
|
1358
|
+ {
|
|
|
1359
|
+ if (ArrayUtils.contains(this.includeFields, field.getName() + "." + attr.targetAttr())
|
|
|
1360
|
+ && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
|
|
1292
|
1361
|
{
|
|
1293
|
1362
|
field.setAccessible(true);
|
|
1294
|
1363
|
fields.add(new Object[] { field, attr });
|
|
1295
|
1364
|
}
|
|
1296
|
|
- if (Collection.class.isAssignableFrom(field.getType()))
|
|
1297
|
|
- {
|
|
1298
|
|
- subMethod = getSubMethod(field.getName(), clazz);
|
|
1299
|
|
- ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
|
1300
|
|
- Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
|
|
1301
|
|
- this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
|
|
1302
|
|
- }
|
|
1303
|
1365
|
}
|
|
1304
|
|
-
|
|
1305
|
|
- // 多注解
|
|
1306
|
|
- if (field.isAnnotationPresent(Excels.class))
|
|
|
1366
|
+ else
|
|
1307
|
1367
|
{
|
|
1308
|
|
- Excels attrs = field.getAnnotation(Excels.class);
|
|
1309
|
|
- Excel[] excels = attrs.value();
|
|
1310
|
|
- for (Excel attr : excels)
|
|
|
1368
|
+ if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
|
|
|
1369
|
+ && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
|
|
1311
|
1370
|
{
|
|
1312
|
|
- if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
|
|
1313
|
|
- && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
|
|
1314
|
|
- {
|
|
1315
|
|
- field.setAccessible(true);
|
|
1316
|
|
- fields.add(new Object[] { field, attr });
|
|
1317
|
|
- }
|
|
|
1371
|
+ field.setAccessible(true);
|
|
|
1372
|
+ fields.add(new Object[] { field, attr });
|
|
1318
|
1373
|
}
|
|
1319
|
1374
|
}
|
|
1320
|
1375
|
}
|
|
1321
|
1376
|
}
|
|
1322
|
|
- return fields;
|
|
1323
|
1377
|
}
|
|
1324
|
1378
|
|
|
1325
|
1379
|
/**
|