当前位置:   article > 正文

easyexcel的导出_easyexcel导出嵌套对象

easyexcel导出嵌套对象

原文链接:easyexcel的导出 – 编程屋

目录

1 前言

2 导出实战


1 前言

        之前用easypoi的导出用到了表头多层嵌套时,发现很麻烦,需要些很多的代码,所以今天看了一下easyexcel的导出,发现表头情况不复杂时和easypoi的难易程度是一样的,但是涉及到多层嵌套时,easyexcel就非常简单了。

2 导出实战

相关依赖:

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>easyexcel</artifactId>
  4. <version>2.2.2</version>
  5. </dependency>

导出类:

  1. @RestController
  2. @RequestMapping("/test/")
  3. public class TestEasyExcel {
  4. @GetMapping("/export")
  5. public void export( HttpServletResponse response) throws IOException {
  6. List<EasyExcelVo> list = new ArrayList<>();
  7. EasyExcelVo easyExcelVo = new EasyExcelVo();
  8. easyExcelVo.setName("文件夹");
  9. easyExcelVo.setResourceId("123");
  10. easyExcelVo.setResourceName("第一手资源");
  11. easyExcelVo.setAngle("正面");
  12. easyExcelVo.setBigArmLeft("fad");
  13. easyExcelVo.setBigArmRight("sgf");
  14. easyExcelVo.setSmallArmLeft("gsz");
  15. easyExcelVo.setSmallArmRight("gsz");
  16. easyExcelVo.setBodyRollDirection("向左转");
  17. easyExcelVo.setTurnHead("不转头");
  18. list.add(easyExcelVo);
  19. //解决中文乱码问题
  20. String fileName = new String("导出标记".getBytes(), "ISO-8859-1");
  21. response.setContentType("application/vnd.ms-excel");
  22. response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
  23. writeExcel(response,list);
  24. }
  25. public static void writeExcel(HttpServletResponse response, List<EasyExcelVo> list) throws IOException {
  26. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
  27. //定义工具表对象
  28. WriteSheet sheet = EasyExcel.writerSheet(0, "导出标记").head(EasyExcelVo.class).build();
  29. //往excel文件中写入数据
  30. excelWriter.write(list, sheet);
  31. excelWriter.finish();
  32. }
  33. }
EasyExcelVo:
  1. @Data
  2. public class EasyExcelVo {
  3. @ExcelIgnore
  4. private String id;
  5. @ExcelProperty(value = {"文件夹名称","文件夹名称","文件夹名称"},index = 0)
  6. @HeadStyle(fillBackgroundColor = 10) //设置表头颜色
  7. private String name;
  8. @ExcelProperty(value = {"资源id","资源id","资源id"},index = 1)
  9. private String resourceId;
  10. @ExcelProperty(value = {"资源名称","资源名称","资源名称"},index = 2)
  11. private String resourceName;
  12. @ExcelProperty(value = {"人工标注","拍摄角度","拍摄角度"},index = 3)
  13. private String angle;
  14. @ExcelProperty(value = {"人工标注","大臂抬起","左"},index = 4)
  15. private String bigArmLeft;
  16. @ExcelProperty(value = {"人工标注","大臂抬起","右"},index = 5)
  17. private String bigArmRight;
  18. @ExcelProperty(value = {"人工标注","小臂抬起","左"},index = 6)
  19. private String smallArmLeft;
  20. @ExcelProperty(value = {"人工标注","小臂抬起","右"},index = 7)
  21. private String smallArmRight;
  22. @ExcelProperty(value = {"人工标注","身体侧倾","身体侧倾"},index = 8)
  23. private String bodyRollDirection;
  24. @ExcelProperty(value = {"人工标注","身体侧倾","身体侧倾"},index = 9)
  25. private String turnHead;
  26. }

导出结果:

以上只是部分内容,为了维护方便,本文已迁移到新地址:easyexcel的导出 – 编程屋

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/170305
推荐阅读
  

闽ICP备14008679号