简体   繁体   中英

How to use rowiterator in apache poi?

I'm looking for a way to create .xlsx file for reporting from database. The problem is that data are being overwritten when I try to return back to the first row of the file. Did anyone have the same problem with apache poi? I would like to create three tables in the same level. I have found a solution for the problem by creating all the tables at the same time row by row but it takes a lot of time as I have many loops in my code. I have also tried to use getRow(i) but it didn't help anymore. I need some help please.

我已经创建了如下的.xlsx文件

AND This is part of code I went to optimize by getting first row:

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++;
    }

What's about: create/edit rows begin 0 for each table, and just the cell index accumulated; then you just need 3 'for' loops for 3 tables;

Steps need to complete the work: 1. get the row, by function getRow; 2. check the row, if null, create row by createRow function;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM