java 实现excel跨行合并单元格

excel表格原始数据

代码实现后得到的效果数据

java 实现excel跨行合并单元格代码

package com.test.com;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class ExcelRelated {

    //跨行合并单元格
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("E:\\file\\测试文件.xlsx");
             Workbook workbook = new XSSFWorkbook(fis);
             FileOutputStream fos = new FileOutputStream("E:\\file\\测试新文件.xlsx")){
            Sheet sheet = workbook.getSheetAt(0);
            //用LinkedHashMap保持取得数据顺序是一致的
            Map<String, List<Row>> listMap = new LinkedHashMap<>();
            for (Row row : sheet) {
                //获取单位列的值,单位放在第一列
                String gName = row.getCell(0).getStringCellValue();
                //将数据根据单位分组,将单位相同的行数据组合在一起
                listMap.computeIfAbsent(gName, k -> new ArrayList<>()).add(row);
            }
            //从map中将行数据取出来
            List<Row> rowList = new ArrayList<>();
            for (Map.Entry<String, List<Row>> entry : listMap.entrySet()) {
                rowList.addAll(entry.getValue());
            }
            int lastRowNum = sheet.getLastRowNum();//最后一行行号
            int beginR = lastRowNum + 1;
            //循环行数据
            for (int i = 0; i < rowList.size(); i++) {
                //从最后一行开始创建行,创建列,赋值
                Row row = sheet.createRow(beginR + i);
                for (int j = 0; j <= 2; j++) {
                    Cell cell = row.createCell(j);
                    cell.setCellStyle(gainStyle(sheet,0,j));
                    cell.setCellValue(rowList.get(i).getCell(j).getStringCellValue());
                }
            }
            //删除开始未排序的数据信息
            for (int i = 0 ; i <= lastRowNum ;i++){
                sheet.removeRow(sheet.getRow(0));
                sheet.shiftRows(1,sheet.getLastRowNum(),-1);
            }
            //相同单位合并单元格
            String prevUnit = "";
            int startRow = 0;
            // 遍历每一行
            for (int i = 0; i <= lastRowNum; i++) {
                Row row = sheet.getRow(i);
                Cell unitCell = row.getCell(0);  // 假设"单位"列是第0列
                String currentUnit = unitCell.getStringCellValue();

                if (!currentUnit.equals(prevUnit)) {
                    // 如果单位改变了,则合并前一组相同单位的单元格
                    if (i > startRow + 1) {
                        mergeCells(sheet, startRow, i - 1, 0, 0);  //合并第一列
                    }
                    // 更新新的起始行和前一个单位
                    startRow = i;
                    prevUnit = currentUnit;
                }
            }
            // 处理最后一组单位的合并
            if (lastRowNum > startRow) {
                mergeCells(sheet, startRow, lastRowNum, 0, 0);  // 合并第一列
            }
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取样式
     * @param sheet sheet表格
     * @param beginH 行
     * @param j 列号
     * @return 样式
     */

    public static CellStyle gainStyle(Sheet sheet,int beginH,int j){
        Row row = sheet.getRow(beginH);
        Cell cell = row.getCell(j);
        return cell.getCellStyle();
    }

    // 合并从startRow到endRow的列,startColumn到endColumn
    private static void mergeCells(Sheet sheet, int startRow, int endRow, int startColumn, int endColumn) {
        for (int col = startColumn; col <= endColumn; col++) {
            // 合并列中指定的单元格范围
            sheet.addMergedRegion(new CellRangeAddress(startRow, endRow, col, col));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值