根据poi接收controller层的excel文件导入
可使用后缀名xls或xlsx格式的excel。
1.pom引入
org.apache.poi
poi
3.17
org.apache.poi
poi-ooxml
3.17
2.ExcelImportUtil工具类创建
importcom.guard.biz.common.util.excel.ExcelIn;
importorg.apache.commons.beanutils.BeanUtilsBean;
importorg.apache.commons.lang3.StringUtils;
importorg.apache.commons.lang3.reflect.FieldUtils;
importorg.apache.poi.hssf.usermodel.HSSFDateUtil;
importorg.apache.poi.ss.usermodel.Cell;
importorg.apache.poi.ss.usermodel.Row;
importorg.apache.poi.ss.usermodel.Sheet;
importorg.apache.poi.ss.usermodel.Workbook;
importorg.apache.poi.ss.usermodel.WorkbookFactory;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStream;
importjava.lang.reflect.Field;
importjava.lang.reflect.InvocationTargetException;
importjava.math.BigDecimal;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
/**
*@authorWei
*@time2019/10/29
*@Descriptionexcel导入工具类
*/
publicclassExcelImportUtil{
privatestaticfinalLoggerlog=LoggerFactory.getLogger(ExcelImportUtil.class);
privatestaticBeanUtilsBeanbeanUtilsBean=newBeanUtilsBean();
static{
beanUtilsBean.getConvertUtils().register(neworg.apache.commons.beanutils.converters.DateConverter(null),java.util.Date.class);
}
/**
*表头名字和对应所在第几列的下标,用于根据title取到对应的值
*/
privatefinalMaptitle_to_index=newHashMap<>();
/**
*所有带有ExcelIn注解的字段
*/
privatefinalListfields=newArrayList<>();
/**
*统计表格的行和列数量用来遍历表格
*/
privateintfirstCellNum=0;
privateintlastCellNum=0;
privateintfirstRowNum=0;
privateintlastRowNum=0;
privateStringsheetName;
privateSheetsheet;
publicListread(InputStreamin,Classclazz)throwsException{
gatherAnnotationFields(clazz);
configSheet(in);
configHeader();
ListrList=null;
try{
rList=readContent(clazz);
}catch(IllegalAccessExceptione){
thrownewException(e);
}catch(InstantiationExceptione){
thrownewException(e);
}catch(InvocationTargetExceptione){
thrownewException(e);
}
returnrList;
}
privateListreadContent(Classclazz)throwsIllegalAccessException,InstantiationException,InvocationTargetException{
Objecto=null;
Rowrow=null;
List