(个人笔记,欢迎指导)
最近学习导出excel文件,一开始接触到的是POI,感觉用起来很繁琐,这个包里面提供了很多的类,样式和数据填充操作起来麻烦,代码量太大了,直接劝退了我。
相反用easyExcel的话,可以在实体类上加上注解来完成映射,使用起来很友好
这是它内存上的区别
借鉴学习了这位大佬的博客(推荐):https://www.jianshu.com/p/d1d264c817ef
easyExcel的官网:https://easyexcel.opensource.alibaba.com/
1.POI的方式
导入pom依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>
简单的写操作
@RequestMapping(value = "/down", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResultMsg getExcel(HttpServletResponse response){
SXSSFWorkbook workbook = new SXSSFWorkbook();
//设置表格名称
SXSSFSheet sheet = workbook.createSheet("销售榜单");
//设置表格默认宽度30
sheet.setDefaultColumnWidth(30);
List<SXSSFRow> rowList = new ArrayList<>();
//设置 sheet 表格的第0行
SXSSFRow row = sheet.createRow(0);
//设置 sheet 表格的第0行第0列的值
row.createCell(0).setCellValue("编码");
//设置 sheet 表格的第0行第1列的值
row.createCell(1).setCellValue("资源主键");
row.createCell(2).setCellValue("名称");
row.createCell(3).setCellValue("相对路径");
row.createCell(4).setCellValue("后缀名");
row.createCell(5).setCellValue("创建用户主键");
row.createCell(6).setCellValue("创建时间");
row.createCell(7).setCellValue("创建部门");
rowList.add(row);
//从数据库中获取数据
List<Resoucefile> list = resoucefileService.list();
//遍历赋值
int i = 1;
for (Resoucefile resoucefile : list) {
SXSSFRow rowtemp = sheet.createRow(i++);
//设置 sheet 表格的第0行第0列的值
rowtemp.createCell(0).setCellValue(resoucefile.getFileid());
//设置 sheet 表格的第0行第1列的值
rowtemp.createCell(1).setCellValue(resoucefile.getResourceid());
rowtemp.createCell(2).setCellValue(resoucefile.getFilename());
rowtemp.createCell(3).setCellValue(resoucefile.getFilepath());
rowtemp.createCell(4).setCellValue(resoucefile.getFileext());
rowtemp.createCell(5).setCellValue(resoucefile.getCreateuserid());
rowtemp.createCell(6).setCellValue(resoucefile.getCreatetime());
rowtemp.createCell(7).setCellValue(resoucefile.getCreatedep());
rowList.add(rowtemp);
}
//将 Excel 表输出到输出流
OutputStream out = null;
try {
//设置响应头
//filename 为 Excel 文件名称
String fileName = "excel文件";
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
out = response1.getOutputStream();
workbook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
workbook.dispose();
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
得到
2.使用easyExcel的方式
导入pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
实体类
public class Resoucefile implements Serializable {
@ExcelProperty({"资源附件", "编码"})
@ColumnWidth(40)
private String fileid;
@ExcelProperty({"资源附件", "资源主键"})
@ColumnWidth(40)
private String resourceid;
@ExcelProperty({"资源附件", "名称"})
@ColumnWidth(20)
private String filename;
@ExcelProperty({"资源附件", "相对路径"})
@ColumnWidth(50)
private String filepath;
@ExcelProperty({"资源附件", "后缀名"})
private String fileext;
@ExcelProperty({"资源附件", "创建用户主键"})
@ColumnWidth(40)
private String createuserid;
@ExcelProperty({"资源附件", "创建时间"})
private Date createtime;
@ExcelProperty({"资源附件", "创建部门"})
private String createdep;
}
简单的写操作
@GetMapping("writeExcel")
public void writeExcel(HttpServletResponse response) throws IOException {
HttpServletResponse response1 = setEncoding(response);
//从数据库查询数据
List<Resoucefile> list = resoucefileService.list();
//使用工具类来创建Excel表
EasyExcel.write(response1.getOutputStream(), Resoucefile.class).sheet("模板").doWrite(list);
}
public HttpServletResponse setEncoding(HttpServletResponse response) throws UnsupportedEncodingException {
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");
return response;
}
得到Excel