EasyExcel 设置行高列宽、隐藏行和列

约定不等于承诺〃 2022-09-07 09:22 929阅读 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>

2 CustomRowHeightColWidthHandler

自定义行高列宽处理器。

  1. package com.easyexcel;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
  5. import com.alibaba.excel.write.metadata.holder.*;
  6. import org.apache.poi.ss.usermodel.*;
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9. /**
  10. * 自定义行高列宽处理器
  11. */
  12. public class CustomRowHeightColWidthHandler extends AbstractRowWriteHandler {
  13. /**
  14. * sheet名称KEY
  15. */
  16. public static final String KEY_SHEET_NAME = "sheetName";
  17. /**
  18. * 行号key
  19. */
  20. public static final String KEY_ROW_INDEX = "rowIndex";
  21. /**
  22. * 列号key
  23. */
  24. public static final String KEY_COL_INDEX = "colIndex";
  25. /**
  26. * 行高key
  27. */
  28. public static final String KEY_ROW_HEIGHT = "rowHeight";
  29. /**
  30. * 列宽key
  31. */
  32. public static final String KEY_COL_WIDTH = "colWidth";
  33. /**
  34. * sheet页名称列表
  35. */
  36. private List<String> sheetNameList;
  37. /**
  38. * 列宽信息
  39. */
  40. private List<Map<String, Object>> colWidthList = new ArrayList<>();
  41. /**
  42. * 行高信息
  43. */
  44. private List<Map<String, Object>> rowHeightList = new ArrayList<>();
  45. /**
  46. * 创建行高信息
  47. *
  48. * @param sheetName sheet页名称
  49. * @param rowIndex 行号
  50. * @param rowHeight 行高
  51. * @return
  52. */
  53. public static Map<String, Object> createRowHeightMap(String sheetName, Integer rowIndex, Float rowHeight) {
  54. return createRowHeightColWidthMap(sheetName, rowIndex, rowHeight, null, null);
  55. }
  56. /**
  57. * 创建列宽信息
  58. *
  59. * @param sheetName sheet页名称
  60. * @param colIndex 列号
  61. * @param colWidth 列宽
  62. * @return
  63. */
  64. public static Map<String, Object> createColWidthMap(String sheetName, Integer colIndex, Integer colWidth) {
  65. return createRowHeightColWidthMap(sheetName, null, null, colIndex, colWidth);
  66. }
  67. /**
  68. * 创建行高列宽信息
  69. *
  70. * @param sheetName sheet页名称
  71. * @param rowIndex 行号
  72. * @param rowHeight 行高
  73. * @param colIndex 列号
  74. * @param colWidth 列宽
  75. * @return
  76. */
  77. public static Map<String, Object> createRowHeightColWidthMap(String sheetName, Integer rowIndex, Float rowHeight, Integer colIndex, Integer colWidth) {
  78. Map<String, Object> map = new HashMap<>();
  79. //sheet页名称
  80. map.put(KEY_SHEET_NAME, sheetName);
  81. //显示行号
  82. map.put(KEY_ROW_INDEX, rowIndex);
  83. //行高
  84. map.put(KEY_ROW_HEIGHT, rowHeight);
  85. //显示列号
  86. map.put(KEY_COL_INDEX, colIndex);
  87. //列宽
  88. map.put(KEY_COL_WIDTH, colWidth);
  89. return map;
  90. }
  91. /**
  92. * 自定义行高列宽适配器构造方法
  93. *
  94. * @param rowHeightColWidthList 行高列宽信息
  95. */
  96. public CustomRowHeightColWidthHandler(List<Map<String, Object>> rowHeightColWidthList) {
  97. if (rowHeightColWidthList == null || rowHeightColWidthList.size() <= 0) {
  98. return;
  99. }
  100. rowHeightColWidthList = rowHeightColWidthList.stream().filter(x ->
  101. //判断sheet名称KEY是否存在
  102. x.keySet().contains(KEY_SHEET_NAME) && x.get(KEY_SHEET_NAME) != null
  103. && StrUtil.isNotBlank(x.get(KEY_SHEET_NAME).toString())
  104. //判断列索引KEY是否存在
  105. && x.keySet().contains(KEY_COL_INDEX)
  106. //判断行索引KEY是否存在
  107. && x.keySet().contains(KEY_ROW_INDEX)
  108. //判断行高KEY是否存在
  109. && x.keySet().contains(KEY_ROW_HEIGHT)
  110. //判断列宽KEY是否存在
  111. && x.keySet().contains(KEY_COL_WIDTH)).collect(Collectors.toList());
  112. //填充行高信息
  113. this.rowHeightList = rowHeightColWidthList.stream().filter(x ->
  114. x.get(KEY_ROW_INDEX) != null && x.get(KEY_ROW_HEIGHT) != null).collect(Collectors.toList());
  115. //填充列宽信息
  116. this.colWidthList = rowHeightColWidthList.stream().filter(x ->
  117. x.get(KEY_COL_INDEX) != null && x.get(KEY_COL_WIDTH) != null).collect(Collectors.toList());
  118. //获取sheet页名称
  119. sheetNameList = this.rowHeightList.stream().map(x -> x.get(KEY_SHEET_NAME).toString()).distinct().collect(Collectors.toList());
  120. sheetNameList.addAll(this.colWidthList.stream().map(x -> x.get(KEY_SHEET_NAME).toString()).distinct().collect(Collectors.toList()));
  121. sheetNameList = sheetNameList.stream().distinct().collect(Collectors.toList());
  122. }
  123. @Override
  124. public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row
  125. , Integer relativeRowIndex, Boolean isHead) {
  126. Sheet sheet = writeSheetHolder.getSheet();
  127. //不需要设置行高列宽,或者当前sheet页不需要设置行高列宽
  128. if ((CollectionUtil.isEmpty(rowHeightList) && CollectionUtil.isEmpty(colWidthList))
  129. || sheetNameList.contains(sheet.getSheetName()) == false) {
  130. return;
  131. }
  132. //获取当前sheet页当前行的行高信息
  133. List<Map<String, Object>> sheetRowHeightMapList = rowHeightList.stream().filter(x ->
  134. StrUtil.equals(x.get(KEY_SHEET_NAME).toString(), sheet.getSheetName())
  135. && (int) x.get(KEY_ROW_INDEX) == relativeRowIndex).collect(Collectors.toList());
  136. for (Map<String, Object> map : sheetRowHeightMapList) {
  137. //行号
  138. Integer rowIndex = (Integer) map.get(KEY_ROW_INDEX);
  139. //行高
  140. Float rowHeight = (Float) map.get(KEY_ROW_HEIGHT);
  141. //设置行高
  142. if (rowIndex != null && rowHeight != null) {
  143. row.setHeightInPoints(rowHeight);
  144. }
  145. }
  146. //获取当前sheet页的列宽信息
  147. List<Map<String, Object>> sheetColWidthMapList = colWidthList.stream().filter(x ->
  148. StrUtil.equals(x.get(KEY_SHEET_NAME).toString(), sheet.getSheetName())).collect(Collectors.toList());
  149. for (Map<String, Object> map : sheetColWidthMapList) {
  150. //列号
  151. Integer colIndex = (Integer) map.get(KEY_COL_INDEX);
  152. //列宽
  153. Integer colWidth = (Integer) map.get(KEY_COL_WIDTH);
  154. //设置列宽
  155. if (colIndex != null && colWidth != null) {
  156. sheet.setColumnWidth(colIndex, colWidth * 256);
  157. }
  158. }
  159. //删除已添加的行高信息
  160. rowHeightList.removeAll(sheetRowHeightMapList);
  161. //删除已添加的列宽信息
  162. colWidthList.removeAll(sheetColWidthMapList);
  163. //重新获取要添加的sheet页姓名
  164. sheetNameList = this.rowHeightList.stream().map(x -> x.get(KEY_SHEET_NAME).toString()).distinct().collect(Collectors.toList());
  165. sheetNameList.addAll(this.colWidthList.stream().map(x -> x.get(KEY_SHEET_NAME).toString()).distinct().collect(Collectors.toList()));
  166. sheetNameList = sheetNameList.stream().distinct().collect(Collectors.toList());
  167. }
  168. }

