欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POI合并单元格

程序员文章站 2022-06-14 09:08:24
...

核心代码:

CellRangeAddress region = new CellRangeAddress(rowNum2-(chongfucishu-1), rowNum2, 0, 0);
                        sheet2.addMergedRegion(region);

        //year1  年份重复的
        for (String key : map_year1.keySet()) {
            String years = key;//重复的矿山ID
            Integer chongfucishu = map_year1.get(key);//重复次数
            int row_num=1;
            //在表中存放查询到的数据放入对应的列
            for (SimilarityRatioEntity sre : quchonghou) {

                if(sre.getPreID().toString().equals(key)){
                    System.out.println("row_num="+row_num);
                    XSSFRow row1 = sheet2.createRow(rowNum2);

                    XSSFCell cell0 = row1.createCell(0);
                    XSSFCell cell2 = row1.createCell(2);
                    XSSFCell cell1 = row1.createCell(1);
                    XSSFCell cell3 = row1.createCell(3);

                    cell0.setCellValue(sre.getPreID());
                    cell1.setCellValue(sre.getNextID());
                    cell2.setCellValue(sre.getPreName());
                    cell3.setCellValue(sre.getNextName());
                    row1.createCell(4).setCellValue(sre.getValue());
                    if (sre.getFlag()==null){
                        row1.createCell(5).setCellValue("");
                    }else{
                        row1.createCell(5).setCellValue(sre.getFlag());
                    }

                    if(row_num==chongfucishu){
                        CellRangeAddress region = new CellRangeAddress(rowNum2-(chongfucishu-1), rowNum2, 0, 0);
                        sheet2.addMergedRegion(region);
                        CellRangeAddress region2 = new CellRangeAddress(rowNum2-(chongfucishu-1), rowNum2, 2, 2);
                        sheet2.addMergedRegion(region2);
                        rowNum2=rowNum2+1;
                    }else {
                        rowNum2++;
                    }

                    row_num++;
                    si.add(sre);
                }


            }

        }
        //把之前添加的都删除
        quchonghou.removeAll(si);
        //year2  年份重复的
        for (String key : map_year2.keySet()) {
            String years = key;//重复的矿山ID
            Integer chongfucishu = map_year2.get(key);//重复次数
            int row_num=1;
            //在表中存放查询到的数据放入对应的列
            for (SimilarityRatioEntity sre : quchonghou) {

                if(sre.getNextID().toString().equals(key)){
                    System.out.println("row_num="+row_num);
                    XSSFRow row1 = sheet2.createRow(rowNum2);

                    XSSFCell cell0 = row1.createCell(0);
                    XSSFCell cell2 = row1.createCell(2);
                    XSSFCell cell1 = row1.createCell(1);
                    XSSFCell cell3 = row1.createCell(3);

                    cell0.setCellValue(sre.getPreID());
                    cell1.setCellValue(sre.getNextID());
                    cell2.setCellValue(sre.getPreName());
                    cell3.setCellValue(sre.getNextName());
                    row1.createCell(4).setCellValue(sre.getValue());
                    if (sre.getFlag()==null){
                        row1.createCell(5).setCellValue("");
                    }else{
                        row1.createCell(5).setCellValue(sre.getFlag());
                    }

                    if(row_num==chongfucishu){
                        CellRangeAddress region = new CellRangeAddress(rowNum2-(chongfucishu-1), rowNum2, 1, 1);
                        sheet2.addMergedRegion(region);
                        CellRangeAddress region2 = new CellRangeAddress(rowNum2-(chongfucishu-1), rowNum2, 3, 3);
                        sheet2.addMergedRegion(region2);
                        rowNum2=rowNum2+1;
                    }else {
                        rowNum2++;
                    }

                    row_num++;
                }
            }
        }