原文链接:easyexcel的导出 – 编程屋
目录
1 前言
之前用easypoi的导出用到了表头多层嵌套时,发现很麻烦,需要些很多的代码,所以今天看了一下easyexcel的导出,发现表头情况不复杂时和easypoi的难易程度是一样的,但是涉及到多层嵌套时,easyexcel就非常简单了。
2 导出实战
相关依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.2</version>
</dependency>
导出类:
@RestController
@RequestMapping("/test/")
public class TestEasyExcel {
@GetMapping("/export")
public void export( HttpServletResponse response) throws IOException {
List<EasyExcelVo> list = new ArrayList<>();
EasyExcelVo easyExcelVo = new EasyExcelVo();
easyExcelVo.setName("文件夹");
easyExcelVo.setResourceId("123");
easyExcelVo.setResourceName("第一手资源");
easyExcelVo.setAngle("正面");
easyExcelVo.setBigArmLeft("fad");
easyExcelVo.setBigArmRight("sgf");
easyExcelVo.setSmallArmLeft("gsz");
easyExcelVo.setSmallArmRight("gsz");
easyExcelVo.setBodyRollDirection("向左转");
easyExcelVo.setTurnHead("不转头");
list.add(easyExcelVo);
//解决中文乱码问题
String fileName = new String("导出标记".getBytes(), "ISO-8859-1");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
writeExcel(response,list);
}
public static void writeExcel(HttpServletResponse response, List<EasyExcelVo> list) throws IOException {
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
//定义工具表对象
WriteSheet sheet = EasyExcel.writerSheet(0, "导出标记").head(EasyExcelVo.class).build();
//往excel文件中写入数据
excelWriter.write(list, sheet);
excelWriter.finish();
}
}
EasyExcelVo:
@Data
public class EasyExcelVo {
@ExcelIgnore
private String id;
@ExcelProperty(value = {"文件夹名称","文件夹名称","文件夹名称"},index = 0)
@HeadStyle(fillBackgroundColor = 10) //设置表头颜色
private String name;
@ExcelProperty(value = {"资源id","资源id","资源id"},index = 1)
private String resourceId;
@ExcelProperty(value = {"资源名称","资源名称","资源名称"},index = 2)
private String resourceName;
@ExcelProperty(value = {"人工标注","拍摄角度","拍摄角度"},index = 3)
private String angle;
@ExcelProperty(value = {"人工标注","大臂抬起","左"},index = 4)
private String bigArmLeft;
@ExcelProperty(value = {"人工标注","大臂抬起","右"},index = 5)
private String bigArmRight;
@ExcelProperty(value = {"人工标注","小臂抬起","左"},index = 6)
private String smallArmLeft;
@ExcelProperty(value = {"人工标注","小臂抬起","右"},index = 7)
private String smallArmRight;
@ExcelProperty(value = {"人工标注","身体侧倾","身体侧倾"},index = 8)
private String bodyRollDirection;
@ExcelProperty(value = {"人工标注","身体侧倾","身体侧倾"},index = 9)
private String turnHead;
}
导出结果:
以上只是部分内容,为了维护方便,本文已迁移到新地址:easyexcel的导出 – 编程屋