Search in sources :

Example 1 with TitleCell

use of com.jeesuite.common2.excel.annotation.TitleCell in project jeesuite-libs by vakinge.

the class ExcelBeanHelper method doCacheClass.

/**
 * @param clazz
 * @param canonicalName
 * @return
 * @throws IntrospectionException
 */
private static synchronized void doCacheClass(Class<?> clazz, String canonicalName) throws Exception {
    if (aliasPropertyDescriptorCache.containsKey(canonicalName))
        return;
    Map<String, PropertyDescriptor> map = new HashMap<>();
    Map<String, PropertyDescriptor> aliasMap = new HashMap<>();
    List<TitleMeta> titleMetas = new ArrayList<>();
    BeanInfo srcBeanInfo = Introspector.getBeanInfo(clazz);
    PropertyDescriptor[] descriptors = srcBeanInfo.getPropertyDescriptors();
    Map<String, TitleMeta> parentMap = new HashMap<>();
    int maxRow = 1;
    for (PropertyDescriptor descriptor : descriptors) {
        String name = descriptor.getName();
        if ("class".equals(name))
            continue;
        Method readMethod = descriptor.getReadMethod();
        Method writeMethod = descriptor.getWriteMethod();
        if (readMethod == null)
            try {
                readMethod = clazz.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
                descriptor.setReadMethod(readMethod);
            } catch (NoSuchMethodException | SecurityException e) {
            }
        if (writeMethod == null)
            try {
                writeMethod = clazz.getMethod("set" + name.substring(0, 1).toUpperCase() + name.substring(1), descriptor.getPropertyType());
                descriptor.setWriteMethod(writeMethod);
            } catch (NoSuchMethodException | SecurityException e) {
            }
        if (readMethod != null || writeMethod != null) {
            map.put(descriptor.getName(), descriptor);
            // 
            TitleCell annotation = null;
            try {
                annotation = clazz.getDeclaredField(name).getAnnotation(TitleCell.class);
            } catch (NoSuchFieldException e) {
                annotation = clazz.getSuperclass().getDeclaredField(name).getAnnotation(TitleCell.class);
            }
            if (annotation != null) {
                aliasMap.put(annotation.name().trim(), descriptor);
                TitleMeta cell = new TitleMeta(annotation.name());
                cell.setValueType(annotation.type());
                if (StringUtils.isBlank(annotation.parentName())) {
                    cell.setColumnIndex(annotation.column());
                    cell.setRowIndex(annotation.row());
                    titleMetas.add(cell);
                } else {
                    TitleMeta cellParent = parentMap.get(annotation.parentName());
                    if (cellParent == null) {
                        cellParent = new TitleMeta(annotation.parentName());
                        cellParent.setValueType(annotation.type());
                        cellParent.setColumnIndex(annotation.column());
                        parentMap.put(annotation.parentName(), cellParent);
                        titleMetas.add(cellParent);
                    }
                    cell.setColumnIndex(annotation.column());
                    cell.setRowIndex(annotation.row());
                    cellParent.addChildren(cell);
                    maxRow = annotation.row() > maxRow ? annotation.row() : maxRow;
                }
            }
        }
    }
    ExcelMeta excelMeta = new ExcelMeta(clazz, titleMetas, maxRow);
    titleCellBeanCache.put(canonicalName, excelMeta);
    aliasPropertyDescriptorCache.put(canonicalName, aliasMap);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) TitleCell(com.jeesuite.common2.excel.annotation.TitleCell) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ExcelMeta(com.jeesuite.common2.excel.model.ExcelMeta) TitleMeta(com.jeesuite.common2.excel.model.TitleMeta)

Example 2 with TitleCell

use of com.jeesuite.common2.excel.annotation.TitleCell in project jeesuite-libs by vakinge.

the class ExcelReader method parse.

/**
 * 解析读取excel文件
 *
 * @param clazz 对应的映射类型
 * @param <T>   泛型
 * @return 读取结果
 */
