简体   繁体   English

如何在Apache POI中使用Rowiterator?

[英]How to use rowiterator in apache poi?

I'm looking for a way to create .xlsx file for reporting from database. 我正在寻找一种创建.xlsx文件以从数据库进行报告的方法。 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? 有人对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. 我也尝试过使用getRow(i),但是它不再有用了。 I need some help please. 我需要一些帮助。

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

AND This is part of code I went to optimize by getting first row: 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++;
    }

What's about: create/edit rows begin 0 for each table, and just the cell index accumulated; 内容是:每个表的创建/编辑行都从0开始,并且仅累加了单元格索引; then you just need 3 'for' loops for 3 tables; 那么您只需要3个表的3个“ for”循环即可;

Steps need to complete the work: 1. get the row, by function getRow; 需要完成工作的步骤:1.通过函数getRow获取行; 2. check the row, if null, create row by createRow function; 2.检查行,如果为空,则通过createRow函数创建行;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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