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
热门推荐
4 合字的祝福语简短
8 庆双节祝福语简短
10 送给老师中秋祝福语简短
11 圣诞祝福语简短文字
12 新生祝福语简短8字
13 18岁简短祝福语经典
14 5岁宝宝祝福语简短
15 足球队祝福语简短
16 过年爱情祝福语大全简短
17 到同事新家简短祝福语
18 祝球员赛前祝福语简短