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);
}
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;
}
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);
}
}
}
}
Aggregations