@NotNull,@Max,@Min,@Pattern这些注解在message属性上加入你想输出的错误信息 。
6、导入工具
/*** excel 导入** @param headerRows表头行* @param needVerfiy是否检验excel内容* @param pojoClasspojo类型* @param <T>* @return*/public static <T> ExcelImportResult<T> importExcel(MultipartFile file, Integer headerRows, boolean needVerfiy, Class<T> pojoClass) throws IOException {if (file == null) {log.info("文件为空");return null;}ImportParams params = new ImportParams();params.setHeadRows(headerRows); //头行忽略的行数params.setNeedVerfiy(needVerfiy); //是否开启校验try {return ExcelImportUtil.importExcelMore(file.getInputStream(), pojoClass, params);} catch (Exception e) {log.error("importExcel IOException {} ",e.getMessage());throw new IOException(e.getMessage());}}ExcelImportResult是一个导入返回的类,里面有很多属性,讲一下常用的 。
/*** 结果集*/private List<T>list;/*** 失败数据*/private List<T>failList;/*** 是否存在校验失败*/private booleanverfiyFail;7、导入示例
@PostMapping("/import")public void importExcel(MultipartFile file){if (file==null){log.info("file 无数据");return;}ExcelImportResult<People> result = null;try {result = ExcelUtil.importExcel(file, 1, true, People.class);} catch (IOException e) {e.printStackTrace();}List<People> failList = result.getFailList();//获取失败的数据if (failList.size()>0){for ( People people : failList) {log.info("第{}行,{}",people.getRowNum(),people.getErrorMsg());//打印失败的行 和失败的信息}}//如果没有错误,可以存入文件服务系统 或者数据库 ,这里只是将数据打印出来List<People> list = result.getList();log.info("成功导入数据 {}",JSON.toJSONString(list));}
附easypoi文档:http://easypoi.mydoc.io/
推荐阅读
- Spring Boot 集成 MyBatis
- Springboot集成阿里云对象存储OSS
- Springboot集成阿里云视频点播
- SpringBoot整合定时器:定时任务不再硬编码,动态定时刷起来
- SpringBoot的可视化接口开发工具
- SpringBoot:拒绝大文件,夏天到了,来给jar包瘦个身
- Springboot 动态设置注解参数值
- 聊一聊 SpringBoot 中配置加载优先级?
- springboot三种拦截器
- SpringBoot集成多数据源
