public static <T> List<T> excel2Pojo(Workbook workbook, Class<T> clazz) throws Exception {
if (!(null != workbook && null != clazz)) {
return null;
}
List<T> poList = null;
try {
Workbook excel = workbook;
Sheet sheet = excel.getSheetAt(0);
// 获取到总共有多少行
int rowNum = sheet.getLastRowNum();
// 获取到标题行
Row headRow = sheet.getRow(0);
// 获取到标题总共有多少列
short cellNum = headRow.getLastCellNum();
// 创建集合用来存储实例化的对象
poList = new ArrayList<>();
// 循环遍历每一行记录
for (int i = 1; i <= rowNum; i++) {
// 创建实体类
T newPojo = clazz.newInstance();
// 遍历每一列记录
for (int j = 0; j < cellNum; j++) {
// 获取到标题文字
String headName = headRow.getCell(j).toString();
/**
* 遍历要封装类的属性并获取起注解上方 属性命名 查看是否和当前Excel的标题相同
* 相同则封装
* 不同则不理
*/
// 获取所有的字段反射对象
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
// 获取到注解类型
PropertiesName annotation = field.getAnnotation(PropertiesName.class);
if(annotation==null){
continue;
}
// 获取到属性命名
String propertiesName = annotation.name();
if (headName.equals(propertiesName)) {
// Excel标题头和属性相对应
Class<? extends Object> type = field.getType();
if (type.equals(Integer.class)) {
if(sheet.getRow(i).getCell(j)!=null){
String string = sheet.getRow(i).getCell(j).toString();
int index = string.lastIndexOf(".");
string = string.substring(0,index);
field.set(newPojo, Integer.valueOf(string));
}
} else if (type.equals(String.class)) {
if(sheet.getRow(i).getCell(j)!=null)
field.set(newPojo, sheet.getRow(i).getCell(j).toString());
} else if (type.equals(Double.class)) {
if(sheet.getRow(i).getCell(j)!=null)
field.set(newPojo, new Double(sheet.getRow(i).getCell(j).toString()));
} else if (type.equals(BigDecimal.class)) {
if(sheet.getRow(i).getCell(j)!=null)
field.set(newPojo, new BigDecimal(sheet.getRow(i).getCell(j).toString()));
} else if (type.equals(Date.class)) {
SimpleDateFormat sdf = null;
String dateStr = sheet.getRow(i).getCell(j).toString();
// 验证 是否为:yyyy-MM-dd HH:mm:ss
if (dateStr.matches(DateFormatEnum.REGEX_ONE.getValue())) {
sdf = new SimpleDateFormat(DateFormatEnum.FORMAT_ONE.getValue());
Date date = sdf.parse(dateStr);
field.set(newPojo, date);
} else if (dateStr.matches(DateFormatEnum.REGEX_TWO.getValue())) {
sdf = new SimpleDateFormat(DateFormatEnum.FORMAT_TWO.getValue());
Date date = sdf.parse(dateStr);
field.set(newPojo, date);
} else if (dateStr.matches(DateFormatEnum.REGEX_THREE.getValue())) {
sdf = new SimpleDateFormat(DateFormatEnum.FORMAT_THREE.getValue());
Date date = sdf.parse(dateStr);
field.set(newPojo, date);
} else if (dateStr.matches(DateFormatEnum.REGEX_FOUR.getValue())) {
sdf = new SimpleDateFormat(DateFormatEnum.FORMAT_FOUR.getValue());
Date date = sdf.parse(dateStr);
field.set(newPojo, date);
}
}
}
}
}
poList.add(newPojo);
}
} catch (Exception e) {
throw e;
}
return poList;
}
这个workbook类是org.apache.poi.ss.usermodel.Workbook这个包
另外需要一个注解类
/**
* 给Bean属性上加上名称
*
* @author 大漠知秋
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertiesName {
public String name();
}
将实体类名都用注解进行关联
所获取的实体类集合用BeanUtils.copy( . )方法复制到我们正式的实体类中
再使用批量插入的方法插入到数据库中