|
15 | 15 |
|
16 | 16 | package eu.esdihumboldt.hale.io.xls;
|
17 | 17 |
|
| 18 | +import java.time.LocalDateTime; |
| 19 | +import java.time.ZoneId; |
| 20 | +import java.time.format.DateTimeFormatter; |
| 21 | +import java.util.Date; |
| 22 | + |
18 | 23 | import org.apache.poi.ss.usermodel.Cell;
|
19 | 24 | import org.apache.poi.ss.usermodel.CellType;
|
20 | 25 | import org.apache.poi.ss.usermodel.CellValue;
|
| 26 | +import org.apache.poi.ss.usermodel.DateUtil; |
21 | 27 | import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
22 | 28 | import org.apache.poi.ss.usermodel.Row;
|
23 | 29 | import org.apache.poi.ss.usermodel.Sheet;
|
|
30 | 36 | */
|
31 | 37 | public class XLSUtil {
|
32 | 38 |
|
| 39 | + /** |
| 40 | + * Parameter for the reader specifying how values imported from date cells |
| 41 | + * should be formatted. |
| 42 | + */ |
| 43 | + public static final String PARAMETER_DATE_FORMAT = "yyyy-MM-dd"; |
| 44 | + |
33 | 45 | /**
|
34 | 46 | * Extract the text from a given cell. Formulas are evaluated, for blank or
|
35 | 47 | * error cells <code>null</code> is returned
|
@@ -65,11 +77,30 @@ public static String extractText(Cell cell, FormulaEvaluator evaluator, Sheet sh
|
65 | 77 | case BOOLEAN:
|
66 | 78 | return String.valueOf(value.getBooleanValue());
|
67 | 79 | case NUMERIC:
|
68 |
| - double number = value.getNumberValue(); |
69 |
| - if (number == Math.floor(number)) { |
70 |
| - return String.valueOf((int) number); |
| 80 | + if (DateUtil.isCellDateFormatted(cell)) { |
| 81 | + // Get the date value from the cell |
| 82 | + Date dateCellValue = cell.getDateCellValue(); |
| 83 | + |
| 84 | + // Convert java.util.Date to java.time.LocalDateTime |
| 85 | + LocalDateTime localDateTime = dateCellValue.toInstant() |
| 86 | + .atZone(ZoneId.systemDefault()).toLocalDateTime(); |
| 87 | + |
| 88 | + // Define a DateTimeFormatter with a specific pattern |
| 89 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PARAMETER_DATE_FORMAT); |
| 90 | + |
| 91 | + // Format LocalDateTime using DateTimeFormatter |
| 92 | + String formattedDate = localDateTime.format(formatter); |
| 93 | + |
| 94 | + return formattedDate; |
| 95 | + } |
| 96 | + else { |
| 97 | + double number = value.getNumberValue(); |
| 98 | + if (number == Math.floor(number)) { |
| 99 | + return String.valueOf((int) number); |
| 100 | + } |
| 101 | + |
| 102 | + return String.valueOf(value.getNumberValue()); |
71 | 103 | }
|
72 |
| - return String.valueOf(value.getNumberValue()); |
73 | 104 | case STRING:
|
74 | 105 | return value.getStringValue();
|
75 | 106 | default:
|
|
0 commit comments