在Excel中显示date表单xlsx在Java中

我将date保存为Excel表格(xlsx)中的一个单元格。 虽然它正在向我展示像2013年6月174日

守则是

String ret=""; if(c.getCellType()==Cell.CELL_TYPE_NUMERIC) { if(col==0) { ret=new java.text.DecimalFormat("0").format(c.getNumericCellValue()); } else if(col==8) { if(HSSFDateUtil.isCellDateFormatted(c)) { DateFormat df=new SimpleDateFormat("DD-MMM-YYYY"); ret=df.format(c.getDateCellValue()); } } else { ret=""+c.getNumericCellValue(); } } else if(c.getCellType()==Cell.CELL_TYPE_STRING) { ret=""+c.getStringCellValue(); } return ret; 

请帮助我。

提前致谢。

使用:

 DateFormat df=new SimpleDateFormat("dd-MMM-yyyy"); 

代替

 DateFormat df=new SimpleDateFormat("DD-MMM-YYYY"); 

因为:

D =每年的dated =每月的date

检查你的date模式:DD / MMM / YYYY应该解决你的问题。

这里是你可以find信息如何正确build立date模式的文档。

为什么不请求Apache POI为您做格式化? 它将读取Excel中单元格上应用的格式规则,然后根据这些规则格式化date

您可以使用Apache POI中的DataFormatter类来执行此操作。 给一个单元格,它将读取格式规则,理解它们,然后格式化单元格的值

你所需要做的就是:

 DataFormatter fmt = new DataFormatter(); String ret = fmt.formatCellValue(cell); 

这适用于数字,date,百分比,作品!

尝试改变

 DateFormat df=new SimpleDateFormat("DD-MMM-YYYY"); ret=df.format(c.getDateCellValue()); 

通过

 DateFormat df=new SimpleDateFormat("dd-MMM-YYYY"); ret=df.format(c.getDateCellValue());