废话不多说,直接上
Easy Poi说明
不论导入还是导出Excel,一个Excel就代表一个对象的集合,即Excel的一行就是一个对象,Excel的一列就是对象的一个属性。
@ExcelTarget(value = “”)就标识该类要导出为Excel,或者导入Excel的接收对象。value值其标识作用,不重复即可,随便取。
@Excel(name = “”)就是作用于类属性上,表示该属性对应Excel的哪一列,即name的值,当然还有其它属性,后面再说。
@ExcelEntity 表示一对一的关系,比如下文的User和Dog的关系
@ExcelCollection(name = “”) 表示一对多关系,比如下文的User和Order的关系
@ExcelIgnore 忽略该字段,不和Excel列映射
需要特别注意的是:
所涉及到的类必须序列化,类中的属性必须要有Getter()方法
1.导出Excel
1.准备工作
1依赖:
mybatis
mysql
lombok
web
外加easypoi的三个依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--easy poi依赖-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
2.数据库建表(User、Dog、Order)
t_user表:
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`birth` datetime DEFAULT NULL,
`photo` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
t_dog表:
DROP TABLE IF EXISTS `t_dog`;
CREATE TABLE `t_dog` (
`name` varchar(255) DEFAULT NULL,
`age` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
t_order表:
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order` (
`oid` int(11) NOT NULL AUTO_INCREMENT,
`money` decimal(4,2) DEFAULT NULL,
`userid` int(11) DEFAULT NULL,
PRIMARY KEY (`oid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
3.创建类:
User:
@Data
@Component
@ExcelTarget(value = "user")
public class User implements Serializable {
@Excel(name = "编号", orderNum = "1")
private Integer id;
@Excel(name = "姓名", orderNum = "2")
private String name;
@Excel(name = "生日", format = "yyyy-MM-dd HH:mm:ss", orderNum = "3", width = 25)
private Date birth;
@Excel(name = "头像",type = 2, width = 10, height = 30, orderNum = "4")
private String photo;
@ExcelEntity
private Dog dog;
@ExcelCollection(name = "订单", orderNum = "7")
private List<Order> orders;
}
Dog:
@Data
@Component
@ExcelTarget(value = "dog")
public class Dog implements Serializable {
@Excel(name = "狗的名字", orderNum = "5")
private String dogName;
@Excel(name = "狗的年龄", orderNum = "6")
private Integer age;
}
Order:
@Data
@Component
@ExcelTarget(value = "order")
public class Order implements Serializable {
@Excel(name = "订单号")
private Integer oid;
@Excel(name = "消费", suffix = "元")
private Double money;
}
4.配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.xp.easypoi.entity
就数据库和mybatis的配置
mapper的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xp.easypoi.mapper.TestMapper">
<select id="exportUser" resultType="User" resultMap="userMap">
select
u.*,
d.name as dName,
d.age,
o.oid,
o.money
from
t_user u,
t_dog d,
t_order o
where
u.id=d.user_id
and
u.id=o.userid
</select>
<resultMap id="userMap" type="User">
<id property="id" column="id"/>
<id property="name" column="name"/>
<id property="birth" column="birth"/>
<id property="photo" column="photo"/>
<association property="dog" javaType="Dog">
<id property="dogName" column="dName"/>
<id property="age" column="age"/>
</association>
<collection property="orders" ofType="Order">
<id property="oid" column="oid"/>
<id property="money" column="money"/>
</collection>
</resultMap>
</mapper>
5.创建接口
crud就不用说了吧,mybatis
这是接口:
@RestController
@CrossOrigin
public class TestController {
@Autowired
private TestMapper mapper;
@GetMapping("/test")
public void test(){
//查询到所有用户
List<User> users = mapper.exportUser();
//生成Excel
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表导出Excel", "1"), User.class, users);
try {
//输出到哪里
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\29443\\Desktop\\test.xls");
//导出完成
workbook.write(fileOutputStream);
fileOutputStream.close();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.调用接口效果:
2.导出Excel解析
我们主要看的是User类,因为导出Excel的就是它:
漏了一个比较实用的,replcae:数据库中状态码是1,替换成正常
大家可以比对上面的效果图
同样,Dog、Order类也要这样写:
需要提一嘴的是图片的问题:
type=2代表是图片类型,
此外还有一个属性叫imageType,其默认值为1,代表图片是String类型的路径(即数据库中存的是图片地址,几乎都是这样的),2代表类中的图片属性是字节数组(即数据库存储的是图片)
因为我数据库存的图片地址,就默认1,没有写imageType了
3.导入Excel
导入很简单,和导出差不多:
1.导入对应的User类:
@Data
@Component
@ExcelTarget(value = "user")
public class User implements Serializable {
@Excel(name = "编号")
private Integer id;
@Excel(name = "姓名")
private String name;
@Excel(name = "生日", format = "yyyy-MM-dd HH:mm:ss")
private String birth;
@Excel(name = "头像",type = 2, savePath = "D:\\javaprojects\\easypoi\\src\\main\\resources\\imgs")
private String photo;
@Excel(name = "状态", replace = {"正常_1","禁用_2"})
private Integer status;
@ExcelIgnore
private Dog dog;
@ExcelIgnore
private List<Order> orders;
}
2.测试接口:
@GetMapping("/import")
public void importExcel(){
try {
//读取本地excel
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\29443\\Desktop\\test1.xls");
//创建导入参数对象,作用是导入的规约
ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
params.setStartSheetIndex(0);
params.setSheetNum(2);
params.setImportFields(new String[]{"编号"});
//导入结果
List<User> users = ExcelImportUtil.importExcel(fileInputStream, User.class, params);
for (User user : users) {
System.out.println(user);
}
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
注意:每个sheet都必须要一致,比如都有标题、都有表头
3.效果
4.模板和校验就等用到了再更新吧
|