BAEL-7278 create getCellText method to reduce duplicated code.
This commit is contained in:
parent
29c0b906b5
commit
1d9a0246fe
|
@ -88,24 +88,7 @@ public class ExcelToPDFConverter {
|
||||||
Row headerRow = worksheet.getRow(0);
|
Row headerRow = worksheet.getRow(0);
|
||||||
for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {
|
for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {
|
||||||
Cell cell = headerRow.getCell(i);
|
Cell cell = headerRow.getCell(i);
|
||||||
|
String headerText = getCellText(cell);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
PdfPCell headerCell = new PdfPCell(new Phrase(headerText, getCellStyle(cell)));
|
PdfPCell headerCell = new PdfPCell(new Phrase(headerText, getCellStyle(cell)));
|
||||||
setBackgroundColor(cell, headerCell);
|
setBackgroundColor(cell, headerCell);
|
||||||
setCellAlignment(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 {
|
private static void addTableData(XSSFSheet worksheet, PdfPTable table) throws DocumentException, IOException {
|
||||||
Iterator<Row> rowIterator = worksheet.iterator();
|
Iterator<Row> rowIterator = worksheet.iterator();
|
||||||
while (rowIterator.hasNext()) {
|
while (rowIterator.hasNext()) {
|
||||||
|
@ -122,14 +122,7 @@ public class ExcelToPDFConverter {
|
||||||
}
|
}
|
||||||
for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
|
for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
|
||||||
Cell cell = row.getCell(i);
|
Cell cell = row.getCell(i);
|
||||||
String cellValue;
|
String cellValue = getCellText(cell);
|
||||||
if (cell.getCellType() == CellType.STRING) {
|
|
||||||
cellValue = cell.getStringCellValue();
|
|
||||||
} else if (cell.getCellType() == CellType.NUMERIC) {
|
|
||||||
cellValue = String.valueOf(cell.getNumericCellValue());
|
|
||||||
} else {
|
|
||||||
cellValue = "";
|
|
||||||
}
|
|
||||||
PdfPCell cellPdf = new PdfPCell(new Phrase(cellValue, getCellStyle(cell)));
|
PdfPCell cellPdf = new PdfPCell(new Phrase(cellValue, getCellStyle(cell)));
|
||||||
setBackgroundColor(cell, cellPdf);
|
setBackgroundColor(cell, cellPdf);
|
||||||
setCellAlignment(cell, cellPdf);
|
setCellAlignment(cell, cellPdf);
|
||||||
|
|
|
@ -23,7 +23,7 @@ endobj
|
||||||
<</Type/Catalog/Pages 4 0 R>>
|
<</Type/Catalog/Pages 4 0 R>>
|
||||||
endobj
|
endobj
|
||||||
7 0 obj
|
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
|
endobj
|
||||||
xref
|
xref
|
||||||
0 8
|
0 8
|
||||||
|
@ -36,7 +36,7 @@ xref
|
||||||
0000001186 00000 n
|
0000001186 00000 n
|
||||||
0000001231 00000 n
|
0000001231 00000 n
|
||||||
trailer
|
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
|
%iText-5.5.13.3
|
||||||
startxref
|
startxref
|
||||||
1391
|
1391
|
||||||
|
|
Loading…
Reference in New Issue