news 2026/6/23 19:07:06

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/6/23 18:41:47

力扣 22. 括号生成:C++ 实现回溯 + 动态规划双解法,面试高频题必掌握

在算法面试中&#xff0c;括号生成问题是经典的字符串组合题型&#xff0c;力扣第 22 题「括号生成」更是高频考点。题目要求给定括号对数 n&#xff0c;生成所有有效的括号组合&#xff0c;看似简单却能深度考察对回溯、动态规划等核心算法思想的掌握。今天用 C 实现两种最优解…

作者头像 李华
网站建设 2026/6/23 19:08:41

【开题答辩全过程】以 基于Django的大学生理财及记账系统设计与实现为例,包含答辩的问题和答案

个人简介一名14年经验的资深毕设内行人&#xff0c;语言擅长Java、php、微信小程序、Python、Golang、安卓Android等开发项目包括大数据、深度学习、网站、小程序、安卓、算法。平常会做一些项目定制化开发、代码讲解、答辩教学、文档编写、也懂一些降重方面的技巧。感谢大家的…

作者头像 李华
网站建设 2026/6/23 10:11:01

Rust的移动语义

在 Rust 中&#xff0c;默认是移动语义&#xff0c;而不是传统的值传递或引用传递。这是 Rust 最重要的特性之一&#xff0c;理解所有权系统很关键。 基本规则 fn main() {let s1 String::from("hello"); // s1 拥有字符串let s2 s1; // 所有…

作者头像 李华
网站建设 2026/6/23 5:40:02

生物毒性在线分析仪:监测水体毒性的利器

生物毒性在线分析仪是一种用于实时监测环境中生物毒性的仪器设备&#xff0c;可快速、自动地检测水样等对生物的急性综合毒性&#xff0c;为环境监测和安全保障提供重要数据支持。一、工作原理 生物毒性在线分析仪主要利用发光细菌在新陈代谢时发出的光强变化来评估样品的毒性。…

作者头像 李华
网站建设 2026/6/23 0:00:46

3倍效率!用AI自动修复Vue属性传递问题

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个对比实验&#xff1a;左侧展示开发者手动调试Vue非props属性问题的典型步骤(约10步)&#xff0c;右侧展示使用快马AI一键识别和修复同样问题的过程。要求&#xff1a;1) 统…

作者头像 李华