Apache POI 日期格式部分有效 [英] Apache POI Date format partially working

查看:44
本文介绍了Apache POI 日期格式部分有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 poi 3.8 创建 Excel 表格

I am using poi 3.8 to create excel sheet

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

I have some date columns which I am formitting with the below code

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

but the problem is that poi is not formating some of the date cells, instead of showing date in "MM/dd/yyyy" format it is showing date as number value like 451235

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

One more thing to mention here is that I am using pre created styles with setDataFormat(df) method call for date and default format for non-date columns

推荐答案

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

Calls to .setCellStyle(...) are not additive. That is, if you apply a Date format style:

cell.setCellStyle(cs);

然后应用一些其他样式:

and then apply some other style:

cell.setCellStyle(thickBorderStyle)

您将删除日期格式

这篇关于Apache POI 日期格式部分有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