JAVA Excel逐级合并单元格算法基于Hutool工具

文章介绍了如何使用Hutool库中的`mergeRowsByMap`方法,根据Map结构动态合并Excel表格的单元格,实现根据不同列值合并内容。
摘要由CSDN通过智能技术生成

pom依赖:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.24</version>
</dependency>

算法代码如下:

/**
     * 根据map 从最深层开始描述excel数据,map的key理解为当前列 value理解为后一列,最后一列不需要合并,一个map如果value有三个,那么key需要合并当前列三行的数据
     * */
    public static ExcelRowTag mergeRowsByMap(Map<Object, ?> map, ExcelWriter writer, Integer column) {
        boolean isFirst = true;
        ExcelRowTag excelRowTag = new ExcelRowTag();
        for (Map.Entry<Object, ?> mapChild : map.entrySet()) {
            Object contentObject = mapChild.getKey();
            if (!(mapChild.getValue() instanceof Map)) {
                if (isFirst) {
                    excelRowTag.setStart(writer.getRowCount());
                    excelRowTag.setEnd(writer.getRowCount());
                    isFirst = false;

                }
                excelRowTag.setEnd(writer.getRowCount());
                writer.writeCellValue(column, writer.getRowCount(), contentObject);
                continue;
            }

            ExcelRowTag excelColumnChild = mergeRowsByMap((Map<Object, ?>) mapChild.getValue(), writer, column + 1);
            if (isFirst) {
                excelRowTag.setStart(excelColumnChild.getStart());
                isFirst = false;
            }
            excelRowTag.setEnd(excelColumnChild.getEnd());
            if (!excelColumnChild.getStart().equals(excelColumnChild.getEnd())) {
                writer.merge(excelColumnChild.getStart(), excelColumnChild.getEnd(), column, column, contentObject, false);
            } else {
                writer.writeCellValue(column, excelColumnChild.getStart(), contentObject);
            }

        }
        return excelRowTag;
    }

ExcelRowTag类:

@Data
public class ExcelRowTag {
    private Integer start;

    private Integer end;
}

启动类

public static void main(String[] args) {
        try (ExcelWriter writer = ExcelUtil.getWriter(true);
             FileOutputStream fileOutputStream = new FileOutputStream("/Users/a1234/Desktop/images/test.xlsx");
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
            List<String> title = new ArrayList<>(Arrays.asList("标题1", "标题2", "标题3", "标题4"));
            writer.writeHeadRow(title);
            //组装数据
            List<List<Object>> rows = new ArrayList<>();
            rows.add(Arrays.asList("测试1-1", "测试2-1", "测试3-1", "测试4-1"));
            rows.add(Arrays.asList("测试1-1", "测试2-1", "测试3-1", "测试4-2"));
            rows.add(Arrays.asList("测试1-1", "测试2-1", "测试3-2", "测试4-1"));
            rows.add(Arrays.asList("测试1-1", "测试2-2", "测试3-1", "测试4-1"));
            rows.add(Arrays.asList("测试2-1", "测试2-2", "测试3-1", "测试4-1"));
            //将数据的每一列都作为key 后一列当作其value
            HashMap<Object, ?> collect = rows.stream().collect(Collectors.groupingBy(x -> getNonNullKey(x.get(0)), LinkedHashMap::new,
                    Collectors.groupingBy(x -> getNonNullKey(x.get(1)), LinkedHashMap::new,
                            Collectors.groupingBy(x -> getNonNullKey(x.get(2)), LinkedHashMap::new,
                                    Collectors.groupingBy(x -> getNonNullKey(x.get(3)))))));
            //输出到excel 并合并单元格

            mergeRowsByMap(collect, writer, 0);

            writer.flush(outputStream);
            outputStream.toByteArray();
            fileOutputStream.write(outputStream.toByteArray());

        }catch (Exception e){
        }

    }

输出结果:

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值