当前位置:   article > 正文

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

easyexcel隐藏列

目录

1 Maven依赖

2 RowHeightColWidthModel

3 CustomRowHeightColWidthHandler

4 调试代码

5 调试结果

注:


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 RowHeightColWidthModel

行高列宽信息(Builder构建者模式)。

  1. package com.xudongbase.common.easyexcel.model;
  2. import lombok.Getter;
  3. /**
  4. * 行高列宽信息(Builder构建者模式)
  5. *
  6. * @author xudongmaster
  7. */
  8. @Getter
  9. public class RowHeightColWidthModel {
  10. /**
  11. * sheet名称
  12. */
  13. private String sheetName;
  14. /**
  15. * 行号
  16. */
  17. private Integer rowIndex;
  18. /**
  19. * 列号
  20. */
  21. private Integer colIndex;
  22. /**
  23. * 行高
  24. */
  25. private Float rowHeight;
  26. /**
  27. * 列宽
  28. */
  29. private Integer colWidth;
  30. private RowHeightColWidthModel(Builder builder) {
  31. this.sheetName = builder.sheetName;
  32. this.rowIndex = builder.rowIndex;
  33. this.colIndex = builder.colIndex;
  34. this.rowHeight = builder.rowHeight;
  35. this.colWidth = builder.colWidth;
  36. }
  37. public static class Builder {
  38. /**
  39. * sheet名称
  40. */
  41. private String sheetName;
  42. /**
  43. * 行号
  44. */
  45. private Integer rowIndex;
  46. /**
  47. * 列号
  48. */
  49. private Integer colIndex;
  50. /**
  51. * 行高
  52. */
  53. private Float rowHeight;
  54. /**
  55. * 列宽
  56. */
  57. private Integer colWidth;
  58. public Builder rowIndex(Integer rowIndex) {
  59. this.rowIndex = rowIndex;
  60. return this;
  61. }
  62. public Builder colIndex(Integer colIndex) {
  63. this.colIndex = colIndex;
  64. return this;
  65. }
  66. public Builder rowHeight(Float rowHeight) {
  67. this.rowHeight = rowHeight;
  68. return this;
  69. }
  70. public Builder colWidth(Integer colWidth) {
  71. this.colWidth = colWidth;
  72. return this;
  73. }
  74. public Builder(String sheetName) {
  75. this.sheetName = sheetName;
  76. }
  77. public RowHeightColWidthModel build() {
  78. return new RowHeightColWidthModel(this);
  79. }
  80. }
  81. /**
  82. * 创建隐藏行信息
  83. *
  84. * @param sheetName sheet页名称
  85. * @param rowIndex 行号
  86. * @return
  87. */
  88. public static RowHeightColWidthModel createHideRowModel(String sheetName, Integer rowIndex) {
  89. return createRowHeightColWidthModel(sheetName, rowIndex, 0f, null, null);
  90. }
  91. /**
  92. * 创建隐藏列信息
  93. *
  94. * @param sheetName sheet页名称
  95. * @param colIndex 列号
  96. * @return
  97. */
  98. public static RowHeightColWidthModel createHideColModel(String sheetName, Integer colIndex) {
  99. return createRowHeightColWidthModel(sheetName, null, null, colIndex, 0);
  100. }
  101. /**
  102. * 创建行高信息
  103. *
  104. * @param sheetName sheet页名称
  105. * @param rowIndex 行号
  106. * @param rowHeight 行高
  107. * @return
  108. */
  109. public static RowHeightColWidthModel createRowHeightModel(String sheetName, Integer rowIndex, Float rowHeight) {
  110. return createRowHeightColWidthModel(sheetName, rowIndex, rowHeight, null, null);
  111. }
  112. /**
  113. * 创建列宽信息
  114. *
  115. * @param sheetName sheet页名称
  116. * @param colIndex 列号
  117. * @param colWidth 列宽
  118. * @return
  119. */
  120. public static RowHeightColWidthModel createColWidthModel(String sheetName, Integer colIndex, Integer colWidth) {
  121. return createRowHeightColWidthModel(sheetName, null, null, colIndex, colWidth);
  122. }
  123. /**
  124. * 创建行高列宽信息
  125. *
  126. * @param sheetName sheet页名称
  127. * @param rowIndex 行号
  128. * @param rowHeight 行高
  129. * @param colIndex 列号
  130. * @param colWidth 列宽
  131. * @return
  132. */
  133. public static RowHeightColWidthModel createRowHeightColWidthModel(String sheetName, Integer rowIndex, Float rowHeight, Integer colIndex, Integer colWidth) {
  134. return new RowHeightColWidthModel.Builder(sheetName)
  135. //行号
  136. .rowIndex(rowIndex)
  137. //行高
  138. .rowHeight(rowHeight)
  139. //显示列号
  140. .colIndex(colIndex)
  141. //列宽
  142. .colWidth(colWidth)
  143. .build();
  144. }
  145. }

3 CustomRowHeightColWidthHandler

