poi通用匯入excell工具類
在對應的pom檔案新增對應的maven依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
/**
* 匯入Excel
*
* @param clazz
* @param xls
* @return
* @throws Exception
*/
@SuppressWarnings("rawtypes")
public static List importExcel(Class<?> clazz, InputStream xls, String fileName) throws Exception {
try {
// 相容2003和2007
Workbook wb = getWorkbook(xls, fileName);
// 取得Excel
Sheet sheet = wb.getSheetAt(0);
Field[] fields = clazz.getDeclaredFields();
List<Field> fieldList = new ArrayList<Field>(fields.length);
for (Field field : fields) {
if (field.isAnnotationPresent(ModelProp.class)) {
ModelProp modelProp = field.getAnnotation(ModelProp.class);
if (modelProp.colIndex() != -1) {
fieldList.add(field);
}
}
}
// 行迴圈
List<ImportModel> modelList = new ArrayList<ImportModel>(sheet.getPhysicalNumberOfRows() * 2);
for (int i = 2; i < sheet.getPhysicalNumberOfRows(); i++) {
// 資料模型
ImportModel model = (ImportModel) clazz.newInstance();
int nullCount = 0;
Exception nullError = null;
int colIndex = 0;
for (Field field : fieldList) {
ModelProp modelProp = field.getAnnotation(ModelProp.class);
Cell cell = sheet.getRow(i).getCell(modelProp.colIndex());
try {
if (cell == null || cell.toString().length() == 0) {
/*nullCount++;
if (!modelProp.nullable()) {
nullError = new Exception(StringUtil.format(modelProp.name()+":存在為空的資料",
new String[] { "" + (1 + i), modelProp.name(), modelProp.name() }));
}*/
continue;
} else if (field.getType().equals(Date.class)) {
if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(), new Date(parseDate(parseString(cell))));
} else {
BeanUtils.setProperty(model, field.getName(),
new Date(cell.getDateCellValue().getTime()));
}
} else if (field.getType().equals(Timestamp.class)) {
if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(),
new Timestamp(parseDate(parseString(cell))));
} else {
BeanUtils.setProperty(model, field.getName(),
new Timestamp(cell.getDateCellValue().getTime()));
}
} else if (field.getType().equals(java.sql.Date.class)) {
if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(),
new java.sql.Date(parseDate(parseString(cell))));
} else {
BeanUtils.setProperty(model, field.getName(),
new java.sql.Date(cell.getDateCellValue().getTime()));
}
} else if (field.getType().equals(java.lang.Integer.class)) {
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(), (int) cell.getNumericCellValue());
} else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(), Integer.parseInt(parseString(cell)));
}
} else if (field.getType().equals(java.math.BigDecimal.class)) {
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(),
new BigDecimal(cell.getNumericCellValue()));
} else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(), new BigDecimal(parseString(cell)));
}
} else {
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(),
new BigDecimal(parseString(cell)));
} else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
BeanUtils.setProperty(model, field.getName(), parseString(cell));
}
}
colIndex++;
} catch (Exception e) {
e.printStackTrace();
throw new Exception(StringUtil.format(errormsg, new String[] { "" + (1 + i), modelProp.name() })
+ "," + e.getMessage());
}
}
if (nullCount == fieldList.size()) {
break;
}
if (nullError != null) {
throw nullError;
}
modelList.add(model);
}
return modelList;
} finally {
xls.close();
}
}