news 2026/7/7 9:54:32

Easypoi Excel导入校验 两种方式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Easypoi Excel导入校验 两种方式

案例一

用JSR 303校验

所用数据

结果

Controller层

@CrossOrigin@PostMapping("/importStudentVerify")publicStringimportStudentVerify(@RequestParam("file")MultipartFilefile,HttpServletResponseresponse)throwsException{if(file.isEmpty()){thrownewException("error file is empty");}ImportParamsparams=newImportParams();//这里也要设置,否则会有异常params.setTitleRows(0);params.setHeadRows(1);params.setNeedVerify(true);//这里结合JSR303用不用自定义的,下一案例用自定义的//params.setVerifyHandler(verifyResult);//这里要用importExcelMore方法ExcelImportResult<StudentImportVerifyEntity2>studentImportEntitiesResult=ExcelImportUtil.importExcelMore(file.getInputStream(),StudentImportVerifyEntity2.class,params);//获取失败的数据List<StudentImportVerifyEntity2>failStudentsImport=studentImportEntitiesResult.getFailList();System.out.println(failStudentsImport);List<StudentImportVerifyEntity2>studentImportEntities=studentImportEntitiesResult.getList();//没有失败的if(CollectionUtil.isEmpty(failStudentsImport)){List<Student>students=studentImportEntities.stream().map(e->{Studentstudent=newStudent();student.setName(e.getName());student.setSex(e.getSex());// 创建一个LocalTime实例,例如午夜12点LocalTimelocalTime=LocalTime.MIDNIGHT;LocalDatebirthDayLocalDate=LocalDate.parse(e.getBirthDay(),DateTimeFormatter.ofPattern("yyyy-MM-dd"));//LocalDate转LocalDateTimeLocalDateTimebirthDayLocalDateTime=LocalDateTime.of(birthDayLocalDate,localTime);student.setBirthDay(birthDayLocalDateTime);LocalDateregistrationLocalDate=LocalDate.parse(e.getRegistrationDate(),DateTimeFormatter.ofPattern("yyyy-MM-dd"));LocalDateTimeregistrationLocalDateTime=LocalDateTime.of(registrationLocalDate,localTime);student.setRegistrationDate(registrationLocalDateTime);returnstudent;}).collect(Collectors.toList());studentService.saveBatch(students);return"成功";}return"excel有错误数据";}

StudentImportVerifyEntity2

//这里要实现IExcelModel接口才有errorMsg输出@DatapublicclassStudentImportVerifyEntity2implementsjava.io.Serializable,IExcelModel{/** * 学生姓名 */@Excel(name="学生姓名",orderNum="0")//这里要加上JSR 303校验@NotNull(message="学生姓名不能为空")privateStringname;/** * 学生性别 */@Excel(name="学生性别",replace={"男_1","女_0"},orderNum="1",suffix="生")privateintsex;@Excel(name="出生日期",orderNum="2",importFormat="yyyy-MM-dd")privateStringbirthDay;@Excel(name="进校日期",orderNum="3",importFormat="yyyy-MM-dd")privateStringregistrationDate;privateStringerrorMsg;@OverridepublicStringgetErrorMsg(){returnerrorMsg;}@OverridepublicvoidsetErrorMsg(StringerrorMsg){this.errorMsg=errorMsg;}}

案例二

自定义校验处理器

处理器:

@ServicepublicclassVerifyResultimplementsIExcelVerifyHandler<StudentImportVerifyEntity>{@OverridepublicExcelVerifyHandlerResultverifyHandler(StudentImportVerifyEntitystudentImportVerifyEntity){ExcelVerifyHandlerResultresult=newExcelVerifyHandlerResult(true);if(Objects.isNull(studentImportVerifyEntity.getName())){result.setSuccess(false);result.setMsg("姓名不能为空");returnresult;}result.setSuccess(true);returnresult;}}

Controller层

@ResourceprivateVerifyResultverifyResult;@CrossOrigin@PostMapping("/importStudentVerify")publicStringimportStudentVerify(@RequestParam("file")MultipartFilefile,HttpServletResponseresponse)throwsException{if(file.isEmpty()){thrownewException("error file is empty");}ImportParamsparams=newImportParams();//这里也要设置,否则会有异常params.setTitleRows(0);params.setHeadRows(1);params.setNeedVerify(true);//这里设置处理器params.setVerifyHandler(verifyResult);//注意这里用的entity是StudentImportVerifyEntityExcelImportResult<StudentImportVerifyEntity>studentImportEntitiesResult=ExcelImportUtil.importExcelMore(file.getInputStream(),StudentImportVerifyEntity.class,params);//获取失败的数据List<StudentImportVerifyEntity>failStudentsImport=studentImportEntitiesResult.getFailList();System.out.println(failStudentsImport);List<StudentImportVerifyEntity>studentImportEntities=studentImportEntitiesResult.getList();//没有失败的if(CollectionUtil.isEmpty(failStudentsImport)){List<Student>students=studentImportEntities.stream().map(e->{Studentstudent=newStudent();student.setName(e.getName());student.setSex(e.getSex());// 创建一个LocalTime实例,例如午夜12点LocalTimelocalTime=LocalTime.MIDNIGHT;LocalDatebirthDayLocalDate=LocalDate.parse(e.getBirthDay(),DateTimeFormatter.ofPattern("yyyy-MM-dd"));//LocalDate转LocalDateTimeLocalDateTimebirthDayLocalDateTime=LocalDateTime.of(birthDayLocalDate,localTime);student.setBirthDay(birthDayLocalDateTime);LocalDateregistrationLocalDate=LocalDate.parse(e.getRegistrationDate(),DateTimeFormatter.ofPattern("yyyy-MM-dd"));LocalDateTimeregistrationLocalDateTime=LocalDateTime.of(registrationLocalDate,localTime);student.setRegistrationDate(registrationLocalDateTime);returnstudent;}).collect(Collectors.toList());studentService.saveBatch(students);return"成功";}return"excel有错误数据";}

StudentImportVerifyEntity

@DatapublicclassStudentImportVerifyEntityimplementsIExcelDataModel,IExcelModel,java.io.Serializable{/** * 学生姓名 */@Excel(name="学生姓名",orderNum="0")//JSR 303的注解注掉了走的是自定义处理器//@NotNull(message = "学生姓名不能为空")privateStringname;/** * 学生性别 */@Excel(name="学生性别",replace={"男_1","女_0"},orderNum="1",suffix="生")privateintsex;@Excel(name="出生日期",orderNum="2",importFormat="yyyy-MM-dd")privateStringbirthDay;@Excel(name="进校日期",orderNum="3",importFormat="yyyy-MM-dd")privateStringregistrationDate;/** * 行号 */privateintrowNum;/** * 错误消息 */privateStringerrorMsg;@OverridepublicStringgetErrorMsg(){returnerrorMsg;}@OverridepublicvoidsetErrorMsg(StringerrorMsg){this.errorMsg=errorMsg;}@OverridepublicintgetRowNum(){returnrowNum;}@OverridepublicvoidsetRowNum(inti){this.rowNum=rowNum;}}


pom引用

<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.1.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.1.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.1.0</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.2.4.Final</version></dependency><dependency><groupId>javax.el</groupId><artifactId>javax.el-api</artifactId><version>3.0.0</version></dependency><dependency><groupId>org.glassfish.web</groupId><artifactId>javax.el</artifactId><version>2.2.6</version></dependency>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/7 6:10:46

赢战2025电商新趋势下的增长策略报告:平台格局、跨境趋势、大促消费|附600+份报告PDF、数据、可视化模板汇总下载

原文链接&#xff1a;https://tecdat.cn/?p44562 原文出处&#xff1a;拓端抖音号拓端tecdat 引言 2025年电商行业正迎来“存量竞争增量突围”的关键拐点&#xff1a;国内市场从“流量争夺”转向“心智占领”&#xff0c;阿里、抖音等平台形成多元竞合格局&#xff1b;跨境赛…

作者头像 李华
网站建设 2026/7/7 17:48:50

英属哥伦比亚大学揭示AI工具学习中的致命缺陷

这项由英属哥伦比亚大学的邓文龙、李玉舒等研究团队以及加州大学伯克利分校的龚博英合作完成的研究&#xff0c;发表于2025年12月3日的arXiv预印本平台&#xff08;论文编号&#xff1a;arXiv:2512.04220v1&#xff09;。研究团队深入分析了当前最先进的AI搜索助手在学习过程中…

作者头像 李华
网站建设 2026/7/7 0:12:12

JVET-AG0058

一、提案主要内容总结 提案标题&#xff1a;EE2-1.14: An extrapolation filter-based intra prediction mode (EIP) 基于外推滤波的帧内预测模式提出单位与作者&#xff1a; 单位&#xff1a;OPPO作者&#xff1a;Luhang Xu, Yue Yu, Haoping Yu, Jonathan Gan, Dong Wang联系…

作者头像 李华
网站建设 2026/7/7 15:42:24

面试实战 问题三十三 Spring 事务常用注解

面试实战 问题三十三 Spring 事务常用注解 Spring事务常用注解 在Spring框架中&#xff0c;事务管理主要通过声明式事务实现&#xff0c;核心注解如下&#xff1a;Transactional 最核心的事务注解&#xff0c;用于方法或类级别。标注后&#xff0c;Spring通过AOP在方法执行前后…

作者头像 李华
网站建设 2026/7/7 13:00:57

基于SpringBoot的养老院管理系统-计算机毕业设计源码+LW文档分享

摘 要 随着社会的不断进步&#xff0c;系统管理的复杂性日益加剧。互联网已成为用户获取信息的主要途径&#xff0c;然而&#xff0c;信息繁杂且真伪难辨。为了确保用户能够便捷、准确地获取养老院管理的相关信息&#xff0c;设计一款既安全又高效的养老院管理系统显得尤为关键…

作者头像 李华