• EasyExcel使用方式(包含导出图片)


    1、导EasyExcel依赖

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>easyexcelartifactId>
    4. <version>3.3.2version>
    5. dependency>

    2、创建导出excel的实体类

    1. @Getter
    2. @Setter
    3. @EqualsAndHashCode
    4. @HeadStyle(fillForegroundColor = 9)
    5. @HeadFontStyle(fontHeightInPoints = 12, bold = BooleanEnum.TRUE) //头字体 加粗
    6. @HeadRowHeight(31)
    7. @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER,borderTop = BorderStyleEnum.THIN,borderBottom = BorderStyleEnum.THIN,borderLeft = BorderStyleEnum.THIN,borderRight = BorderStyleEnum.THIN) //水平居中 垂直居中 上 下 左 右(边框线)
    8. @ContentFontStyle(fontHeightInPoints = 11) //内容字体
    9. @ContentRowHeight(123)
    10. public class ExcelDto {
    11. @ExcelProperty(value = "序号", index = 0) //意思为序号这一列为第0列(从0开始算)
    12. @ColumnWidth(14)
    13. private String number;
    14. @ExcelProperty(value = "姓名", index = 1)
    15. @ColumnWidth(18)
    16. private String name;
    17. @ExcelProperty(value = "时间", index = 2)
    18. @ColumnWidth(35)
    19. private Date time;
    20. @ColumnWidth(54)
    21. @ExcelProperty(value = "备注", index = 3)
    22. private String text;
    23. @ExcelProperty(value = "图片", index = 4)
    24. @ContentStyle(shrinkToFit = BooleanEnum.TRUE,borderTop = BorderStyleEnum.THIN,borderBottom = BorderStyleEnum.THIN,borderLeft = BorderStyleEnum.THIN,borderRight = BorderStyleEnum.THIN) //上下左右(边框线)
    25. @ColumnWidth(43)
    26. private File image; //file类型
    27. }

    3、代码进行逻辑赋值

    controller

    1. /**
    2. * 导出excel
    3. */
    4. @GetMapping(value = "/exportExcel")
    5. public void exportExcel(HttpServletResponse response, HttpServletRequest request,T entity) {
    6. jsjtProblemLabelService.exportExcel(response, request,entity);
    7. }

    service

    1. /**
    2. * 导出
    3. * @param response
    4. * @param request
    5. */
    6. public void exportExcel(HttpServletResponse response, HttpServletRequest request,T entity) {
    7. try {
    8. //创建导出excel的集合
    9. List list = ListUtils.newArrayList();
    10. //假设excelListBySQL 为数据库中查询数据的集合
    11. List excelListBySQL = new ArrayList();
    12. //处理逻辑代码取出符合条件的值返回进excelListBySQL中
    13. //循环赋值
    14. for (int i = 0; i < excelListBySQL.size(); i++) {
    15. //取出每一个从数据库中取出的对象
    16. Excel excel = excelListBySQL.get(i);
    17. //返回进导出excel的类中
    18. ExcelDto excelDto = new ExcelDto();
    19. //赋值序号
    20. excelDto.setNumber(String.valueOf(++number));
    21. //赋值姓名
    22. excelDto.setName(excel .getName());
    23. //赋值时间
    24. excelDto.setTime(excel .getTime());
    25. //赋值备注
    26. excelDto.setText(excel .getText());
    27. //赋值图片(数据库路径为相对路径)
    28. excelDto.setImage(imageByUrl(excel .getImageUrl()));
    29. //将每一行对象添加进导出excel的集合中
    30. list.add(excelDto);
    31. }
    32. String fileName = URLEncoder.encode("EasyExcel导出.xlsx", "UTF-8");
    33. OutputStream fileOutputStream = null;
    34. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    35. response.setContentType("application/x-download");
    36. response.setCharacterEncoding("UTF-8");
    37. response.addHeader("Pargam", "no-cache");
    38. response.addHeader("Cache-Control", "no-cache");
    39. response.flushBuffer();
    40. fileOutputStream = response.getOutputStream();
    41. ExcelWriter excelWriter = EasyExcel.write(fileOutputStream, ExcelDto.class)
    42. .build();
    43. WriteSheet sheet = EasyExcel.writerSheet(0, "sheet1").build(); //控制sheet页
    44. excelWriter.write(list, sheet);
    45. excelWriter.finish();
    46. fileOutputStream.flush();
    47. fileOutputStream.close();
    48. } catch (Exception e) {
    49. e.getMessage();
    50. }
    51. }
    52. /**
    53. *将string类型的url转换为文件
    54. */
    55. public File imageByUrl(String imageUrl) throws Exception{
    56. if (StringUtils.isBlank(imageUrl)){
    57. return null;
    58. }
    59. //指定读取的图片文件
    60. URI uri = new URI("file:///" + path+"/"+imageUrl);
    61. File file = new File(uri);
    62. //存在返回file否则返回null
    63. return file.exists() ? file : null;
    64. }

  • 相关阅读:
    Linux防火墙设置开机自启/永久开机不启动
    AndroidStudio 新建工程的基本修改及事件添加
    微信小程序项目实例SSM农产品销售系统|商城|电商系统+后台
    企业架构LNMP学习笔记23
    POJ 2836 Rectangular Covering 状态压缩DP(铺砖问题)
    QObject::connect: signal not found in QPushButton
    Spring框架的两大核心IOC&DI
    正则表达式
    JavaScript 中基本数据类型和引用数据类型是如何存储的
    2022~2023计算机毕业设计选题篇
  • 原文地址:https://blog.csdn.net/weixin_56777219/article/details/134004314