Skip to content

Commit 17d0efe

Browse files
committed
filter field
1 parent cb9d2c9 commit 17d0efe

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

src/main/java/com/jeeapp/excel/builder/TableBuilder.java

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ public TableBuilder<T> createRows(Collection<T> beans) {
189189
*/
190190
public TableBuilder<T> createHeader(String... names) {
191191
if (ArrayUtils.isNotEmpty(names)) {
192-
// 只保留指定属性,包含子属性
193-
properties.removeIf(property -> !matchesProperty(Arrays.asList(names), property));
192+
properties.removeIf(property -> !IterableUtils.matchesAny(Arrays.asList(names),
193+
it -> it.equals(property) || property.startsWith(it + ".")));
194194
}
195195
thisRow = parent.sheet.getLastRowNum() + 1;
196196
lastRow = thisRow;
@@ -222,13 +222,6 @@ protected SheetBuilder self() {
222222
return parent;
223223
}
224224

225-
/**
226-
* 判断属性是否需要过滤
227-
*/
228-
private Boolean matchesProperty(List<String> properties, String property) {
229-
return IterableUtils.matchesAny(properties, it -> it.startsWith(property + ".") || property.equals(it));
230-
}
231-
232225
/**
233226
* 创建表头
234227
*/
@@ -237,7 +230,7 @@ private List<Column> resolveHeaders(Column column, Class<?> type) {
237230
for (Field field : getFields(type)) {
238231
Class<?> fieldType = field.getType();
239232
String property = column == null ? field.getName() : column.getName() + "." + field.getName();
240-
if (!matchesProperty(properties, property)) {
233+
if (!IterableUtils.matchesAny(properties, it -> it.equals(property) || it.startsWith(property + "."))) {
241234
continue;
242235
}
243236
Column header = new Column(field);
@@ -260,8 +253,6 @@ private List<Column> resolveHeaders(Column column, Class<?> type) {
260253
}
261254
if (CollectionUtils.isEmpty(header.getChildren())) {
262255
thisCol++;
263-
} else {
264-
properties.remove(property);
265256
}
266257
header.setLastCol(thisCol);
267258
headers.add(header);
@@ -323,38 +314,39 @@ private List<Cell> resolveCells(Object target, String parentPropertyPath, List<S
323314
String propertyName = parentPropertyPath + field.getName();
324315
String property = RegExUtils.removeAll(propertyName, "\\[(.*?)]");
325316
int column = properties.indexOf(property);
326-
if (IterableUtils.matchesAny(properties, str -> str.startsWith(property + ".") || str.equals(property))) {
327-
Object propertyValue = beanWrapper.getPropertyValue(field.getName());
328-
if (propertyType.isArray() && propertyValue != null) {
329-
Object[] values = ObjectUtils.toObjectArray(propertyValue);
330-
int length = values.length;
331-
calculateNestedPropertyPathRowCount(parentPropertyPath, length, nestedPropertyPathRowCount);
332-
for (int i = 0; i < length; i++) {
333-
String propertyPath = propertyName + "[" + i + "].";
334-
nestedPropertyPaths.add(StringUtils.substringBeforeLast(propertyPath, "."));
335-
cells.addAll(resolveCells(values[i], propertyPath, nestedPropertyPaths, nestedPropertyPathRowCount));
336-
}
337-
} else if (Collection.class.isAssignableFrom(propertyType) && propertyValue != null) {
338-
Collection<?> values = (Collection<?>) propertyValue;
339-
int size = values.size();
340-
calculateNestedPropertyPathRowCount(parentPropertyPath, size, nestedPropertyPathRowCount);
341-
for (int i = 0; i < size; i++) {
342-
String propertyPath = propertyName + "[" + i + "].";
343-
nestedPropertyPaths.add(StringUtils.substringBeforeLast(propertyPath, "."));
344-
cells.addAll(resolveCells(values.toArray()[i], propertyPath, nestedPropertyPaths, nestedPropertyPathRowCount));
345-
}
346-
} else if (!BeanUtils.isSimpleProperty(propertyType) && propertyValue != null) {
347-
String propertyPath = propertyName + ".";
317+
if (!IterableUtils.matchesAny(properties, it -> it.equals(property) || it.startsWith(property + "."))) {
318+
continue;
319+
}
320+
Object propertyValue = beanWrapper.getPropertyValue(field.getName());
321+
if (propertyType.isArray() && propertyValue != null) {
322+
Object[] values = ObjectUtils.toObjectArray(propertyValue);
323+
int length = values.length;
324+
calculateNestedPropertyPathRowCount(parentPropertyPath, length, nestedPropertyPathRowCount);
325+
for (int i = 0; i < length; i++) {
326+
String propertyPath = propertyName + "[" + i + "].";
348327
nestedPropertyPaths.add(StringUtils.substringBeforeLast(propertyPath, "."));
349-
cells.addAll(resolveCells(propertyValue, propertyPath, nestedPropertyPaths, nestedPropertyPathRowCount));
350-
} else if (column > -1) {
351-
Cell cell = new Cell(field);
352-
cell.setName(propertyName);
353-
cell.setValue(propertyValue);
354-
cell.setFirstCol(column);
355-
cell.setLastCol(column);
356-
cells.add(cell);
328+
cells.addAll(resolveCells(values[i], propertyPath, nestedPropertyPaths, nestedPropertyPathRowCount));
357329
}
330+
} else if (Collection.class.isAssignableFrom(propertyType) && propertyValue != null) {
331+
Collection<?> values = (Collection<?>) propertyValue;
332+
int size = values.size();
333+
calculateNestedPropertyPathRowCount(parentPropertyPath, size, nestedPropertyPathRowCount);
334+
for (int i = 0; i < size; i++) {
335+
String propertyPath = propertyName + "[" + i + "].";
336+
nestedPropertyPaths.add(StringUtils.substringBeforeLast(propertyPath, "."));
337+
cells.addAll(resolveCells(values.toArray()[i], propertyPath, nestedPropertyPaths, nestedPropertyPathRowCount));
338+
}
339+
} else if (!BeanUtils.isSimpleProperty(propertyType) && propertyValue != null) {
340+
String propertyPath = propertyName + ".";
341+
nestedPropertyPaths.add(StringUtils.substringBeforeLast(propertyPath, "."));
342+
cells.addAll(resolveCells(propertyValue, propertyPath, nestedPropertyPaths, nestedPropertyPathRowCount));
343+
} else if (column > -1) {
344+
Cell cell = new Cell(field);
345+
cell.setName(propertyName);
346+
cell.setValue(propertyValue);
347+
cell.setFirstCol(column);
348+
cell.setLastCol(column);
349+
cells.add(cell);
358350
}
359351
}
360352
return cells;
@@ -399,8 +391,9 @@ private List<String> getProperties(String parentProperty, Class<?> type) {
399391
} else {
400392
properties.addAll(getProperties(property, fieldType));
401393
}
394+
} else {
395+
properties.add(property);
402396
}
403-
properties.add(property);
404397
}
405398
return properties;
406399
}

0 commit comments

Comments
 (0)