2011-05-06 55 views
1

所以我使用Java的HSSF POI填充Excel文檔,我使用了一些已經進入它的頭文件的excel模板。 。使用java填充字段在Excel中HSSF POI - excel沒有正確排序日期字段

HSSFWorkbook workbook = readFile(TEMPLATE_LOCATION); 

而我的問題是,當我在MM的日期格式填充的一列數據/ DD/YYYY這樣的...

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

它適當填充柱像......的數據。 01/01/2011 05/04/2010用說,一個autofilter->升序排列則返回日期順序錯誤,像這樣03/03/2009

的錯誤是,當我在此列(在Excel)執行排序....

01/01/2011

03/03/2009

05/04/2010

(由於它是讀取它像一個串和排序,而不是由一個日期排序)

我試圖將列作爲「數字」列,然後排序仍然沒有骰子....

 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中進行。不知道如何解決。

回答

2

你的問題是,你將數據轉換爲字符串時,您填充細胞

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

我不是一個Java用戶,所以不知道,但你可以嘗試只

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

如果底層數據是以Excel識別爲日期的形式存在,則應該可以。

相關問題