Spring Boot+poi实现excel文件上传、下载
最近为了实现excel文件上传与下载找了很多的代码,可总有一些写的不是很完整而且用的方法也特别多(感受到了代码的强大),所以我决定综合资料自己做出来后也保存一份下来(亲测有效?)
1.poi介绍
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
开始整合spring boot+poi
1.第一步还是照旧添加poi的依赖
<!-- 文件上传与下载 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>
2.编写实体类
package com.test.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* <p>
*/
@Data
@Accessors(chain = true)
public class User {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private Date date;
private Boolean type;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getType() {
return type;
}
public void setType(Boolean type) {
this.type = type;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDate() {
return date;
}
}
/***
* 存储错误数据实体类
*/
@Data
public class ErrorUserDto {
/**
* 参数错误信息
*/
private String message;
/**
* 参数所在表格行数
*/
private Integer lineId;
}
3. 编写上传需要用的工具类
package com.test.demo.support;
import com.test.demo.constant.ExcelUtilConstant;
import lombok.extern.slf4j.Slf4j