Apache POI日期格式部分起作用 [英] Apache POI Date format partially working

查看:139
本文介绍了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);

然后应用其他样式:

cell.setCellStyle(thickBorderStyle)

您将删除日期格式

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

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