簡體   English   中英

如何在Apache POI中使用Rowiterator?

[英]How to use rowiterator in apache poi?

我正在尋找一種創建.xlsx文件以從數據庫進行報告的方法。 問題是,當我嘗試返回文件的第一行時,數據將被覆蓋。 有人對Apache Poi有同樣的問題嗎? 我想在同一級別創建三個表。 通過在同一時間逐行同時創建所有表,我已經找到了解決該問題的方法,但是由於我的代碼中有很多循環,所以這花費了很多時間。 我也嘗試過使用getRow(i),但是它不再有用了。 我需要一些幫助。

我已經創建了如下的.xlsx文件

AND這是我通過獲取第一行來優化的代碼的一部分:

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

內容是:每個表的創建/編輯行都從0開始,並且僅累加了單元格索引; 那么您只需要3個表的3個“ for”循環即可;

需要完成工作的步驟:1.通過函數getRow獲取行; 2.檢查行,如果為空,則通過createRow函數創建行;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM