我正在疯狂地尝试使用Apache POI从 Java 解析 Excel,并且我发现以下问题解析包含日期的特定列。
这是我的 Excel 文件中的列:
如您所见,此列包含日期字段,但此列的某些单元格包含数字值,其他一些单元格包含文本值(我可以使用TYPE() Excel 函数看到它)。我真的不知道这怎么可能,因为所有单元格都包含日期字段。有什么想法?
无论如何,在我的代码中,我试图以这种方式处理这种奇怪的情况:
if(currentRow.getCell(20) != null && !currentRow.getCell(20).toString().trim().isEmpty()) {
if(currentRow.getCell(20).getCellType().toString().equals("STRING")) {
System.out.println("Data sottoscrizione contratto è STRING: " + currentRow.getCell(20).toString());
}
else if(currentRow.getCell(20).getCellType().toString().equals("NUMERIC")) {
System.out.println("Data sottoscrizione contratto è NUMERIC");
String dateAsString = currentRow.getCell(20).toString();
System.out.println("DATA SOTTOSCRIZIONE CONTRATTO: " + currentRow.getCell(20).toString());
}
}
通过这种方式,我可以处理尝试转换为日期的两种情况。
这是我的问题。当它在 if NUMERIC案例中找到一个 Excel 数值时
并通过以下方式打印单元格值:
System.out.println("DATA SOTTOSCRIZIONE CONTRATTO: " + currentRow.getCell(20).toString());
我打印了与日期值16/10/2017相关的值16-ott-2017
这里有一些疑问:为什么我会以这种格式获得16/10/2017 之类的东西。
16-ott-2017应该是日期的意大利语格式。如何将其转换为合适的 Date 对象?