SpringBoot集成EasyExcel实现文件导入导出
一、什么是EasyExcel
EasyExcel 是一款基于Java的开源Excel操作工具,它提供了简单且强大的 API,使开发人员可以轻松地读写、操作和生成Excel文件。
(图片来源网络,侵删)
二、常用注解
-
@ExcelProperty:用于标识实体类中的字段与 Excel 表格中的列的对应关系。
-
@ExcelIgnore:用于标识在读写 Excel 时忽略某个字段。
-
@ExcelIgnoreUnannotated:用于标识在读取 Excel 时忽略未标注 @ExcelProperty 注解的字段。
-
@ExcelTable:用于标识表格的属性,如表头所在行数等。
三、SpringBoot整合
1.添加依赖
主要依赖:
com.alibaba easyexcel 3.1.3
项目完整依赖:
(图片来源网络,侵删)
com.alibaba easyexcel 3.1.3 org.projectlombok lombok com.google.guava guava 28.2-jre org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine mysql mysql-connector-java 8.0.31 com.alibaba druid-spring-boot-starter 1.2.9 com.baomidou mybatis-plus-boot-starter 3.4.0 com.baomidou dynamic-datasource-spring-boot-starter 3.4.0 org.projectlombok lombok cn.hutool hutool-all 5.8.16
2.实体类
@Data public class Stu implements Serializable { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.AUTO) private Integer id; @ExcelProperty("姓名") private String name; @ExcelProperty("年龄") private Integer age; @ExcelProperty("班级") private String calss; @ExcelProperty("学校") private String school; @ExcelProperty(value = "性别") private Integer sex; }
3.自定义监听器
/** * 自定义监听器,对下载的excel中的数据进行校验 * */ public class StuListener extends AnalysisEventListener { List names = new ArrayList(); /** * 每解析一行,回调该方法 * * @param data * @param context */ @Override public void invoke(Object data, AnalysisContext context) { //校验名称 String name = ((Stu) data).getName(); if (StrUtil.isBlank(name)) { throw new RuntimeException(String.format("第%s行名称为空,请核实", context.readRowHolder().getRowIndex() + 1)); } if (names.contains(name)) { throw new RuntimeException(String.format("第%s行名称已重复,请核实", context.readRowHolder().getRowIndex() + 1)); } else { names.add(name); } } /** * 出现异常回调 * * @param exception * @param context * @throws Exception */ @Override public void onException(Exception exception, AnalysisContext context) throws Exception { if (exception instanceof ExcelDataConvertException) { /**从0开始计算*/ Integer columnIndex = ((ExcelDataConvertException) exception).getColumnIndex() + 1; Integer rowIndex = ((ExcelDataConvertException) exception).getRowIndex() + 1; String message = "第" + rowIndex + "行,第" + columnIndex + "列" + "数据格式有误,请核实"; throw new RuntimeException(message); } else if (exception instanceof RuntimeException) { throw exception; } else { super.onException(exception, context); } } /** * 解析完,全部回调 * * @param context */ @Override public void doAfterAllAnalysed(AnalysisContext context) { //解析完,全部回调逻辑实现 names.clear(); } }
4.导入
准备一个导入数据的文件,下方是我的文件:
Controller层代码
@PostMapping(value = "/importData") public void importData(MultipartFile file) { try { //获取文件的输入流 InputStream inputStream = file.getInputStream(); List lst = EasyExcel.read(inputStream) //调用read方法 //注册自定义监听器,字段校验可以在监听器内实现 .registerReadListener(new StuListener()) .head(Stu.class) //对应导入的实体类 .sheet(0) //导入数据的sheet页编号,0代表第一个sheet页,如果不填,则会导入所有sheet页的数据 .headRowNumber(1) //列表头行数,1代表列表头有1行,第二行开始为数据行 .doReadSync(); //开始读Excel,返回一个List集合,继续后续入库操作 System.out.println(lst); // TODO 导入后的处理逻辑 } catch (IOException exception) { throw new RuntimeException(exception); } }
导入成功
5.导出
Controller代码
(图片来源网络,侵删)
/** * 导出数据 * */ @GetMapping("/export/user") public void exportUserExcel(HttpServletResponse response) throws IOException { OutputStream outputStream=response.getOutputStream(); try { String filePath = "userlist.xlsx"; // 保存到当前项目下的文件名 File file = new File(filePath); this.setExcelResponseProp(response, "用户列表"); // 模拟根据条件在数据库查询数据 // List stus = stuMapper.selectList(null); List stus = new ArrayList(); for (int i = 0; i导出成功
以上一个简单的demo就编写完成了。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...