繁体   English   中英

Apache POI 日期格式部分有效

[英]Apache POI Date format partially working

我正在使用 poi 3.8 创建 excel 表

我有一些日期列,我正在使用以下代码进行格式化

CreationHelper ch = wb.getCreationHelper();
short df = ch.createDataFormat().getFormat("MM/dd/yyyy");
System.out.println(df);
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(df);

Cell cell = sheet.createRow(0).createCell(0);

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    cell.setCellValue( sdf.parse("04/30/2013") );
    sheet.setColumnWidth(0, 6000);
    cell.setCellStyle(cs);

但问题是 poi 没有格式化某些日期单元格,而不是以“MM/dd/yyyy”格式显示日期,而是将日期显示为数字值,如 451235

这里要提到的另一件事是,我使用预创建的样式和 setDataFormat(df) 方法调用日期和非日期列的默认格式

.setCellStyle(...)调用不是附加的。 也就是说,如果您应用日期格式样式:

cell.setCellStyle(cs);

然后应用一些其他样式:

cell.setCellStyle(thickBorderStyle)

您将删除日期格式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM