use of com.github.liaochong.myexcel.core.annotation.ExcelColumn in project myexcel by liaochong.
the class ReflectUtil method sortFields.
public static int sortFields(Field field1, Field field2) {
ExcelColumn excelColumn1 = field1.getAnnotation(ExcelColumn.class);
ExcelColumn excelColumn2 = field2.getAnnotation(ExcelColumn.class);
if (excelColumn1 == null && excelColumn2 == null) {
return 0;
}
int defaultOrder = 0;
int order1 = defaultOrder;
if (excelColumn1 != null) {
order1 = excelColumn1.order();
}
int order2 = defaultOrder;
if (excelColumn2 != null) {
order2 = excelColumn2.order();
}
if (order1 == order2) {
return 0;
}
return order1 > order2 ? 1 : -1;
}
use of com.github.liaochong.myexcel.core.annotation.ExcelColumn in project myexcel by liaochong.
the class AbstractSimpleExcelBuilder method getFilteredFields.
/**
* Core methods for obtaining export related fields, styles, etc
*
* @param classFieldContainer classFieldContainer
* @param groups 分组
* @return Field
*/
protected List<Field> getFilteredFields(ClassFieldContainer classFieldContainer, Class<?>... groups) {
ConfigurationUtil.parseConfiguration(classFieldContainer, configuration);
this.parseGlobalStyle();
List<Field> preElectionFields = this.getPreElectionFields(classFieldContainer);
List<Field> buildFields = this.getGroupFields(preElectionFields, groups);
// 初始化标题容器
List<String> titles = new ArrayList<>(buildFields.size());
for (int i = 0, size = buildFields.size(); i < size; i++) {
Field field = buildFields.get(i);
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
String[] columnStyles = null;
if (excelColumn != null) {
if (configuration.useFieldNameAsTitle && excelColumn.title().isEmpty()) {
titles.add(field.getName());
} else {
titles.add(excelColumn.title());
}
if (!excelColumn.defaultValue().isEmpty()) {
defaultValueMap.put(field, excelColumn.defaultValue());
}
if (excelColumn.width() > -1) {
customWidthMap.putIfAbsent(i, excelColumn.width());
}
if (excelColumn.style().length > 0) {
columnStyles = excelColumn.style();
}
if (!excelColumn.format().isEmpty()) {
formats.put(i, excelColumn.format());
} else if (!excelColumn.decimalFormat().isEmpty()) {
formats.put(i, excelColumn.decimalFormat());
} else if (!excelColumn.dateFormatPattern().isEmpty()) {
formats.put(i, excelColumn.dateFormatPattern());
}
ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn);
excelColumnMappingMap.put(field, mapping);
} else {
if (configuration.useFieldNameAsTitle) {
titles.add(field.getName());
} else {
titles.add(null);
}
}
styleParser.setColumnStyle(field, i, columnStyles);
setGlobalFormat(i, field);
}
setTitles(titles);
hasMultiColumn = buildFields.stream().anyMatch(field -> field.isAnnotationPresent(MultiColumn.class));
return buildFields;
}
use of com.github.liaochong.myexcel.core.annotation.ExcelColumn in project myexcel by liaochong.
the class AbstractReadHandler method setConfiguration.
private void setConfiguration(Class<T> dataType, boolean isMapType) {
if (isMapType) {
return;
}
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
ConfigurationUtil.parseConfiguration(classFieldContainer, convertContext.configuration);
List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
fields.forEach(field -> {
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
if (excelColumn == null) {
return;
}
ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn);
convertContext.excelColumnMappingMap.put(field, mapping);
});
}
use of com.github.liaochong.myexcel.core.annotation.ExcelColumn in project myexcel by liaochong.
the class ReflectUtil method getFieldMapOfTitleExcelColumn.
public static Map<String, Field> getFieldMapOfTitleExcelColumn(Class<?> dataType) {
if (dataType == Map.class) {
return Collections.emptyMap();
}
Map<String, Field> fieldMap = TITLE_FIELD_CACHE.get(dataType);
if (fieldMap != null) {
return fieldMap;
}
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
if (fields.isEmpty()) {
throw new IllegalStateException("There is no field with @ExcelColumn");
}
fieldMap = new HashMap<>(fields.size());
for (Field field : fields) {
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
String title = excelColumn.title();
if (title.isEmpty()) {
continue;
}
Field f = fieldMap.get(title);
if (f != null) {
throw new IllegalStateException("Title cannot be repeated: " + title + ". Please check it.");
}
field.setAccessible(true);
fieldMap.put(title, field);
}
TITLE_FIELD_CACHE.cache(dataType, fieldMap);
return fieldMap;
}
use of com.github.liaochong.myexcel.core.annotation.ExcelColumn in project myexcel by liaochong.
the class ReflectUtil method isFieldSelected.
public static boolean isFieldSelected(List<Class<?>> selectedGroupList, Field field) {
if (selectedGroupList.isEmpty()) {
return true;
}
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
if (excelColumn == null) {
return false;
}
Class<?>[] groupArr = excelColumn.groups();
if (groupArr.length == 0) {
return false;
}
List<Class<?>> reservedGroupList = Arrays.stream(groupArr).collect(Collectors.toList());
return reservedGroupList.stream().anyMatch(selectedGroupList::contains);
}
Aggregations