news 2026/6/9 23:33:23

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/10 14:30:45

【专家亲授】边缘计算场景下Docker部署Agent的最佳实践

第一章&#xff1a;边缘 Agent 的 Docker 轻量级部署在物联网和边缘计算场景中&#xff0c;边缘 Agent 扮演着设备与云端通信的核心角色。为提升部署效率与环境隔离性&#xff0c;采用 Docker 容器化技术实现轻量级、可移植的部署方案成为最佳实践。环境准备 部署前需确保目标边…

作者头像 李华
网站建设 2026/6/10 15:15:47

Dify企业级实战深度解析 (1)

一、学习目标本集作为 Dify 与 Deepseek 联动实战的入门开篇&#xff0c;核心目标聚焦于夯实两大工具的基础使用能力&#xff1a;一方面帮助学习者全面掌握 Dify 开源 AI 应用开发平台的核心操作逻辑与界面交互规则&#xff0c;理解其低代码开发的设计理念&#xff1b;另一方面…

作者头像 李华
网站建设 2026/6/10 5:25:49

halcon gray图像转bgra图像

dev_update_off () dev_close_window ()* * 加载黑白图像 * read_image (GrayImage, C:/Users/范/Pictures/20250410110735.bmp)* 如果不是灰度图像&#xff0c;转换为灰度 count_channels (GrayImage, Channels) if (Channels ! 1)rgb1_to_gray (GrayImage, GrayImage) endif…

作者头像 李华
网站建设 2026/6/9 18:36:04

【图像分割】基于EM算法无监督图像分割附Matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 &#x1f34e; 往期回顾关注个人主页&#xff1a;Matlab科研工作室 &#x1f34a;个人信条&#xff1a;格物致知,完整Matlab代码获取及仿…

作者头像 李华
网站建设 2026/6/9 15:25:29

锂电池PSE认证流程是什么?

锂电池 PSE 认证核心流程分前期准备、样品测试、文件审核与发证备案&#xff0c;菱形 PSE 还需额外工厂审查&#xff0c;整体遵循日本 METI 与 JIS 标准&#xff08;JIS C 8712/8714&#xff09;。一、前期准备&#xff08;3-5 个工作日&#xff09;认证类型判定按额定能量划分…

作者头像 李华
网站建设 2026/6/10 15:34:18

JVET-AL0205

&#x1f4c4; JVET-AL0205-r1 提案分析 标题&#xff1a;EE2-2.1: EIP filters with diagonal shapes 作者&#xff1a;Krit Panusopone, Michelle He, Seungwook Hong 等&#xff08;Nokia&#xff09; 会议&#xff1a;JVET 第38次会议&#xff08;线上&#xff0c;2025年3月…

作者头像 李华