java springboot poi 从controller 接收不同类型excel 文件处理
根据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取到对应的值 */ privatefinalMap title_to_index=newHashMap<>(); /** *所有带有ExcelIn注解的字段 */ privatefinalList fields=newArrayList<>(); /** *统计表格的行和列数量用来遍历表格 */ privateintfirstCellNum=0; privateintlastCellNum=0; privateintfirstRowNum=0; privateintlastRowNum=0; privateStringsheetName; privateSheetsheet; publicList read(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
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短