赞
踩
目录
3 CustomRowHeightColWidthHandler
- <!--hutool工具包-->
- <dependency>
- <groupId>cn.hutool</groupId>
- <artifactId>hutool-all</artifactId>
- <version>5.5.1</version>
- </dependency>
- <!--easyexcel文档处理工具-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>easyexcel</artifactId>
- <version>2.2.8</version>
- </dependency>
行高列宽信息(Builder构建者模式)。
- package com.xudongbase.common.easyexcel.model;
-
- import lombok.Getter;
-
- /**
- * 行高列宽信息(Builder构建者模式)
- *
- * @author xudongmaster
- */
- @Getter
- public class RowHeightColWidthModel {
- /**
- * sheet名称
- */
- private String sheetName;
- /**
- * 行号
- */
- private Integer rowIndex;
- /**
- * 列号
- */
- private Integer colIndex;
- /**
- * 行高
- */
- private Float rowHeight;
- /**
- * 列宽
- */
- private Integer colWidth;
-
- private RowHeightColWidthModel(Builder builder) {
- this.sheetName = builder.sheetName;
- this.rowIndex = builder.rowIndex;
- this.colIndex = builder.colIndex;
- this.rowHeight = builder.rowHeight;
- this.colWidth = builder.colWidth;
- }
-
- public static class Builder {
-
- /**
- * sheet名称
- */
- private String sheetName;
- /**
- * 行号
- */
- private Integer rowIndex;
- /**
- * 列号
- */
- private Integer colIndex;
- /**
- * 行高
- */
- private Float rowHeight;
- /**
- * 列宽
- */
- private Integer colWidth;
-
-
- public Builder rowIndex(Integer rowIndex) {
- this.rowIndex = rowIndex;
- return this;
- }
-
- public Builder colIndex(Integer colIndex) {
- this.colIndex = colIndex;
- return this;
- }
-
- public Builder rowHeight(Float rowHeight) {
- this.rowHeight = rowHeight;
- return this;
- }
-
- public Builder colWidth(Integer colWidth) {
- this.colWidth = colWidth;
- return this;
- }
-
- public Builder(String sheetName) {
- this.sheetName = sheetName;
- }
-
- public RowHeightColWidthModel build() {
- return new RowHeightColWidthModel(this);
- }
- }
-
- /**
- * 创建隐藏行信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @return
- */
- public static RowHeightColWidthModel createHideRowModel(String sheetName, Integer rowIndex) {
- return createRowHeightColWidthModel(sheetName, rowIndex, 0f, null, null);
- }
-
- /**
- * 创建隐藏列信息
- *
- * @param sheetName sheet页名称
- * @param colIndex 列号
- * @return
- */
- public static RowHeightColWidthModel createHideColModel(String sheetName, Integer colIndex) {
- return createRowHeightColWidthModel(sheetName, null, null, colIndex, 0);
- }
-
- /**
- * 创建行高信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param rowHeight 行高
- * @return
- */
- public static RowHeightColWidthModel createRowHeightModel(String sheetName, Integer rowIndex, Float rowHeight) {
- return createRowHeightColWidthModel(sheetName, rowIndex, rowHeight, null, null);
- }
-
- /**
- * 创建列宽信息
- *
- * @param sheetName sheet页名称
- * @param colIndex 列号
- * @param colWidth 列宽
- * @return
- */
- public static RowHeightColWidthModel createColWidthModel(String sheetName, Integer colIndex, Integer colWidth) {
- return createRowHeightColWidthModel(sheetName, null, null, colIndex, colWidth);
- }
-
- /**
- * 创建行高列宽信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param rowHeight 行高
- * @param colIndex 列号
- * @param colWidth 列宽
- * @return
- */
- public static RowHeightColWidthModel createRowHeightColWidthModel(String sheetName, Integer rowIndex, Float rowHeight, Integer colIndex, Integer colWidth) {
- return new RowHeightColWidthModel.Builder(sheetName)
- //行号
- .rowIndex(rowIndex)
- //行高
- .rowHeight(rowHeight)
- //显示列号
- .colIndex(colIndex)
- //列宽
- .colWidth(colWidth)
- .build();
- }
- }
自定义行高列宽处理器。
- package com.xudongbase.common.easyexcel.handler;
-
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
- import com.alibaba.excel.write.metadata.holder.*;
- import com.xudongbase.common.easyexcel.model.RowHeightColWidthModel;
- import com.xudongbase.common.util.EqualsUtil;
- import org.apache.poi.ss.usermodel.*;
-
- import java.util.*;
- import java.util.stream.Collectors;
-
- /**
- * 自定义行高列宽处理器
- *
- * @author xudongmaster
- */
- public class CustomRowHeightColWidthHandler extends AbstractRowWriteHandler {
-
- /**
- * sheet页名称列表
- */
- private List<String> sheetNameList;
- /**
- * 列宽信息
- */
- private List<RowHeightColWidthModel> colWidthList = new ArrayList<>();
-
- /**
- * 行高信息
- */
- private List<RowHeightColWidthModel> rowHeightList = new ArrayList<>();
-
-
- /**
- * 自定义行高列宽适配器构造方法
- *
- * @param rowHeightColWidthList 行高列宽信息
- */
- public CustomRowHeightColWidthHandler(List<RowHeightColWidthModel> rowHeightColWidthList) {
- if (CollUtil.isEmpty(rowHeightColWidthList)) {
- return;
- }
- rowHeightColWidthList = rowHeightColWidthList.stream().filter(x ->
- StrUtil.isNotBlank(x.getSheetName())).collect(Collectors.toList());
- //填充行高信息
- this.rowHeightList = rowHeightColWidthList.stream().filter(x ->
- x.getRowIndex() != null && x.getRowIndex() >= 0 && x.getRowHeight() != null && x.getRowHeight() >= 0).collect(Collectors.toList());
- //填充列宽信息
- this.colWidthList = rowHeightColWidthList.stream().filter(x ->
- x.getColIndex() != null && x.getColIndex() >= 0 && x.getColWidth() != null && x.getColWidth() >= 0).collect(Collectors.toList());
- //获取sheet页名称
- sheetNameList = this.rowHeightList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
- sheetNameList.addAll(this.colWidthList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList()));
- sheetNameList = sheetNameList.stream().distinct().collect(Collectors.toList());
- }
-
- @Override
- public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row
- , Integer relativeRowIndex, Boolean isHead) {
- Sheet sheet = writeSheetHolder.getSheet();
- //不需要设置行高列宽,或者当前sheet页不需要设置行高列宽
- if ((CollUtil.isEmpty(rowHeightList) && CollUtil.isEmpty(colWidthList))
- || sheetNameList.contains(sheet.getSheetName()) == false) {
- return;
- }
- //获取当前sheet页当前行的行高信息
- List<RowHeightColWidthModel> sheetRowHeightList = rowHeightList.stream().filter(x ->
- StrUtil.equals(x.getSheetName(), sheet.getSheetName())
- && EqualsUtil.equals(x.getRowIndex(),relativeRowIndex)).collect(Collectors.toList());
- for (RowHeightColWidthModel rowHeightModel : sheetRowHeightList) {
- //行高
- Float rowHeight = rowHeightModel.getRowHeight();
- //设置行高
- if (rowHeight != null) {
- row.setHeightInPoints(rowHeight);
- }
- }
- //获取当前sheet页的列宽信息
- List<RowHeightColWidthModel> sheetColWidthList = colWidthList.stream().filter(x ->
- StrUtil.equals(x.getSheetName(), sheet.getSheetName())).collect(Collectors.toList());
- for (RowHeightColWidthModel colWidthModel : sheetColWidthList) {
- //列号
- Integer colIndex = colWidthModel.getColIndex();
- //列宽
- Integer colWidth = colWidthModel.getColWidth();
- //设置列宽
- if (colIndex != null && colWidth != null) {
- sheet.setColumnWidth(colIndex, colWidth * 256);
- }
- }
- //删除已添加的行高信息
- rowHeightList.removeAll(sheetRowHeightList);
- //删除已添加的列宽信息
- colWidthList.removeAll(sheetColWidthList);
- //重新获取要添加的sheet页姓名
- sheetNameList = this.rowHeightList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
- sheetNameList.addAll(this.colWidthList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList()));
- sheetNameList = sheetNameList.stream().distinct().collect(Collectors.toList());
- }
- }
- /**
- * 测试行高列宽、隐藏行和列
- */
- @Test
- public void testRowHeightColWidth() {
- try {
- File file = new File("D:/easyexcel/testRowHeightColWidth.xlsx");
- FileUtil.createNewFile(file);
- //生成表格数据
- List<List<Object>> dataList = new ArrayList<>();
- dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头11", "表头2", "表头3", "表头4"})));
- dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头17777777777", "表头2", "表头3", "表头4444"})));
- dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头31", "表头2", "表头3", "表头4"})));
-
- List<RowHeightColWidthModel> rowHeightColWidthList = new ArrayList<>();
- String sheetName="模板";
- //设置行高
- rowHeightColWidthList.add(RowHeightColWidthModel.createRowHeightModel(sheetName, 0, 20f));
- //隐藏行
- rowHeightColWidthList.add(RowHeightColWidthModel.createHideRowModel(sheetName, 2));
- //设置列宽
- rowHeightColWidthList.add(RowHeightColWidthModel.createColWidthModel(sheetName, 0, 20));
- //隐藏列
- rowHeightColWidthList.add(RowHeightColWidthModel.createHideColModel("模板", 2));
- FileOutputStream fileOutputStream = new FileOutputStream(file);
- ExcelWriter excelWriter = EasyExcel.write(fileOutputStream)
- .registerWriteHandler(new CustomRowHeightColWidthHandler(rowHeightColWidthList)).build();
- WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
- excelWriter.write(dataList, writeSheet);
- //千万别忘记finish 会帮忙关闭流
- excelWriter.finish();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
如果需要源码请前往Gitee查看。
xudongbase: 主要是项目中可以用到的共通方法https://gitee.com/xudong_master/xudongbase
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。