ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-08-10 22:30:43  阅读:218  来源: 互联网

标签:Cell 清除 单元格 Excel cell createCell 新增 setCellValue


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、目前没有实现的功能

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

 

标签:Cell,清除,单元格,Excel,cell,createCell,新增,setCellValue
来源: https://www.cnblogs.com/Bella-fu/p/16574164.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有