Set Date format using Apache POI

后端 未结 2 1819
情深已故
情深已故 2021-02-19 04:58

I want to set date in date format in excel file with Apache POI. The value will set in such a manner so that in Address Bar it will show in mm/dd/YYYY and in cell it will show i

相关标签:
2条回答
  • 2021-02-19 05:29

    Best approach is:

    HSSFDataFormatter hdf = new HSSFDataFormatter();
    System.out.println (hdf.formatCellValue(mycell));
    

    This worked completely fine with me. This piece of code simply returns the cell in its raw form, no change at all in its format.

    0 讨论(0)
  • 2021-02-19 05:34

    You can apply a HSSFCellStyle to the cell you need to fill. Here some code snippets from my past work, it's not intact but shows the basic idea:

    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);
    

    More infomation about date format in JDK, you should read this: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

    0 讨论(0)
提交回复
热议问题