【发布时间】:2012-12-28 04:29:30
【问题描述】:
我想用 Apache POI 在 Excel 文件中以日期格式设置日期。该值将以这样的方式设置,以便在地址栏中显示为 mm/dd/YYYY,在单元格中显示为 dd-mmm(数字日期和月份缩写:01-Jan)。
【问题讨论】:
-
请注意“地址栏”格式取决于您的区域设置,而不是 xls(x) 中的任何内容
标签: java date apache-poi
我想用 Apache POI 在 Excel 文件中以日期格式设置日期。该值将以这样的方式设置,以便在地址栏中显示为 mm/dd/YYYY,在单元格中显示为 dd-mmm(数字日期和月份缩写:01-Jan)。
【问题讨论】:
标签: java date apache-poi
您可以将CellStyle 应用于您需要填写的单元格。这是我过去工作中的一些代码sn-ps,它不完整但显示了基本思想:
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
【讨论】: