我想使用 Apache POI 在 Excel 文件中以日期格式设置日期。该值将以这样的方式设置,以便在地址栏中显示为 mm/dd/YYYY,在单元格中显示为 dd-mmm(数字日期和月份缩写:01-Jan)。
最佳答案
您可以将 CellStyle
应用于您需要填充的单元格。这是我过去工作的一些代码片段,它并不完整,但显示了基本思想:
Row row = sheet.createRow(0);
Cell cell = row.createCell((short) 0);
cell.setCellType(Cell.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.
CellStyle 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
关于java - 使用 Apache POI 设置日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14314620/