其他分享
首页 > 其他分享> > POI导出Excel将相同的列合并行

POI导出Excel将相同的列合并行

作者:互联网

public static void mergeCell(List<QsggDTO> list, Sheet sheet, int[] index){
        int id1 = 0;
        int id2 = 0;

        int firstRow = 2;
        int lastRow = 2;

        QsggDTO qsggDTO = list.get(0);
        id1 = qsggDTO.getId();

        for (int i = 1; i < list.size(); i++) {
            qsggDTO = list.get(i);
            id2 = qsggDTO.getId();

            if(id1==id2){
                lastRow = i+2;
            }else {
                if(firstRow < lastRow){
                    for(int c:index){
                        sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,c,c));
                    }
                }

                id1 = id2;
                firstRow = lastRow + 1;

                lastRow = firstRow;
            }

            if((i+1)==list.size()){
                if(firstRow < lastRow){
                    for(int c:index){
                        sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,c,c));
                    }
                }
            }
        }
    }

 

调用:

mergeCell(list, sheet, new int[]{0,1,2});

参数

new int[]{0,1,2}

表示合并第1,2,3列。

 

标签:id2,sheet,int,Excel,导出,firstRow,list,POI,lastRow
来源: https://www.cnblogs.com/yshyee/p/15631596.html