3 调试代码

  1. /**
  2. * 导出(设置行高列宽、隐藏行和列)
  3. *
  4. * @param response
  5. */
  6. @GetMapping("/exportRowHeightColWidth")
  7. public void exportRowHeightColWidth(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[]{"表头17777777777", "表头2", "表头3", "表头4444"})));
  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<Map<String, Object>> rowHeightColWidthList = new ArrayList<>();
  17. //设置行高
  18. rowHeightColWidthList.add(CustomRowHeightColWidthHandler.createRowHeightMap("模板", 0, 20f));
  19. //隐藏行
  20. rowHeightColWidthList.add(CustomRowHeightColWidthHandler.createRowHeightMap("模板", 2, 0f));
  21. //设置列宽
  22. rowHeightColWidthList.add(CustomRowHeightColWidthHandler.createColWidthMap("模板", 0, 20));
  23. //隐藏列
  24. rowHeightColWidthList.add(CustomRowHeightColWidthHandler.createColWidthMap("模板", 2, 0));
  25. response.addHeader("Content-Disposition", "filename=" + fileName);
  26. //设置类型,扩展名为.xls
  27. response.setContentType("application/vnd.ms-excel");
  28. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
  29. .registerWriteHandler(new CustomRowHeightColWidthHandler(rowHeightColWidthList)).build();
  30. WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
  31. excelWriter.write(dataList, writeSheet);
  32. //千万别忘记finish 会帮忙关闭流
  33. excelWriter.finish();
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }

4 调试结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTc0NjM4_size_16_color_FFFFFF_t_70

发表评论

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

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

相关阅读