Java 如何在ApachePOI中使用rowiterator?

Java 如何在ApachePOI中使用rowiterator?,java,apache-poi,Java,Apache Poi,我正在寻找一种方法来创建用于从数据库报告的.xlsx文件。问题是,当我试图返回到文件的第一行时,数据被覆盖。有人对apache poi有同样的问题吗?我想在同一级别创建三个表。通过逐行同时创建所有表,我找到了解决问题的方法,但这需要很多时间,因为我的代码中有很多循环。我也尝试过使用getRow(I),但它不再有用了。我需要一些帮助 这是我通过获取第一行来优化的代码的一部分: for (int i = 0, j = 1, k = 2; k <= utilisateurs.size(); i

我正在寻找一种方法来创建用于从数据库报告的.xlsx文件。问题是,当我试图返回到文件的第一行时,数据被覆盖。有人对apache poi有同样的问题吗?我想在同一级别创建三个表。通过逐行同时创建所有表,我找到了解决问题的方法,但这需要很多时间,因为我的代码中有很多循环。我也尝试过使用getRow(I),但它不再有用了。我需要一些帮助

这是我通过获取第一行来优化的代码的一部分:

for (int i = 0, j = 1, k = 2; k <= utilisateurs.size(); i = i + 3, j = j + 3, k = k + 3) {
        Utilisateur u1 = null;
        Utilisateur u2 = null;
        Utilisateur u3 = null;
        int totaleU1 = 0;
        int totaleU2 = 0;
        int totaleU3 = 0;
        List<SyntheseCandidatDto> listeU1;
        List<SyntheseCandidatDto> listeU2;
        List<SyntheseCandidatDto> listeU3;
        if (utilisateurs.size() - i >= 3) {
            u1 = utilisateurs.get(i);
            u2 = utilisateurs.get(j);
            u3 = utilisateurs.get(k);
        } else if (utilisateurs.size() - i == 2) {
            u1 = utilisateurs.get(i);
            u2 = utilisateurs.get(j);
        } else if (utilisateurs.size() - i == 1) {
            u1 = utilisateurs.get(i);
        }
        Row hderRow;
        Cell hdrCell;
        Cell nomSourceurCell;
        hderRow = sh.createRow(rowNum++);
        if (u1 != null) {
            hdrCell = hderRow.createCell(0);
            hdrCell.setCellValue("Sourceur: ");
            hdrCell.setCellStyle(sousTitre);
            nomSourceurCell = hderRow.createCell(1);
            nomSourceurCell.setCellValue(utilisateurs.get(i).getNom() + " "
                    + utilisateurs.get(i).getPrenom());
            nomSourceurCell.setCellStyle(csNomSrc);
            sh.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1,
                    1, 2));
        }
        if (u2 != null) {
            Cell cell = hderRow.createCell(technologies.size() + 2);
            cell.setCellValue("Sourceur: ");
            cell.setCellStyle(sousTitre);
            nomSourceurCell = hderRow.createCell(technologies.size() + 3);
            nomSourceurCell
                    .setCellValue(u2.getNom() + " " + u2.getPrenom());
            nomSourceurCell.setCellStyle(csNomSrc);
            sh.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1,
                    technologies.size() + 3, technologies.size() + 4));
        }
        if (u3 != null) {
            Cell cellSrc = hderRow.createCell(2 * technologies.size() + 3);
            cellSrc.setCellValue("Sourceur: ");
            cellSrc.setCellStyle(sousTitre);
            nomSourceurCell = hderRow
                    .createCell(2 * technologies.size() + 4);
            nomSourceurCell
                    .setCellValue(u3.getNom() + " " + u3.getPrenom());
            nomSourceurCell.setCellStyle(csNomSrc);
            sh.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1,
                    2 * technologies.size() + 4,
                    2 * technologies.size() + 5));
        }
        if (u1 != null) {
            Row technoRow = sh.createRow(rowNum++);
            Cell technoCell;
            for (int l = 0; l < technologies.size(); l++) {
                if (u1 != null) {
                    technoCell = technoRow.createCell(l + 1);
                    technoCell.setCellValue(technologies.get(l)
                            .getLibelle());
                    technoCell.setCellStyle(csCellTechno);
                    sh.setColumnWidth(technoCell.getColumnIndex(),
                            (short) (50 * 80));
                }
                if (u2 != null) {
                    technoCell = technoRow.createCell(l
                            + technologies.size() + 2);
                    technoCell.setCellValue(technologies.get(l)
                            .getLibelle());
                    technoCell.setCellStyle(csCellTechno);
                    sh.setColumnWidth(technoCell.getColumnIndex(),
                            (short) (50 * 80));
                }
                if (u3 != null) {
                    technoCell = technoRow.createCell(l + 2
                            * technologies.size() + 3);
                    technoCell.setCellValue(technologies.get(l)
                            .getLibelle());
                    technoCell.setCellStyle(csCellTechno);
                    sh.setColumnWidth(technoCell.getColumnIndex(),
                            (short) (50 * 80));
                }
            }
        }
        long val = 0;
        Row villeRow;
        for (int m = 0; m < regions.size(); m++) {
            villeRow = sh.createRow(rowNum++);
            Cell cellVille;
            cellVille = villeRow.createCell(0);
            cellVille.setCellValue(regions.get(m).getCode());
            cellVille.setCellStyle(csCellVille);
            sh.setColumnWidth(cellVille.getColumnIndex(),
                    (short) (50 * 150));
            Cell cellTechno;
            if (u1 != null) {

                listeU1 = map.get(u1);
                for (int l = 0; l < technologies.size(); l++) {
                    Boolean foundU1 = false;
                    int sommeRegionU1 = 0;

                    Boolean foundU2 = false;
                    int sommeRegionU2 = 0;

                    Boolean foundU3 = false;
                    int sommeRegionU3 = 0;
                    for (SyntheseCandidatDto candidatDtoU1 : listeU1) {
                        if (candidatDtoU1.getRegion().equals(
                                regions.get(m).getCode())
                                && candidatDtoU1
                                        .getTechnologie()
                                        .getLibelle()
                                        .equals(technologies.get(l)
                                                .getLibelle())) {
                            sommeRegionU1 += candidatDtoU1
                                    .getTotalCandidat();
                            foundU1 = true;
                        }
                    }
                    if (u2 != null) {
                        listeU2 = map.get(u2);
                        for (SyntheseCandidatDto candidatDtoU2 : listeU2) {
                            if (candidatDtoU2.getRegion().equals(
                                    regions.get(m).getCode())
                                    && candidatDtoU2
                                            .getTechnologie()
                                            .getLibelle()
                                            .equals(technologies.get(l)
                                                    .getLibelle())) {
                                sommeRegionU2 += candidatDtoU2
                                        .getTotalCandidat();
                                foundU2 = true;
                            }
                        }
                        if (u3 != null) {
                            listeU3 = map.get(u3);
                            for (SyntheseCandidatDto candidatDtoU3 : listeU3) {
                                if (candidatDtoU3.getRegion().equals(
                                        regions.get(m).getCode())
                                        && candidatDtoU3
                                                .getTechnologie()
                                                .getLibelle()
                                                .equals(technologies.get(l)
                                                        .getLibelle())) {
                                    val = candidatDtoU3.getTotalCandidat();
                                    sommeRegionU3 += candidatDtoU3
                                            .getTotalCandidat();
                                    foundU3 = true;
                                }
                            }
                        }
                        if (foundU1) {
                            cellTechno = villeRow.createCell(l + 1);
                            cellTechno.setCellValue(sommeRegionU1 + "");
                            cellTechno.setCellStyle(csCellNumber);
                        } else {
                            cellTechno = villeRow.createCell(l + 1);
                            cellTechno.setCellValue("-");
                            cellTechno.setCellStyle(csCellNumber);
                        }

                        if (u2 != null) {
                            if (foundU2) {
                                cellTechno = villeRow.createCell(l
                                        + technologies.size() + 2);
                                cellTechno.setCellValue(sommeRegionU2 + "");
                                cellTechno.setCellStyle(csCellNumber);
                            } else {
                                cellTechno = villeRow.createCell(l
                                        + technologies.size() + 2);
                                cellTechno.setCellValue("-");
                                cellTechno.setCellStyle(csCellNumber);
                            }
                        }

                        if (u3 != null) {
                            if (foundU3) {
                                cellTechno = villeRow.createCell(l + 2
                                        * technologies.size() + 3);
                                cellTechno.setCellValue(sommeRegionU3 + "");
                                cellTechno.setCellStyle(csCellNumber);
                            } else {
                                cellTechno = villeRow.createCell(l + 2
                                        * technologies.size() + 3);
                                cellTechno.setCellValue("-");
                                cellTechno.setCellStyle(csCellNumber);
                            }
                        }

                    }
                }
            }
        }
        if (u1 != null) {
            listeU1 = map.get(u1);
            Row totRow = sh.createRow(rowNum++);
            Cell totCell;
            Cell cellHdrVill1;
            sh.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum, 0,
                    0));
            cellHdrVill1 = totRow.createCell(0);
            cellHdrVill1.setCellValue("Total");
            cellHdrVill1.setCellStyle(csCellTot);

            for (int l = 0; l < technologies.size(); l++) {
                int sumtechU1 = 0;
                int sumtechU2 = 0;
                int sumtechU3 = 0;
                for (SyntheseCandidatDto syntheseCandidatDto : listeU1) {
                    if (syntheseCandidatDto.getTechnologie().equals(
                            technologies.get(l))) {
                        sumtechU1 += syntheseCandidatDto.getTotalCandidat();
                    }
                }
                totCell = totRow.createCell(l + 1);
                totCell.setCellValue(sumtechU1);
                totCell.setCellStyle(csCellTot);

                if (u2 != null) {
                    listeU2 = map.get(u2);
                    for (SyntheseCandidatDto syntheseCandidatDto : listeU2) {

                        if (syntheseCandidatDto.getTechnologie().equals(
                                technologies.get(l))) {
                            sumtechU2 += syntheseCandidatDto
                                    .getTotalCandidat();

                        }
                    }
                    totCell = totRow
                            .createCell(l + technologies.size() + 2);
                    totCell.setCellValue(sumtechU2);
                    totCell.setCellStyle(csCellTot);

                }
                if (u3 != null) {
                    listeU3 = map.get(u3);
                    for (SyntheseCandidatDto syntheseCandidatDto : listeU3) {

                        if (syntheseCandidatDto.getTechnologie().equals(
                                technologies.get(l))) {
                            sumtechU3 += syntheseCandidatDto
                                    .getTotalCandidat();
                        }
                    }
                    totCell = totRow.createCell(l + 2 * technologies.size()
                            + 3);
                    totCell.setCellValue(sumtechU3);
                    totCell.setCellStyle(csCellTot);
                }
                totaleU1 += sumtechU1;
                totaleU2 += sumtechU2;
                totaleU3 += sumtechU3;
            }
        }
        Row totUserRow = sh.createRow(rowNum++);
        Cell totCell = totUserRow.createCell(0);
        totCell.setCellValue("");

        totCell.setCellStyle(csCellTot);
        Cell totCellValue = totUserRow.createCell(1);
        totCellValue.setCellValue(totaleU1);
        totCellValue.setCellStyle(csCellTot);

        totCell = totUserRow.createCell(technologies.size() + 2);
        totCell.setCellValue(totaleU2);
        totCell.setCellStyle(csCellTot);

        totCellValue = totUserRow.createCell(2 * technologies.size() + 3);
        totCellValue.setCellValue(totaleU3);
        totCellValue.setCellStyle(csCellTot);
        rowNum++;
    }
