Apache poi 日期格式 [英] Apache poi date format

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

问题描述

我正在读取 csv 文件并获取 01-01-2011 的日期,但是当我使用 apache poi 库编写 .xlsx 文件时,我希望它采用 01-Jan-2011 格式.我的代码是

Hi i am reading csv file and getting date in 01-01-2011 but i want it in 01-Jan-2011 format when i write .xlsx file using apache poi library. my code is

XSSFDataFormat df = workBook.createDataFormat();
cs.setDataFormat(df.getFormat("dd-MMM-yy"));

但它对我不起作用.我哪里做错了.

but it is not working for me. where am i doing mistake.

推荐答案

不仅需要创建单元格格式,还需要将其应用到单元格中!

Not only do you need to create a cell format, but you also need to apply it to the cell!

XSSFDataFormat df = workBook.createDataFormat();
cs.setDataFormat(df.getFormat("d-mmm-yy"));

// Get / Create our cell
XSSFRow row = sheet.createRow(2);
XSSFCell cell = row.createCell(3);

// Set it to be a date
Calendar c = Calendar.getInstance();
c.set(2012,3-1,18); // Don't forget months are 0 based on Calendar
cell.setCellValue( c.getTime() );

// Style it as a date
cell.setCellStyle(cs);

其次,您需要注意 Java 和 Excel 在表达日期格式规则的方式上略有不同.您应该打开一个 Excel 副本,按照您想要的方式设置示例单元格的格式,然后记下所需的格式设置规则.在你的情况下,你选择了 Java 风格的大写 M,而在 Excel 中它是小写(见上文)

Secondly, you need to be aware that Java and Excel differ slightly in how they express Date formatting rules. You should open up a copy of Excel, format a sample cell how you want, then take a note of the formatting rules needed. In your case, you'd gone for a Java style upper case M, while in Excel it's lower case (see above)

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

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