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
@@ -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);
+2 -2
View File
@@ -23,7 +23,7 @@ endobj
4 0 obj
<</Type/Pages/Count 1/Kids[5 0 R]>>
endobj
6 0 obj
6 0 obj
<</Type/Catalog/Pages 4 0 R>>
endobj
7 0 obj
@@ -36,7 +36,7 @@ xref
0000001047 00000 n
0000000015 00000 n
0000001135 00000 n
0000000833 00000 n
0000000833 00000 n
0000001186 00000 n
0000001231 00000 n
trailer