for(int i=0,j=1,k=2;k=3){
u1=利用者获得(i);
u2=利用率。获取(j);
u3=利用率。获取(k);
}else if(usilizateurs.size()-i==2){
u1=利用者获得(i);
u2=利用率。获取(j);
}else if(usilizateurs.size()-i==1){
u1=利用者获得(i);
}
行hderRow;
细胞hdrCell;
细胞形态;
hderRow=sh.createRow(rowNum++);
如果(u1!=null){
hdrCell=hderRow.createCell(0);
setCellValue(“Sourceur:”);
hdrCell.setCellStyle(soustire);
nomsourcell=hderRow.createCell(1);
nomSourceurCell.setCellValue(使用URS.get(i).getNom()+“”
+获取(i.getPrenom());
nomsourcell.setCellStyle(csNomSrc);
sh.addMergedRegion(新的CellRangeAddress(rowNum-1,rowNum-1,
1, 2));
}
如果(u2!=null){
Cell Cell=hderRow.createCell(technologies.size()+2);
cell.setCellValue(“Sourceur:”);
cell.setCellStyle(soustire);
nomsourcell=hderRow.createCell(technologies.size()+3);
诺姆塞尔
.setCellValue(u2.getNom()+“”+u2.getPrenom());
nomsourcell.setCellStyle(csNomSrc);
sh.addMergedRegion(新的CellRangeAddress(rowNum-1,rowNum-1,
technologies.size()+3,technologies.size()+4);
}
如果(u3!=null){
Cell-cellSrc=hderRow.createCell(2*technologies.size()+3);
cellSrc.setCellValue(“Sourceur:”);
cellSrc.setCellStyle(soustire);
nomsourcell=hderRow
.createCell(2*technologies.size()+4);
诺姆塞尔
.setCellValue(u3.getNom()+“”+u3.getPrenom());
nomsourcell.setCellStyle(csNomSrc);
sh.addMergedRegion(新的CellRangeAddress(rowNum-1,rowNum-1,
2*技术。尺寸()+4,
2*technologies.size()+5));
}
如果(u1!=null){
Row technoRow=sh.createRow(rowNum++);
细胞技术细胞;
对于(int l=0;l