Merge pull request #31 from LQYBill/fix/invoiceExcelBug

fix: Excel display bug in invoices
pull/6221/head
Qiuyi LI 2023-09-15 11:21:50 +02:00 committed by GitHub
commit 8b32a17a00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 25 deletions

View File

@ -156,28 +156,49 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
// if the number of rows of data is greater than the available space in the default template // if the number of rows of data is greater than the available space in the default template
// we shift down the rows after the table and we clone the row in the table // we shift down the rows after the table and we clone the row in the table
int dataRowNumber = LAST_ROW - FIRST_ROW; int dataRowNumber = LAST_ROW - FIRST_ROW;
int additionalRowNum = data.size() - dataRowNumber - 1; int additionalRowNum = Math.max(data.size() - dataRowNumber - 1, 0);
TOTAL_ROW = LAST_ROW+additionalRowNum; TOTAL_ROW = LAST_ROW + additionalRowNum;
Sheet sheet = factory.getWorkbook().getSheetAt(0); Sheet sheet = factory.getWorkbook().getSheetAt(0);
org.apache.poi.ss.usermodel.Row sourceRow = sheet.getRow(FIRST_ROW); org.apache.poi.ss.usermodel.Row sourceRow = sheet.getRow(FIRST_ROW);
if(data.size() > dataRowNumber) int footerRow = TOTAL_ROW + 1; // la ligne à laquelle le footer commence (1 ligne avant le total)
int imgShift = additionalRowNum; // le nombre de ligne qu'on va décaler les images (signatures et tampon)
if(data.size() > dataRowNumber + 1)
{ {
int startRow = LAST_ROW+1; int startRow = LAST_ROW+1;
int fileLastRow = sheet.getLastRowNum(); int fileLastRow = sheet.getLastRowNum();
// shifting the footer of the file, to X rows below // shifting the footer of the file, to X rows below
// making sure the whole footer is in the same page (13 lines) and we fill the end of page with blank data lines // making sure the whole footer is in the same page (13 lines) and we fill the end of page with blank data lines
if(additionalRowNum%PAGE_ROW_MAX <= 13) {
TOTAL_ROW = PAGE_ROW_MAX-2; // si le nombre de lignes de data rentre dans 1 page A4
sheet.shiftRows(startRow, fileLastRow, PAGE_ROW_MAX - LAST_ROW, true, false); if(data.size() < 44) {
if(TOTAL_ROW > LAST_ROW + 3) { // s'il ne reste pas assez de place pour le footer
// on shift le footer à la page suivante (total + signature etc..)
sheet.shiftRows(startRow, fileLastRow, PAGE_ROW_MAX - LAST_ROW - 1, true, false);
footerRow = PAGE_ROW_MAX - LAST_ROW + 1;
imgShift = PAGE_ROW_MAX - LAST_ROW + 1;
TOTAL_ROW = PAGE_ROW_MAX;
}
else {
sheet.shiftRows(startRow, fileLastRow, additionalRowNum, true, false);
footerRow = additionalRowNum + 1;
TOTAL_ROW = LAST_ROW + additionalRowNum+1;
}
} }
else { else {// on dépasse forcément le format A4 d'un PDF
// +6 because if we use US template there's one more row if(((TOTAL_ROW - 44) % 63) < 13) {
sheet.shiftRows(startRow, fileLastRow, additionalRowNum, true, false); sheet.shiftRows(startRow, fileLastRow, TOTAL_ROW - LAST_ROW + ((TOTAL_ROW-44)%63), true, false);
footerRow = additionalRowNum + ((TOTAL_ROW-44)%63) + 1;
imgShift = TOTAL_ROW-44 + ((TOTAL_ROW-44)%63) -1;
TOTAL_ROW += ((TOTAL_ROW-44)%63) + 1;
}
else {
sheet.shiftRows(startRow, fileLastRow, TOTAL_ROW - LAST_ROW, true, false);
footerRow = TOTAL_ROW - LAST_ROW +1;
}
} }
// inserting new rows after row 42 // inserting new rows after row 42
for(int i = 0; i < (data.size() - dataRowNumber - 1 <= 13 ? PAGE_ROW_MAX-LAST_ROW+1 : additionalRowNum+1); i++) { for(int i = 0; i < footerRow; i++) {
sheet.createRow(startRow-1 + i); sheet.createRow(startRow-1 + i);
org.apache.poi.ss.usermodel.Row newRow = sheet.getRow(startRow-1 + i); org.apache.poi.ss.usermodel.Row newRow = sheet.getRow(startRow-1 + i);
newRow.setHeight(sourceRow.getHeight()); newRow.setHeight(sourceRow.getHeight());
@ -192,7 +213,7 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
cellStyle.setBorderLeft(BorderStyle.DOUBLE); cellStyle.setBorderLeft(BorderStyle.DOUBLE);
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
} }
if(startRow + i < PAGE_ROW_MAX-2) { if((startRow + i <= TOTAL_ROW && data.size() >= 44) || (startRow + i <= PAGE_ROW_MAX - 1 && data.size() < 44)) {
if (j >= 2 && j <= 7) { if (j >= 2 && j <= 7) {
middleCellStyle.setBorderLeft(BorderStyle.THIN); middleCellStyle.setBorderLeft(BorderStyle.THIN);
middleCellStyle.setBorderRight(BorderStyle.THIN); middleCellStyle.setBorderRight(BorderStyle.THIN);
@ -209,11 +230,14 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
XSSFPicture picture = (XSSFPicture)shape; XSSFPicture picture = (XSSFPicture)shape;
XSSFClientAnchor anchor = picture.getClientAnchor(); XSSFClientAnchor anchor = picture.getClientAnchor();
anchor.setRow1(data.size() - dataRowNumber - 1 <= 13 ? anchor.getRow1() + PAGE_ROW_MAX-LAST_ROW : anchor.getRow1() + additionalRowNum); anchor.setRow1(anchor.getRow1() + imgShift);
anchor.setRow2(data.size() - dataRowNumber - 1 <= 13 ? anchor.getRow2() + PAGE_ROW_MAX-LAST_ROW : anchor.getRow2() + additionalRowNum); anchor.setRow2(anchor.getRow2() + imgShift);
} }
} }
} }
else {
TOTAL_ROW = LAST_ROW + additionalRowNum + 1;
}
// table section // table section
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
lineNum = i + FIRST_ROW; lineNum = i + FIRST_ROW;
@ -298,16 +322,16 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
totalDueCellStyle.setBorderTop(BorderStyle.THIN); totalDueCellStyle.setBorderTop(BorderStyle.THIN);
totalDueCellStyle.setFont(arialBold); totalDueCellStyle.setFont(arialBold);
if(additionalRowNum%PAGE_ROW_MAX <= 13) { if(((LAST_ROW+additionalRowNum - 44) % 63) < 13 && ((LAST_ROW+additionalRowNum - 44) % 63) > 0) {
totalDueRow = sheet.getRow(PAGE_ROW_MAX + 2); totalDueRow = sheet.getRow( data.size() < 44 ? PAGE_ROW_MAX + 1 : TOTAL_ROW + 1);
Cell totalDueCell = totalDueRow.createCell(7); Cell totalDueCell = totalDueRow.createCell(7);
totalDueCell.setCellFormula("H" + (PAGE_ROW_MAX - 2) + "-G" + (PAGE_ROW_MAX - 2)); totalDueCell.setCellFormula("H" + (TOTAL_ROW) + "-G" + (TOTAL_ROW));
totalDueCell.setCellStyle(totalDueCellStyle); totalDueCell.setCellStyle(totalDueCellStyle);
} }
else { else {
totalDueRow = sheet.getRow(TOTAL_ROW + 2); totalDueRow = sheet.getRow(data.size() < 44 ? TOTAL_ROW + 1 : TOTAL_ROW + 2);
Cell totalDueCell = totalDueRow.createCell(7); Cell totalDueCell = totalDueRow.createCell(7);
totalDueCell.setCellFormula("H" + (TOTAL_ROW+1) + "-G" + (TOTAL_ROW+1)); totalDueCell.setCellFormula("H" + (data.size() < 44 ? TOTAL_ROW : TOTAL_ROW + 1) + "-G" + (data.size() < 44 ? TOTAL_ROW : TOTAL_ROW + 1));
totalDueCell.setCellStyle(totalDueCellStyle); totalDueCell.setCellStyle(totalDueCellStyle);
} }
} }
@ -315,13 +339,13 @@ public abstract class AbstractInvoice<E, F, G, H, I> {
if (targetClient.getCurrency().equals("USD")) { if (targetClient.getCurrency().equals("USD")) {
org.apache.poi.ss.usermodel.Row dollarRow; org.apache.poi.ss.usermodel.Row dollarRow;
String formula; String formula;
if (additionalRowNum % PAGE_ROW_MAX <= 13) { if ((((LAST_ROW + additionalRowNum - 44) % 63) < 13) && ((LAST_ROW + additionalRowNum - 44) % 63) > 0) {
dollarRow = sheet.getRow(TOTAL_ROW + 5); dollarRow = sheet.getRow(TOTAL_ROW + 2);
formula = "H"+ (TOTAL_ROW+5) +" *" + exchangeRate; formula = "H"+ (TOTAL_ROW + 2) +" *" + exchangeRate;
} }
else { else {
dollarRow = sheet.getRow(TOTAL_ROW + 3); dollarRow = sheet.getRow(data.size() >= 44 ? TOTAL_ROW + 3 : TOTAL_ROW + 2);
formula = "H" + (TOTAL_ROW + 3) + " *" + exchangeRate; formula = "H" + (data.size() >= 44 ? TOTAL_ROW + 3 : TOTAL_ROW + 2) + " *" + exchangeRate;
} }
Cell dollarCell = dollarRow.createCell(7); // column H Cell dollarCell = dollarRow.createCell(7); // column H
CellStyle cellStyle = factory.getWorkbook().createCellStyle(); CellStyle cellStyle = factory.getWorkbook().createCellStyle();

View File

@ -92,7 +92,7 @@ public class InvoiceStyleFactory {
rightSideDecimalStyle.setVerticalAlignment(VerticalAlignment.CENTER); rightSideDecimalStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// decimal // decimal
// DataFormat format =workbook.createDataFormat(); // DataFormat format =workbook.createDataFormat();
leftSideStyle.setDataFormat(creationHelper.createDataFormat().getFormat("#,##0.00")); rightSideDecimalStyle.setDataFormat(creationHelper.createDataFormat().getFormat("#,##0.00"));
// font // font
Font font = workbook.createFont(); Font font = workbook.createFont();
font.setFontName("Arial"); font.setFontName("Arial");