EasyExcel 设置字体样式(字体、字体大小、字体颜色、字体加粗、字体斜体、字体下划线、字体上标下标、字体删除线)

Dear 丶 2022-10-16 09:55 378阅读 0赞

1 Maven配置

  1. <!--hutool工具包-->
  2. <dependency>
  3. <groupId>cn.hutool</groupId>
  4. <artifactId>hutool-all</artifactId>
  5. <version>5.5.1</version>
  6. </dependency>
  7. <!-- EasyExcel文档处理工具 -->
  8. <dependency>
  9. <groupId>com.alibaba</groupId>
  10. <artifactId>easyexcel</artifactId>
  11. <version>2.2.8</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.projectlombok</groupId>
  15. <artifactId>lombok</artifactId>
  16. <optional>true</optional>
  17. </dependency>

2 调试代码

  1. /**
  2. * 导出(设置字体样式,支持设置字体、字体大小、字体颜色、字体加粗、字体斜体、字体下划线、字体上标下标、字体删除线)
  3. *
  4. * @param response
  5. */
  6. @GetMapping("/exportFontStyle")
  7. public void exportFontStyle(HttpServletResponse response) {
  8. try {
  9. //生成表格数据
  10. List<List<Object>> dataList = new ArrayList<>();
  11. dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头11", "表头2", "表头3", "表头4"})));
  12. dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头1", "表头2", "表头3", "表头4"})));
  13. dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头31", "表头2", "表头3", "表头4"})));
  14. //导出文件
  15. String fileName = new String("文件名称.xlsx".getBytes(), "UTF-8");
  16. List<CellStyleModel> cellStyleList = new ArrayList<>();
  17. //第一种一次性设置字体样式
  18. cellStyleList.add(CellStyleModel.createFontCellStyleModel("模板", 2, 2, "微软雅黑", 20D, IndexedColors.BLUE
  19. , true, true, Font.U_SINGLE, Font.SS_NONE, true));
  20. //第二种分别设置字体样式
  21. //设置单元格字体(黑体)
  22. cellStyleList.add(CellStyleModel.createFontNameCellStyleModel("模板", 0, 0, "黑体"));
  23. //设置单元格大小 18号
  24. cellStyleList.add(CellStyleModel.createFontHeightCellStyleModel("模板", 0, 1, 18D));
  25. //设置单元格字体颜色
  26. cellStyleList.add(CellStyleModel.createFontColorCellStyleModel("模板", 0, 2, IndexedColors.RED));
  27. //设置单元格字体加粗
  28. cellStyleList.add(CellStyleModel.createFontBoldCellStyleModel("模板", 0, 3, true));
  29. //设置单元格字体斜体
  30. cellStyleList.add(CellStyleModel.createFontItalicCellStyleModel("模板", 1, 0, true));
  31. //设置单元格字体单下划线
  32. cellStyleList.add(CellStyleModel.createFontUnderLineCellStyleModel("模板", 1, 1, Font.U_SINGLE));
  33. //设置单元格字体双下划线
  34. cellStyleList.add(CellStyleModel.createFontUnderLineCellStyleModel("模板", 1, 2, Font.U_DOUBLE));
  35. //设置单元格字体上标
  36. cellStyleList.add(CellStyleModel.createFontTypeOffsetCellStyleModel("模板", 1, 3, Font.SS_SUPER));
  37. //设置单元格字体下标
  38. cellStyleList.add(CellStyleModel.createFontTypeOffsetCellStyleModel("模板", 2, 0, Font.SS_SUB));
  39. //设置单元格字体删除线
  40. cellStyleList.add(CellStyleModel.createFontStrikeoutCellStyleModel("模板", 2, 1, true));
  41. response.addHeader("Content-Disposition", "filename=" + fileName);
  42. //设置类型,扩展名为.xls
  43. response.setContentType("application/vnd.ms-excel");
  44. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new CustomCellStyleHandler(cellStyleList)).build();
  45. WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
  46. excelWriter.write(dataList, writeSheet);
  47. //千万别忘记finish 会帮忙关闭流
  48. excelWriter.finish();
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }

3 调试结果

20210529172758381.png

注:

(1)有关CellStyleModel和CustomCellStyleHandler的源码请查看以下博客。

EasyExcel 批量设置单元格样式(字体样式、背景颜色、边框样式、对齐方式、自动换行)

(2)有关自定义RGB颜色的使用请查看以下博客。

EasyExcel 单元格背景颜色、字体颜色使用2种设置颜色方法(IndexedColors中定义的颜色,自定义RGB颜色)实现

发表评论

表情:
评论列表 (有 0 条评论,378人围观)

还没有评论,来说两句吧...

相关阅读