首页 文章

使用Apache POI设置日期格式

提问于
浏览
14

我想用Apache POI在excel文件中设置日期格式的日期 . 该值将以这样的方式设置,使得在地址栏中它将以mm / dd / YYYY显示,在单元格中将以dd-mmm显示(数字中的日期和字符串中的月份,如01-Jan) . 在这种情况下你能帮我吗?

1 回答

  • 18

    您可以将 HSSFCellStyle 应用于需要填充的单元格 . 这里有一些来自我过去作品的代码片段,它并不完整,但显示了基本的想法:

    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    
    SimpleDateFormat datetemp = new SimpleDateFormat("yyyy-MM-dd");
    Date cellValue = datetemp.parse("1994-01-01 12:00");
    cell.setCellValue(cellValue);
    
    //binds the style you need to the cell.
    HSSFCellStyle dateCellStyle = wb.createCellStyle();
    short df = wb.createDataFormat().getFormat("dd-mmm");
    dateCellStyle.setDataFormat(df);
    cell.setCellStyle(dateCellStyle);
    

    有关JDK中日期格式的更多信息,您应该阅读:http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

相关问题