com.alibaba.easyexcel导出EXCEL文件
1.POM.XML
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
2.创建导出实体
@Data
public class XXXX{
//表头文字
@ExcelProperty("主键ID")
private Integer id;
@ExcelProperty("用户ID")
private String userId;
@ExcelProperty("姓名")
private String userName;
3.接口
@RequestMapping("/toExcel.do")
public void toExcel(@RequestBody XXX param,HttpServletRequest request, HttpServletResponse response)throws Exception{
//定义文件名称
String sheetName = "";
//获取想要导出的数据
List<XXXX> resultList = toExcel(param, response);
//对文件名进行固定格式编码
String fileName = URLEncoder.encode(sheetName, "UTF-8");
//设置请求响应内容类型
//作用:使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//设置请求响应内容编码方式
response.setCharacterEncoding("utf-8");
//文件下载,指定默认名
response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
//写入
EasyExcel.write(response.getOutputStream(), XXX.class) .sheet(sheetName) .doWrite(resultList);
}
参考官方文档:https://alibaba-easyexcel.github.io/index.html