最近遇到了这么一个功能,要实现Excel文件的导入导出功能,下面直接上代码好了:
项目目录图:

导入导出主要使用了这三个pom依赖:
<!-- 导入导出 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
yml配置
#配置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&
username: root
password: root
main:
allow-bean-definition-overriding: on
#指定mybatis映射文件的地址
mybatis:
mapper-locations: classpath:mapper/*.xml
UserController 代码:
package easypoi.controller;
import easypoi.entity.User;
import easypoi.service.UserService;
import easypoi.utils.ExcelUtils;
import org.springframework.beans.factory.annotation

本文介绍了如何使用Java的POI库来实现Excel的导入和导出功能。通过添加相关依赖,配置YML文件,编写UserController、User实体类、UserService以及导入导出工具类,实现了在Postman中测试导入功能和浏览器中测试导出功能。在导入时需要注意使用POST方法并处理相应细节。
最低0.47元/天 解锁文章
3352





