问题描述
我想使用Apache POI在excel文件中设置日期格式。该值将以这种方式设置,以便在地址栏中将以mm / dd / YYYY显示,在单元格中将以dd-mmm(字符串中的日期,字符串中的月份为01年1月)显示。你会在这种情况下帮我吗?
I want to set date in date format in excel file with Apache POI. The value will set in such a manner so that in Address Bar it will show in mm/dd/YYYY and in cell it will show in dd-mmm (Date in numeric and Month in String like 01-Jan). Will you please help me in this situation?
推荐答案
你可以申请一个 HSSFCellStyle
到您需要填充的单元格。这里有一些我过去工作中的代码段,它不完整,但显示了基本思想:
You can apply a HSSFCellStyle
to the cell you need to fill. Here some code snippets from my past work, it's not intact but shows the basic idea:
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.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.
HSSFCellStyle dateCellStyle = wb.createCellStyle();
short df = wb.createDataFormat().getFormat("dd-mmm");
dateCellStyle.setDataFormat(df);
cell.setCellStyle(dateCellStyle);
有关JDK中日期格式的更多信息,您应该阅读以下内容:
More infomation about date format in JDK, you should read this: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
这篇关于使用Apache POI设置日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!