需要的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.0</version>
</dependency>
用到的注解
@ExcelProperty(“职位编号”) 对字段进行注解,即导出的excel的目录一栏
@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”, timezone = “Asia/Shanghai”) 日期格式注解
@TableId(value = “id”, type = IdType.AUTO) 标注表的主键
关键
Serializable 开启流的接口
private static final long serialVersionUID = 1L;相关的配置
package com.huang.vhr.framework.web.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
*
*
*
*
* @author 张三
* @since 2022-05-17
*/
public class Position implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
@ExcelProperty("职位编号")
private Integer id;
/**
* 职位
*/
@ExcelProperty("职位名称")
private String name;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
@ExcelProperty("创建时间")
private LocalDateTime createDate;
@ExcelProperty("是否启用")
private Boolean enabled;
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 LocalDateTime getCreateDate() {
return createDate;
}
public void setCreateDate(LocalDateTime createDate) {
this.createDate = createDate;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
@Override
public String toString() {
return "Position{" +
"id=" + id +
", name=" + name +
", createDate=" + createDate +
", enabled=" + enabled +
"}";
}
}
@GetMapping("/export")
public void download(HttpServletResponse response) throws IOException {
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("职位数据", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), Position.class).sheet("职位信息").doWrite(positionService.getAllpp());
}
service
List<Position> getAllpp();
serviceImpl
@Override
public List<Position> getAllpp() {
List<Position> positions = positionMapper.selectPageInfo();
return positions;
}
其实没有什么不同,只是controller的实现分页方法中的sql查询用了mybatis-plus的写法而已
package com.huang.vhr.framework.web.entity;
import java.io.Serializable;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@NoArgsConstructor
public class Employeeremove implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId(value = "id", type = IdType.AUTO)
@ExcelProperty("调动编号")
private Integer id;
/**
*
*/
@ExcelProperty("调动与主表关联")
private Integer eid;
/**
* 调动后部门
*/
@ExcelProperty("调动后部门")
private Integer afterDepId;
/**
* 调动后职位
*/
@ExcelProperty("调动后职位")
private Integer afterJobId;
/**
* 调动日期
*/
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
@ExcelProperty("调动日期")
private Date removeDate;
/**
* 调动原因
*/
@ExcelProperty("调动原因")
private String reason;
/**
*
*/
@ExcelProperty("调动评语")
private String remark;
}
@GetMapping("/export")
public void download(HttpServletResponse response) throws IOException {
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("职位数据", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), Employeeremove.class).sheet("职位信息").doWrite(employeeremoveService.list());
}
service层
service
{
public interface EmployeeremoveService extends IService<Employeeremove> {
}
serviceImpl
@Service
public class EmployeeremoveServiceImpl extends ServiceImpl<EmployeeremoveMapper, Employeeremove> implements EmployeeremoveService {
}
注意:
1.postman这种测试工具测试excel下载是可以成功的,但是没办法进行下载
2.想要下载拿到文件的话你要通过浏览器
3.excel下载的时候一定会涉及到权限的问题,怎么处理这个权限要考虑好,否则会报错
例如黄黄这里是把动态权限关掉了,然后配了一个放行。
下载成功excel浏览图