报错:
java.io.IOException: There is no getter for property named 'OrderTime' in 'class com.gobao.hepha.order.vo.excel.OrderImportVo'
注意这个大写的Order,代码书写错误。
应该改成小写,因为getter是小驼峰的,对应的变量是orderTime。
本地excel文件转换为java对象列表:
public static <T> List<T> importExcel(File file, Class<?> pojoClass, ImportParams params) {
FileInputStream in = null;
List var4;
try {
in = new FileInputStream(file);
var4 = (new ExcelImportService()).importExcelByIs(in, pojoClass, params, false).getList();
} catch (ExcelImportException var9) {
throw new ExcelImportException(var9.getType(), var9);
} catch (Exception var10) {
LOGGER.error(var10.getMessage(), var10);
throw new ExcelImportException(var10.getMessage(), var10);
} finally {
IOUtils.closeQuietly(in);
}
return var4;
}
file为文件名,pojoClass为目标对象的类。
调用:
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
params.setNeedSave(true);
params.setSaveUrl("/excel/");
try {
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
throw new IOException("模板不能为空");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}