public <T> List<T> parse(Class<T> clazz) {
    List<T> resultList = null;
    try {
        Sheet sheet = workbook.getSheet(this.sheetName);
        if (null != sheet) {
            resultList = new ArrayList<T>(sheet.getLastRowNum() - 1);
            Row row = sheet.getRow(this.startRow);
            Map<String, Field> fieldMap = new HashMap<String, Field>();
            Map<String, String> titleMap = new HashMap<String, String>();
            Field[] fields = clazz.getDeclaredFields();
            // 这里开始处理映射类型里的注解
            for (Field field : fields) {
                if (field.isAnnotationPresent(TitleCell.class)) {
                    TitleCell mapperCell = field.getAnnotation(TitleCell.class);
                    fieldMap.put(mapperCell.name(), field);
                }
            }
            for (Cell title : row) {
                CellReference cellRef = new CellReference(title);
                titleMap.put(cellRef.getCellRefParts()[2], title.getRichStringCellValue().getString());
            }
            for (int i = this.startRow + 1; i <= sheet.getLastRowNum(); i++) {
                T t = clazz.newInstance();
                Row dataRow = sheet.getRow(i);
                for (Cell data : dataRow) {
                    CellReference cellRef = new CellReference(data);
                    String cellTag = cellRef.getCellRefParts()[2];
                    String name = titleMap.get(cellTag);
                    Field field = fieldMap.get(name);
                    if (null != field) {
                        field.setAccessible(true);
                        getCellValue(data, t, field);
                    }
                }
                resultList.add(t);
            }
        } else {
            throw new RuntimeException("sheetName:" + this.sheetName + " is not exist");
        }
    } catch (InstantiationException e) {
        LOG.error("初始化异常", e);
    } catch (IllegalAccessException e) {
        LOG.error("初始化异常", e);
    } catch (ParseException e) {
        LOG.error("时间格式化异常:{}", e);
    } catch (Exception e) {
        LOG.error("其他异常", e);
    }
    return resultList;
}
Also used : HashMap(java.util.HashMap) TitleCell(com.jeesuite.common2.excel.annotation.TitleCell) CellReference(org.apache.poi.ss.util.CellReference) ParseException(java.text.ParseException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) Field(java.lang.reflect.Field) Row(org.apache.poi.ss.usermodel.Row) ParseException(java.text.ParseException) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) TitleCell(com.jeesuite.common2.excel.annotation.TitleCell)

Example 3 with TitleCell

use of com.jeesuite.common2.excel.annotation.TitleCell in project jeesuite-libs by vakinge.

the class PersonSalaryInfo method main.

public static void main(String[] args) {
    List<TitleMeta> titleCellBeans = new ArrayList<>();
    Field[] fields = PersonSalaryInfo.class.getDeclaredFields();
    Map<String, TitleMeta> parentMap = new HashMap<>();
    int index = 0, subIndex = 0;
    for (Field field : fields) {
        if (!field.isAnnotationPresent(TitleCell.class))
            continue;
        TitleCell annotation = field.getAnnotation(TitleCell.class);
        TitleMeta cell = new TitleMeta(annotation.name());
        if (StringUtils.isBlank(annotation.parentName())) {
            cell.setColumnIndex(++index);
            titleCellBeans.add(cell);
        } else {
            TitleMeta cellParent = parentMap.get(annotation.parentName());
            if (cellParent == null) {
                subIndex = index;
                cellParent = new TitleMeta(annotation.parentName());
                cellParent.setColumnIndex(++index);
                parentMap.put(annotation.parentName(), cellParent);
                titleCellBeans.add(cellParent);
            }
            cell.setColumnIndex(++subIndex);
            cell.setRowIndex(1);
            cellParent.addChildren(cell);
        }
    }
    for (TitleMeta cell : titleCellBeans) {
        System.out.println(cell);
        if (cell.getChildren().size() > 0) {
            for (TitleMeta child : cell.getChildren()) {
                System.out.println("--" + child);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) TitleCell(com.jeesuite.common2.excel.annotation.TitleCell) ArrayList(java.util.ArrayList) TitleMeta(com.jeesuite.common2.excel.model.TitleMeta)

Aggregations

TitleCell (com.jeesuite.common2.excel.annotation.TitleCell)3 HashMap (java.util.HashMap)3 TitleMeta (com.jeesuite.common2.excel.model.TitleMeta)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 ExcelMeta (com.jeesuite.common2.excel.model.ExcelMeta)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ParseException (java.text.ParseException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)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 CellReference (org.apache.poi.ss.util.CellReference)1