Excel合并单元格后,原单元格的数值没有清除的处理

1、背景:

工作中需要对一个固定的单元格做合并处理,并且合并后原有单元格的数据要清除,例如:

合并前Excel如下:

 

 合并后Excel如下:

 

 并且将A,B列解除合并后,需要显示如下(而非恢复到合并前的Excel):

 

 

 

2、代码如下:

 

 public static void main(String[] args) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("sheet");

        //第一行
        Row row0 = sheet.createRow(0);//新增行
        Cell cell_00 = row0.createCell(0);//新增(0,0)单元格
        cell_00.setCellValue("序号");

        Cell cell_01 = row0.createCell(1);//新增(0,1)单元格
        cell_01.setCellValue("学生名称");

        Cell cell_02 = row0.createCell(2);//新增(0,2)单元格
        cell_02.setCellValue("学科");

        Cell cell_03 = row0.createCell(3);//新增(0,3)单元格
        cell_03.setCellValue("成绩");

        //第二行
        Row row1 = sheet.createRow(1);//新增行
        Cell cell_10 = row1.createCell(0);//新增(0,0)单元格
        cell_10.setCellValue("1");

        Cell cell_11 = row1.createCell(1);//新增(0,1)单元格
        cell_11.setCellValue("张三");

        Cell cell_12 = row1.createCell(2);//新增(0,2)单元格
        cell_12.setCellValue("数学");

        Cell cell_13 = row1.createCell(3);//新增(0,3)单元格
        cell_13.setCellValue("98.00");

        //第三行
        Row row2 = sheet.createRow(2);//新增行
        Cell cell_20 = row2.createCell(0);//新增(0,0)单元格
        cell_20.setCellValue("1");

        Cell cell_21 = row2.createCell(1);//新增(0,1)单元格
        cell_21.setCellValue("张三");

        Cell cell_22 = row2.createCell(2);//新增(0,2)单元格
        cell_22.setCellValue("语文");

        Cell cell_23 = row2.createCell(3);//新增(0,3)单元格
        cell_23.setCellValue("78.00");

        //第四行
        Row row3 = sheet.createRow(3);//新增行
        Cell cell_30 = row3.createCell(0);//新增(0,0)单元格
        cell_30.setCellValue("1");

        Cell cell_31 = row3.createCell(1);//新增(0,1)单元格
        cell_31.setCellValue("张三");

        Cell cell_32 = row3.createCell(2);//新增(0,2)单元格
        cell_32.setCellValue("外语");

        Cell cell_33 = row3.createCell(3);//新增(0,3)单元格
        cell_33.setCellValue("100.00");


        int[] hebingIndex = {0, 1};

        for (int m = 0; m < hebingIndex.length; m++) {
            int num = hebingIndex[m];//合并的列
            int totalRow = row0.getLastCellNum() - 1;//除标题外的总行数
            String s = sheet.getRow(1).getCell(num).getStringCellValue();
            for (int i = 1; i < totalRow; i++) {
                String value = sheet.getRow(i + 1).getCell(num).getStringCellValue();
                if (value.equals(s)) {//如果后面每一行的值都和第一行一样,就清除这些单元格的值
                    sheet.getRow(i + 1).getCell(num).setCellValue("");

                }
            }
            // 合并日期占两行(4个参数,分别为起始行,结束行,起始列,结束列)
            // 行和列都是从0开始计数,且起始结束都会合并
            CellRangeAddress region = new CellRangeAddress(1, totalRow, num, num);
            sheet.addMergedRegion(region);
        }
        File file = new File("demo.xls");
        FileOutputStream fout = new FileOutputStream(file);
        workbook.write(fout);
        fout.close();


    }

 使用依赖:

 <!-- 使用 xls 格式 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- 使用 xlsx 格式 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>

 

3、目前没有实现的功能

目前固定了要合并的行数,没法根据实际情况来灵活合并行。

 

posted @ 2022-08-10 22:24  一块糖  阅读(618)  评论(0编辑  收藏  举报