BAEL-7278 create getCellText method to reduce duplicated code.

This commit is contained in:
SGWebFreelancer 2023-12-13 17:46:25 +08:00
parent 29c0b906b5
commit 1d9a0246fe
2 changed files with 21 additions and 28 deletions

View File

@ -88,24 +88,7 @@ public class ExcelToPDFConverter {
Row headerRow = worksheet.getRow(0);
for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {
Cell cell = headerRow.getCell(i);
String headerText;
switch (cell.getCellType()) {
case STRING:
headerText = cell.getStringCellValue();
break;
case NUMERIC:
headerText = String.valueOf(BigDecimal.valueOf(cell.getNumericCellValue()));
break;
case BLANK:
headerText = ""; // or null
break;
default:
logger.warn("Unsupported cell type: {}", cell.getCellType());
headerText = ""; // or throw an exception
break;
}
String headerText = getCellText(cell);
PdfPCell headerCell = new PdfPCell(new Phrase(headerText, getCellStyle(cell)));
setBackgroundColor(cell, headerCell);
setCellAlignment(cell, headerCell);
@ -113,6 +96,23 @@ public class ExcelToPDFConverter {
}
}
public static String getCellText(Cell cell) {
String cellValue;
switch (cell.getCellType()) {
case STRING:
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
cellValue = String.valueOf(BigDecimal.valueOf(cell.getNumericCellValue()));
break;
case BLANK:
default:
cellValue = "";
break;
}
return cellValue;
}
private static void addTableData(XSSFSheet worksheet, PdfPTable table) throws DocumentException, IOException {
Iterator<Row> rowIterator = worksheet.iterator();
while (rowIterator.hasNext()) {
@ -122,14 +122,7 @@ public class ExcelToPDFConverter {
}
for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
Cell cell = row.getCell(i);
String cellValue;
if (cell.getCellType() == CellType.STRING) {
cellValue = cell.getStringCellValue();
} else if (cell.getCellType() == CellType.NUMERIC) {
cellValue = String.valueOf(cell.getNumericCellValue());
} else {
cellValue = "";
}
String cellValue = getCellText(cell);
PdfPCell cellPdf = new PdfPCell(new Phrase(cellValue, getCellStyle(cell)));
setBackgroundColor(cell, cellPdf);
setCellAlignment(cell, cellPdf);

View File

@ -23,7 +23,7 @@ endobj
<</Type/Catalog/Pages 4 0 R>>
endobj
7 0 obj
<</Producer(iText® 5.5.13.3 ©2000-2022 iText Group NV \(AGPL-version\))/CreationDate(D:20231213103259+08'00')/ModDate(D:20231213103259+08'00')>>
<</Producer(iText® 5.5.13.3 ©2000-2022 iText Group NV \(AGPL-version\))/CreationDate(D:20231213174247+08'00')/ModDate(D:20231213174247+08'00')>>
endobj
xref
0 8
@ -36,7 +36,7 @@ xref
0000001186 00000 n
0000001231 00000 n
trailer
<</Size 8/Root 6 0 R/Info 7 0 R/ID [<f2e21b07de44685166b7f4df9920b8a2><f2e21b07de44685166b7f4df9920b8a2>]>>
<</Size 8/Root 6 0 R/Info 7 0 R/ID [<6a28b1036b62f3808f3bfb62a88a5239><6a28b1036b62f3808f3bfb62a88a5239>]>>
%iText-5.5.13.3
startxref
1391