该说说优点了,比如注解式 起手, 导出,可以无侵入代码,可以侵入代码写法, 侵入代码写法,可以明确校验字段,字段转换 有dataHandle 作者自己定义了一套 对应类型的转化 formatter 机制
比如时间 --》dataformat
比如数字类型–〉 小树
比如枚举类型,自动替换id 为汉字
支持sax 大批量导入
缺点代码:
private List importExcel(Collection result, Sheet sheet, Class> pojoClass, ImportParams params, Map pictures) throws Exception {
List collection = new ArrayList();
Map excelParams = new HashMap();
List excelCollection = new ArrayList();
String targetId = null;
this.i18nHandler = params.getI18nHandler();
boolean isMap = Map.class.equals(pojoClass);
if (!isMap) {
Field[] fileds = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = (ExcelTarget)pojoClass.getAnnotation(ExcelTarget.class);
if (etarget != null) {
targetId = etarget.value();
}
this.getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, (List)null, (ExcelEntity)null);
}
Iterator rows = sheet.rowIterator();
for(int j = 0; j < params.getTitleRows(); ++j) {
rows.next();
}
Map titlemap = this.getTitleMap(rows, params, excelCollection, excelParams);
this.checkIsValidTemplate(titlemap, excelParams, params, excelCollection);
Row row = null;
Object object = null;
int readRow = 1;
for(int i = 0; i < params.getStartRows(); ++i) {
rows.next();
}
if (excelCollection.size() > 0 && params.getKeyIndex() == null) {
params.setKeyIndex(0);
}
if (params.isConcurrentTask()) {
ForkJoinPool forkJoinPool = new ForkJoinPool();
int endRow = sheet.getLastRowNum() - params.getLastOfInvalidRow();
if (params.getReadRows() > 0) {
endRow = params.getReadRows();
}
ExcelImportForkJoinWork task = new ExcelImportForkJoinWork(params.getStartRows() + params.getHeadRows() + params.getTitleRows(), endRow, sheet, params, pojoClass, this, targetId, titlemap, excelParams);
ExcelImportResult forkJoinResult = (ExcelImportResult)forkJoinPool.invoke(task);
collection = forkJoinResult.getList();
this.failCollection = forkJoinResult.getFailList();
return (List)collection;
} else {
while(true) {
do {
if (!rows.hasNext() || row != null && sheet.getLastRowNum() - row.getRowNum() <= params.getLastOfInvalidRow() || params.getReadRows() > 0 && readRow > params.getReadRows()) {
return (List)collection;
}
row = (Row)rows.next();
if (sheet.getLastRowNum() - row.getRowNum() < params.getLastOfInvalidRow()) {
return (List)collection;
}
} while(row.getLastCellNum() < 0);
if (isMap && object != null) {
((Map)object).put("excelRowNum", row.getRowNum());
}
StringBuilder errorMsg = new StringBuilder();
//有问题: 第一次,除去标题行,object!=null==》永远false 所以第一数据行为空 不会忽略
if (params.getKeyIndex() != null && (row.getCell(params.getKeyIndex()) == null || StringUtils.isEmpty(this.getKeyValue(row.getCell(params.getKeyIndex())))) && object != null) {
Iterator var32 = excelCollection.iterator();
while(var32.hasNext()) {
ExcelCollectionParams param = (ExcelCollectionParams)var32.next();
this.addListContinue(object, param, row, titlemap, targetId, pictures, params, errorMsg);
}
} else {
object = PoiPublicUtil.createObject(pojoClass, targetId);
try {
Set keys = titlemap.keySet();
Iterator var19 = keys.iterator();
label136:
while(true) {
while(true) {
Integer cn;
Cell cell;
String titleString;
do {
if (!var19.hasNext()) {
if (object instanceof IExcelDataModel) {
((IExcelDataModel)object).setRowNum(row.getRowNum());
}
var19 = excelCollection.iterator();
while(var19.hasNext()) {
ExcelCollectionParams param = (ExcelCollectionParams)var19.next();
this.addListContinue(object, param, row, titlemap, targetId, pictures, params, errorMsg);
}
if (this.verifyingDataValidity(object, row, params, isMap, errorMsg)) {
((List)collection).add(object);
} else {
this.failCollection.add(object);
}
break label136;
}
cn = (Integer)var19.next();
cell = row.getCell(cn);
titleString = (String)titlemap.get(cn);
} while(!excelParams.containsKey(titleString) && !isMap);
if (excelParams.get(titleString) != null && ((ExcelImportEntity)excelParams.get(titleString)).getType() == BaseEntityTypeConstants.IMAGE_TYPE) {
String picId = row.getRowNum() + "_" + cn;
this.saveImage(object, picId, excelParams, titleString, pictures, params);
} else {
try {
this.saveFieldValue(params, object, cell, excelParams, titleString, row);
} catch (ExcelImportException var24) {
if (params.isNeedVerify() && ExcelImportEnum.GET_VALUE_ERROR.equals(var24.getType())) {
errorMsg.append(" ").append(titleString).append(ExcelImportEnum.GET_VALUE_ERROR.getMsg());
}
}
}
}
}
} catch (ExcelImportException var25) {
LOGGER.error("excel import error , row num:{},obj:{}", readRow, ReflectionToStringBuilder.toString(object));
if (!var25.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
throw new ExcelImportException(var25.getType(), var25);
}
} catch (Exception var26) {
LOGGER.error("excel import error , row num:{},obj:{}", readRow, ReflectionToStringBuilder.toString(object));
throw new RuntimeException(var26);
}
}
++readRow;
}
}
}