在Excel中使用java填充字段HSSF POI – excel不会正确sortingdate字段

所以我正在使用Java的HSSF POI填充一个Excel文档,我正在使用一个Excel模板,其中有一些头文件已经进入它像这样…

HSSFWorkbook workbook = readFile(TEMPLATE_LOCATION); 

而我的问题是,当我用MM / dd / yyyy这样的date格式填充其中一列的数据时…

 row.createCell((short)column++).setCellValue(Tools.dateToString(rfq.getCreationDate())); 

它适当地填充数据列如01/01/2011 05/04/2010 03/03/2009

错误是当我在这个列上执行sorting(在Excel中)使用说一个自动filter – >升序sorting然后它返回错误的顺序date,就像这样….

01/01/2011

03/03/2009

05/04/2010

(因为它像string一样读取,而不是按datesorting)

我试图将列设置为“数字”列,然后sorting仍然没有骰子….

  cell = row.createCell((short)column++); cell.setCellStyle(workbook.createCellStyle()); cell.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_RIGHT); cell.setCellType(cell.CELL_TYPE_NUMERIC); cell.setCellValue(Tools.dateToString(rfq.getCreationDate())); 

也格式化这种方式以及没有帮助…

 SimpleDateFormat formatter = new SimpleDateFormat("M/d/yyyy"); row.createCell((short)column++).setCellValue(formatter.format(order.getCreationDate())); 

这一切都在Excel 2003中进行。不知道如何解决。

你的问题是,当你填充单元格时,你将数据转换为string

 row.createCell((short)column++).setCellValue(Tools.dateToString(rfq.getCreationDate())); 

我不是一个Java用户,所以不知道,但你可以试试

 row.createCell((short)column++).setCellValue(rfq.getCreationDate()); 

如果基础数据是以Excel可以识别的date的forms出现的话,那应该没问题。