自定义行高列宽处理器。

  1. package com.xudongbase.common.easyexcel.handler;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
  6. import com.alibaba.excel.write.metadata.holder.*;
  7. import com.xudongbase.common.easyexcel.model.RowHeightColWidthModel;
  8. import com.xudongbase.common.util.EqualsUtil;
  9. import org.apache.poi.ss.usermodel.*;
  10. import java.util.*;
  11. import java.util.stream.Collectors;
  12. /**
  13. * 自定义行高列宽处理器
  14. *
  15. * @author xudongmaster
  16. */
  17. public class CustomRowHeightColWidthHandler extends AbstractRowWriteHandler {
  18. /**
  19. * sheet页名称列表
  20. */
  21. private List<String> sheetNameList;
  22. /**
  23. * 列宽信息
  24. */
  25. private List<RowHeightColWidthModel> colWidthList = new ArrayList<>();
  26. /**
  27. * 行高信息
  28. */
  29. private List<RowHeightColWidthModel> rowHeightList = new ArrayList<>();
  30. /**
  31. * 自定义行高列宽适配器构造方法
  32. *
  33. * @param rowHeightColWidthList 行高列宽信息
  34. */
  35. public CustomRowHeightColWidthHandler(List<RowHeightColWidthModel> rowHeightColWidthList) {
  36. if (CollUtil.isEmpty(rowHeightColWidthList)) {
  37. return;
  38. }
  39. rowHeightColWidthList = rowHeightColWidthList.stream().filter(x ->
  40. StrUtil.isNotBlank(x.getSheetName())).collect(Collectors.toList());
  41. //填充行高信息
  42. this.rowHeightList = rowHeightColWidthList.stream().filter(x ->
  43. x.getRowIndex() != null && x.getRowIndex() >= 0 && x.getRowHeight() != null && x.getRowHeight() >= 0).collect(Collectors.toList());
  44. //填充列宽信息
  45. this.colWidthList = rowHeightColWidthList.stream().filter(x ->
  46. x.getColIndex() != null && x.getColIndex() >= 0 && x.getColWidth() != null && x.getColWidth() >= 0).collect(Collectors.toList());
  47. //获取sheet页名称
  48. sheetNameList = this.rowHeightList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
  49. sheetNameList.addAll(this.colWidthList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList()));
  50. sheetNameList = sheetNameList.stream().distinct().collect(Collectors.toList());
  51. }
  52. @Override
  53. public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row
  54. , Integer relativeRowIndex, Boolean isHead) {
  55. Sheet sheet = writeSheetHolder.getSheet();
  56. //不需要设置行高列宽,或者当前sheet页不需要设置行高列宽
  57. if ((CollUtil.isEmpty(rowHeightList) && CollUtil.isEmpty(colWidthList))
  58. || sheetNameList.contains(sheet.getSheetName()) == false) {
  59. return;
  60. }
  61. //获取当前sheet页当前行的行高信息
  62. List<RowHeightColWidthModel> sheetRowHeightList = rowHeightList.stream().filter(x ->
  63. StrUtil.equals(x.getSheetName(), sheet.getSheetName())
  64. && EqualsUtil.equals(x.getRowIndex(),relativeRowIndex)).collect(Collectors.toList());
  65. for (RowHeightColWidthModel rowHeightModel : sheetRowHeightList) {
  66. //行高
  67. Float rowHeight = rowHeightModel.getRowHeight();
  68. //设置行高
  69. if (rowHeight != null) {
  70. row.setHeightInPoints(rowHeight);
  71. }
  72. }
  73. //获取当前sheet页的列宽信息
  74. List<RowHeightColWidthModel> sheetColWidthList = colWidthList.stream().filter(x ->
  75. StrUtil.equals(x.getSheetName(), sheet.getSheetName())).collect(Collectors.toList());
  76. for (RowHeightColWidthModel colWidthModel : sheetColWidthList) {
  77. //列号
  78. Integer colIndex = colWidthModel.getColIndex();
  79. //列宽
  80. Integer colWidth = colWidthModel.getColWidth();
  81. //设置列宽
  82. if (colIndex != null && colWidth != null) {
  83. sheet.setColumnWidth(colIndex, colWidth * 256);
  84. }
  85. }
  86. //删除已添加的行高信息
  87. rowHeightList.removeAll(sheetRowHeightList);
  88. //删除已添加的列宽信息
  89. colWidthList.removeAll(sheetColWidthList);
  90. //重新获取要添加的sheet页姓名
  91. sheetNameList = this.rowHeightList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
  92. sheetNameList.addAll(this.colWidthList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList()));
  93. sheetNameList = sheetNameList.stream().distinct().collect(Collectors.toList());
  94. }
  95. }

4 调试代码

  1. /**
  2. * 测试行高列宽、隐藏行和列
  3. */
  4. @Test
  5. public void testRowHeightColWidth() {
  6. try {
  7. File file = new File("D:/easyexcel/testRowHeightColWidth.xlsx");
  8. FileUtil.createNewFile(file);
  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. List<RowHeightColWidthModel> rowHeightColWidthList = new ArrayList<>();
  15. String sheetName="模板";
  16. //设置行高
  17. rowHeightColWidthList.add(RowHeightColWidthModel.createRowHeightModel(sheetName, 0, 20f));
  18. //隐藏行
  19. rowHeightColWidthList.add(RowHeightColWidthModel.createHideRowModel(sheetName, 2));
  20. //设置列宽
  21. rowHeightColWidthList.add(RowHeightColWidthModel.createColWidthModel(sheetName, 0, 20));
  22. //隐藏列
  23. rowHeightColWidthList.add(RowHeightColWidthModel.createHideColModel("模板", 2));
  24. FileOutputStream fileOutputStream = new FileOutputStream(file);
  25. ExcelWriter excelWriter = EasyExcel.write(fileOutputStream)
  26. .registerWriteHandler(new CustomRowHeightColWidthHandler(rowHeightColWidthList)).build();
  27. WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
  28. excelWriter.write(dataList, writeSheet);
  29. //千万别忘记finish 会帮忙关闭流
  30. excelWriter.finish();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }

5 调试结果

注:

如果需要源码请前往Gitee查看。

xudongbase: 主要是项目中可以用到的共通方法https://gitee.com/xudong_master/xudongbase

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

闽ICP备14008679号