Search in sources :

Example 1 with ExcelProperty

use of com.alibaba.excel.annotation.ExcelProperty in project metersphere by metersphere.

the class EasyExcelListener method getFieldNameSet.

/**
 * @description: 获取注解里ExcelProperty的value
 */
public Set<String> getFieldNameSet(Class clazz) throws NoSuchFieldException {
    Set<String> result = new HashSet<>();
    Field field;
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        field = clazz.getDeclaredField(fields[i].getName());
        field.setAccessible(true);
        ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
        if (excelProperty != null) {
            StringBuilder value = new StringBuilder();
            for (String v : excelProperty.value()) {
                value.append(v);
            }
            // 检查是否必有的头部信息
            if (field.getAnnotation(NotRequired.class) == null) {
                result.add(value.toString());
            }
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) NotRequired(io.metersphere.excel.annotation.NotRequired)

Example 2 with ExcelProperty

use of com.alibaba.excel.annotation.ExcelProperty in project metersphere by metersphere.

the class TestCaseNoModelDataListener method genExcelHeadToFieldNameDicAndGetNotRequiredFields.

/**
 * @description: 获取注解里ExcelProperty的value
 */
public Set<String> genExcelHeadToFieldNameDicAndGetNotRequiredFields() throws NoSuchFieldException {
    Set<String> result = new HashSet<>();
    Field field;
    Field[] fields = excelDataClass.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        field = excelDataClass.getDeclaredField(fields[i].getName());
        field.setAccessible(true);
        ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
        if (excelProperty != null) {
            StringBuilder value = new StringBuilder();
            for (String v : excelProperty.value()) {
                value.append(v);
            }
            excelHeadToFieldNameDic.put(value.toString(), field.getName());
            // 检查是否必有的头部信息
            if (field.getAnnotation(NotRequired.class) != null) {
                result.add(value.toString());
            }
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) NotRequired(io.metersphere.excel.annotation.NotRequired)

Example 3 with ExcelProperty

use of com.alibaba.excel.annotation.ExcelProperty in project metersphere by metersphere.

the class ExcelValidateHelper method validateEntity.

public static <T> String validateEntity(T obj) throws NoSuchFieldException {
    StringBuilder result = new StringBuilder();
    Set<ConstraintViolation<T>> set = excelValidateHelper.validator.validate(obj, Default.class);
    if (set != null && !set.isEmpty()) {
        for (ConstraintViolation<T> cv : set) {
            Field declaredField = obj.getClass().getDeclaredField(cv.getPropertyPath().toString());
            ExcelProperty annotation = declaredField.getAnnotation(ExcelProperty.class);
            // 拼接错误信息,包含当前出错数据的标题名字+错误信息
            result.append(annotation.value()[0] + cv.getMessage()).append("; ");
        }
    }
    return result.toString();
}
Also used : Field(java.lang.reflect.Field) ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) ConstraintViolation(javax.validation.ConstraintViolation)

Example 4 with ExcelProperty

use of com.alibaba.excel.annotation.ExcelProperty in project mall-learning by macrozheng.

the class CustomMergeStrategy method lazyInit.

/**
 * 初始化主键下标和需要合并字段的下标
 */
private void lazyInit(WriteSheetHolder writeSheetHolder) {
    // 获取当前sheet
    Sheet sheet = writeSheetHolder.getSheet();
    // 获取标题行
    Row titleRow = sheet.getRow(0);
    // 获取DTO的类型
    Class<?> eleType = this.elementType;
    // 获取DTO所有的属性
    Field[] fields = eleType.getDeclaredFields();
    // 遍历所有的字段,因为是基于DTO的字段来构建excel,所以字段数 >= excel的列数
    for (Field theField : fields) {
        // 获取@ExcelProperty注解,用于获取该字段对应在excel中的列的下标
        ExcelProperty easyExcelAnno = theField.getAnnotation(ExcelProperty.class);
        // 为空,则表示该字段不需要导入到excel,直接处理下一个字段
        if (null == easyExcelAnno) {
            continue;
        }
        // 获取自定义的注解,用于合并单元格
        CustomMerge customMerge = theField.getAnnotation(CustomMerge.class);
        // 没有@CustomMerge注解的默认不合并
        if (null == customMerge) {
            continue;
        }
        for (int index = 0; index < fields.length; index++) {
            Cell theCell = titleRow.getCell(index);
            // 当配置为不需要导出时,返回的为null,这里作一下判断,防止NPE
            if (null == theCell) {
                continue;
            }
            // 将字段和excel的表头匹配上
            if (easyExcelAnno.value()[0].equalsIgnoreCase(theCell.getStringCellValue())) {
                if (customMerge.isPk()) {
                    pkIndex = index;
                }
                if (customMerge.needMerge()) {
                    needMergeColumnIndex.add(index);
                }
            }
        }
    }
    // 没有指定主键,则异常
    if (null == this.pkIndex) {
        throw new IllegalStateException("使用@CustomMerge注解必须指定主键");
    }
}
Also used : Field(java.lang.reflect.Field) ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) CustomMerge(com.macro.mall.tiny.anno.CustomMerge) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 5 with ExcelProperty

