Java 在单元格上使用apache poi DataFormatter时获取错误的日期格式

Java 在单元格上使用apache poi DataFormatter时获取错误的日期格式,java,apache-poi,excel-2002,Java,Apache Poi,Excel 2002,我使用ApachePOI数据格式化程序类来获取与excel工作表中给定的相同的日期。但在下面的例子中,它失败了 例如,如果我给出的日期是2017年9月19日,那么它就是2017年9月19日 我用下面的代码得到的日期值与excel表格中给出的相同 DataFormatter df = new DataFormatter(); String stringCellValue = df.formatCellValue(cell).toString(); 此代码打印2017年12月19日的2017年12

我使用ApachePOI数据格式化程序类来获取与excel工作表中给定的相同的日期。但在下面的例子中,它失败了

例如,如果我给出的日期是2017年9月19日,那么它就是2017年9月19日

我用下面的代码得到的日期值与excel表格中给出的相同

DataFormatter df = new DataFormatter();
String stringCellValue = df.formatCellValue(cell).toString();

此代码打印2017年12月19日的
2017年12月19日的
,以及2017年9月19日的
2017年9月19日的
。也许您可以调整我的代码以使您的代码正常工作。

如果包含日期的单元格被格式化为默认日期格式(短日期),则文件中仅存储格式id 0xE(14)。因此,
DataFormatter
没有机会确定特殊的
Excel
如何显示此日期,因为没有DataFormatter字符串。看见
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = wb.getSheetAt(0);  
XSSFRow row = sheet.getRow(0);
XSSFCell cell = row.getCell((short)0);
short x = wb.createDataFormat().getFormat("dd/m/yyyy;@");
CellStyle dateCellFormat = wb.createCellStyle();
dateCellFormat.setDataFormat(x);

cell.setCellStyle(dateCellFormat);
DataFormatter df = new DataFormatter();
String stringCellValue = df.formatCellValue(cell).toString();
System.out.println(stringCellValue);