SpringBoot集成Easyexcel-导出

1.创建一个SpringBoot项目(SpringBoot生成

2.导入依赖(在pom中导入)

<!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- easyexcel --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.1</version> </dependency>

3.创建一个实体类(User)

package com.huyi.easyexcel.pojo; import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data; /** * @HeadRowHeight(int):设置表头高度(与 @ContentRowHeight 相反)标记在类上 * @ColumnWidth(int):设置列宽标记在属性上 * @ExcelProperty("String") 是标题 * @ColumnWidth(value = 20) 宽度 value:宽度大小 * @ContentRowHeight(int):设置 row 高度,不包含表头标记在 类上 */ public class User { @ExcelProperty(value ="用户ID",index = 0) private Long userId; @ExcelProperty(value ="用户名",index = 1) private String userName; @ExcelProperty(value ="用户密码",index = 2) private String passWord; @ExcelProperty(value ="用户邮箱",index = 3) private String email; public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public User(Long userId, String userName, String passWord, String email) { this.userId = userId; this.userName = userName; this.passWord = passWord; this.email = email; } public User() { } }

4.创建一个监听器继承AnalysisEventListener

package com.huyi.easyexcel.listenter; import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.huyi.easyexcel.pojo.User; public class ReadListenter extends AnalysisEventListener<User>{ /** * 读取表头 * * @param Map<Integer, String> 表头内容 * @param AnalysisContext 读取合并单元格 */ public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { System.err.println(headMap); } /** * 一行一行的读取 * @param User 实体类 * @param AnalysisContext 读取合并单元格 */ @Override public void invoke(User data, AnalysisContext context) { // TODO Auto-generated method stub System.err.println(data.getUserId()+""+data.getUserName()+""+data.getPassWord()+""+data.getEmail()); } /** * 读取完成后要做的事 * @param AnalysisContext 读取合并单元格 */ @Override public void doAfterAllAnalysed(AnalysisContext context) { // TODO Auto-generated method stub } }

5.在项目的test中测试

package com.huyi.easyexcel; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import com.alibaba.excel.EasyExcel; import com.huyi.easyexcel.listenter.ReadListenter; import com.huyi.easyexcel.pojo.User; @SpringBootTest class EasyexcelApplicationTests { @Test void testRead(){ // 读取excel的位置 String fileName = "E:\\test.xlsx"; /** * fileName 读取excel的位置 * User.class 实体类 * new ExcelListenter() 监听器 */ EasyExcel.read(fileName, User.class,new ReadListenter()).sheet().doRead(); } }
  • 注意:文件如果打开的话运行会报错

6.运行结果如下


__EOF__

本文作者一只小狐疑
本文链接https://www.cnblogs.com/Huyi-1208/p/16406427.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   一只小狐疑  阅读(512)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2021-06-23 Switch、while、do while、for
点击右上角即可分享
微信分享提示