Spring Boot 引入 easyexcel 最新版本 3.3.2,实现读写 Excel

EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能

在 Spring Boot 环境中使用 easyexcel,需要完成以下几个步骤

1、引入依赖

pom.xml中引入easyexcel的依赖,此处版本号3.3.2

<dependency>
    <groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>3.3.2</version>
</dependency>

2、easyexcel读取excel

建立一个实体对象,保存读取excel后的数据。可以设置读取的excel的列名。

@Data
public class ArticleScoreData {
    @ExcelProperty("姓名")
    private String name;
    @ExcelProperty("文章")
    private String title;
    @ExcelProperty("得分")
    private Double score;
}

easyexcel读操作:

public void testRead() {
        String fileName = "D://article.xls";
        EasyExcel.read(fileName, ArticleScoreData.class, new PageReadListener<ArticleScoreData>(dataList -> {
            for (ArticleScoreData demoData : dataList) {
                log.info("读取到一条数据{}", "姓名:" + demoData.getName() + " 文章:" + demoData.getTitle() + " 得分:" + demoData.getScore());
            }
        })).sheet().doRead();
 }

3、easyexcel写excel
 

public void testWrite() {
    String fileName = "D://output.xls";
    EasyExcel.write(fileName, ArticleScoreData.class)
                .sheet("文章得分表")
                .doWrite(() -> data());
}

private List<ArticleScoreData> data() {
    List<ArticleScoreData> list = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        ArticleScoreData data = new ArticleScoreData();
        data.setName("姓名" + i);
        data.setTitle("文章" + i);
        data.setScore(0.56);
        list.add(data);
     }
     return list;
 }

 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Spring BootEasyExcel可以很好地结合使用,来实现Excel文件的读写操作。下面是一个简单的示例代码,演示如何在Spring Boot项目中使用EasyExcel进行Excel文件的读写: 1. 首先,在pom.xml文件中添加EasyExcel的依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> </dependency> ``` 2. 创建一个Excel读写的服务类,例如ExcelService: ```java import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Table; import org.springframework.stereotype.Service; import java.util.List; @Service public class ExcelService { // 读取Excel文件 public List<User> readExcel(String filePath) { return EasyExcel.read(filePath).head(User.class).sheet().doReadSync(); } // 写入Excel文件 public void writeExcel(String filePath, List<User> userList) { ExcelWriter excelWriter = EasyExcel.write(filePath).build(); Sheet sheet = new Sheet(1, 0, User.class); excelWriter.write(userList, sheet); excelWriter.finish(); } } ``` 3. 创建一个User实体类,对应Excel中的每一行数据: ```java public class User { private String name; private Integer age; // Getters and setters... } ``` 4. 在控制器中注入ExcelService,并通过调用其方法来实现Excel读写操作。 ```java @RestController public class ExcelController { @Autowired private ExcelService excelService; // 读取Excel文件 @GetMapping("/readExcel") public List<User> readExcel(@RequestParam String filePath) { return excelService.readExcel(filePath); } // 写入Excel文件 @PostMapping("/writeExcel") public void writeExcel(@RequestParam String filePath, @RequestBody List<User> userList) { excelService.writeExcel(filePath, userList); } } ``` 这样就可以通过访问"/readExcel"接口来读取Excel文件,通过访问"/writeExcel"接口来写入Excel文件。 以上是一个简单的示例,你可以根据实际需求进行相应的扩展和优化。希望对你有所帮助!如果有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

良枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值