我正在寻找一种方法来创建.xlsx文件,以便从数据库进行报告。问题是当我尝试返回到文件的第一行时,数据被覆盖。有人和apache poi有同样的问题吗?我想在同一级别创建三个表。我通过逐行创建所有表来找到问题的解决方案,但是由于我的代码中有很多循环,因此需要花费很多时间。我也试过使用getRow(i),但它不再有用了。我需要一些帮助。
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 :(得分:0)
关于:为每个表创建/编辑行开始0,只累积单元格索引;然后你只需要3&#39; for&#39;循环3个表;
步骤需要完成工作: 1.按函数getRow获取行; 2.检查行,如果为null,则通过createRow函数创建行;