use of com.alibaba.excel.annotation.ExcelProperty in project easyexcel by alibaba.

the class ClassUtils method declaredFieldContentMap.

private static Map<String, ExcelContentProperty> declaredFieldContentMap(Class<?> clazz) {
    if (clazz == null) {
        return null;
    }
    return CLASS_CONTENT_CACHE.computeIfAbsent(clazz, key -> {
        List<Field> tempFieldList = new ArrayList<>();
        Class<?> tempClass = clazz;
        while (tempClass != null) {
            Collections.addAll(tempFieldList, tempClass.getDeclaredFields());
            // Get the parent class and give it to yourself
            tempClass = tempClass.getSuperclass();
        }
        ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class);
        ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class);
        Map<String, ExcelContentProperty> fieldContentMap = MapUtils.newHashMapWithExpectedSize(tempFieldList.size());
        for (Field field : tempFieldList) {
            ExcelContentProperty excelContentProperty = new ExcelContentProperty();
            excelContentProperty.setField(field);
            ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
            if (excelProperty != null) {
                Class<? extends Converter<?>> convertClazz = excelProperty.converter();
                if (convertClazz != AutoConverter.class) {
                    try {
                        Converter<?> converter = convertClazz.getDeclaredConstructor().newInstance();
                        excelContentProperty.setConverter(converter);
                    } catch (Exception e) {
                        throw new ExcelCommonException("Can not instance custom converter:" + convertClazz.getName());
                    }
                }
            }
            ContentStyle contentStyle = field.getAnnotation(ContentStyle.class);
            if (contentStyle == null) {
                contentStyle = parentContentStyle;
            }
            excelContentProperty.setContentStyleProperty(StyleProperty.build(contentStyle));
            ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class);
            if (contentFontStyle == null) {
                contentFontStyle = parentContentFontStyle;
            }
            excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle));
            excelContentProperty.setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class)));
            excelContentProperty.setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class)));
            fieldContentMap.put(field.getName(), excelContentProperty);
        }
        return fieldContentMap;
    });
}
Also used : ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) ArrayList(java.util.ArrayList) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException) ContentStyle(com.alibaba.excel.annotation.write.style.ContentStyle) Field(java.lang.reflect.Field) ContentFontStyle(com.alibaba.excel.annotation.write.style.ContentFontStyle) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException)

Aggregations

ExcelProperty (com.alibaba.excel.annotation.ExcelProperty)7 Field (java.lang.reflect.Field)6 ExcelCommonException (com.alibaba.excel.exception.ExcelCommonException)2 NotRequired (io.metersphere.excel.annotation.NotRequired)2 ArrayList (java.util.ArrayList)2 ExcelIgnore (com.alibaba.excel.annotation.ExcelIgnore)1 ContentFontStyle (com.alibaba.excel.annotation.write.style.ContentFontStyle)1 ContentStyle (com.alibaba.excel.annotation.write.style.ContentStyle)1 Head (com.alibaba.excel.metadata.Head)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 CustomMerge (com.macro.mall.tiny.anno.CustomMerge)1 ConstraintViolation (javax.validation.ConstraintViolation)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1