Search in sources :

Example 1 with ExcelColumn

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;
}
Also used : ExcelColumn(com.github.liaochong.myexcel.core.annotation.ExcelColumn)

Example 2 with ExcelColumn

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;
}
Also used : Table(com.github.liaochong.myexcel.core.parser.Table) ReflectUtil(com.github.liaochong.myexcel.utils.ReflectUtil) IntStream(java.util.stream.IntStream) NumberDropDownList(com.github.liaochong.myexcel.core.constant.NumberDropDownList) Arrays(java.util.Arrays) Date(java.util.Date) Pair(com.github.liaochong.myexcel.core.container.Pair) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExcludeColumn(com.github.liaochong.myexcel.core.annotation.ExcludeColumn) BooleanDropDownList(com.github.liaochong.myexcel.core.constant.BooleanDropDownList) ImageFile(com.github.liaochong.myexcel.core.constant.ImageFile) WriteConverterContext(com.github.liaochong.myexcel.core.converter.WriteConverterContext) Map(java.util.Map) Constants(com.github.liaochong.myexcel.core.constant.Constants) WidthStrategy(com.github.liaochong.myexcel.core.strategy.WidthStrategy) LinkedList(java.util.LinkedList) MultiColumn(com.github.liaochong.myexcel.core.annotation.MultiColumn) IgnoreColumn(com.github.liaochong.myexcel.core.annotation.IgnoreColumn) NullType(javax.lang.model.type.NullType) LinkUrl(com.github.liaochong.myexcel.core.constant.LinkUrl) Tr(com.github.liaochong.myexcel.core.parser.Tr) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) List(java.util.List) Td(com.github.liaochong.myexcel.core.parser.Td) StringUtil(com.github.liaochong.myexcel.utils.StringUtil) Modifier(java.lang.reflect.Modifier) LocalDate(java.time.LocalDate) ConvertContext(com.github.liaochong.myexcel.core.converter.ConvertContext) ExcelColumn(com.github.liaochong.myexcel.core.annotation.ExcelColumn) StyleParser(com.github.liaochong.myexcel.core.parser.StyleParser) Comparator(java.util.Comparator) LinkEmail(com.github.liaochong.myexcel.core.constant.LinkEmail) Collections(java.util.Collections) ConfigurationUtil(com.github.liaochong.myexcel.utils.ConfigurationUtil) DropDownList(com.github.liaochong.myexcel.core.constant.DropDownList) ContentTypeEnum(com.github.liaochong.myexcel.core.parser.ContentTypeEnum) ClassFieldContainer(com.github.liaochong.myexcel.core.reflect.ClassFieldContainer) TdUtil(com.github.liaochong.myexcel.utils.TdUtil) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ExcelColumn(com.github.liaochong.myexcel.core.annotation.ExcelColumn)

Example 3 with ExcelColumn

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);
    });
}
Also used : ClassFieldContainer(com.github.liaochong.myexcel.core.reflect.ClassFieldContainer) Field(java.lang.reflect.Field) ExcelColumn(com.github.liaochong.myexcel.core.annotation.ExcelColumn)

Example 4 with ExcelColumn

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;
}
Also used : ClassFieldContainer(com.github.liaochong.myexcel.core.reflect.ClassFieldContainer) Field(java.lang.reflect.Field) ExcelColumn(com.github.liaochong.myexcel.core.annotation.ExcelColumn)

Example 5 with ExcelColumn

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);
}
Also used : ExcelColumn(com.github.liaochong.myexcel.core.annotation.ExcelColumn)

Aggregations

ExcelColumn (com.github.liaochong.myexcel.core.annotation.ExcelColumn)6 ClassFieldContainer (com.github.liaochong.myexcel.core.reflect.ClassFieldContainer)4 Field (java.lang.reflect.Field)4 ExcludeColumn (com.github.liaochong.myexcel.core.annotation.ExcludeColumn)1 IgnoreColumn (com.github.liaochong.myexcel.core.annotation.IgnoreColumn)1 MultiColumn (com.github.liaochong.myexcel.core.annotation.MultiColumn)1 BooleanDropDownList (com.github.liaochong.myexcel.core.constant.BooleanDropDownList)1 Constants (com.github.liaochong.myexcel.core.constant.Constants)1 DropDownList (com.github.liaochong.myexcel.core.constant.DropDownList)1 ImageFile (com.github.liaochong.myexcel.core.constant.ImageFile)1 LinkEmail (com.github.liaochong.myexcel.core.constant.LinkEmail)1 LinkUrl (com.github.liaochong.myexcel.core.constant.LinkUrl)1 NumberDropDownList (com.github.liaochong.myexcel.core.constant.NumberDropDownList)1 Pair (com.github.liaochong.myexcel.core.container.Pair)1 ConvertContext (com.github.liaochong.myexcel.core.converter.ConvertContext)1 WriteConverterContext (com.github.liaochong.myexcel.core.converter.WriteConverterContext)1 ContentTypeEnum (com.github.liaochong.myexcel.core.parser.ContentTypeEnum)1 StyleParser (com.github.liaochong.myexcel.core.parser.StyleParser)1 Table (com.github.liaochong.myexcel.core.parser.Table)1 Td (com.github.liaochong.myexcel.core.parser.Td)1