【发布时间】:2012-06-29 07:14:11
【问题描述】:
我正在使用 apache poi 3.8 创建一个 excel 文件。这个excel文件需要包含一些日期。
我正在尝试将日期写入 excel 文件,格式为 excel 类型“日期”。但我总是得到一种“自定义”类型。我需要使用“日期”类型,因此它将根据用户设置进行本地化。
我尝试了以下方法:
Apache POI localized Date into Excel cell
但它不起作用。
这是我的代码:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("new sheet");
XSSFDataFormat df = wb.createDataFormat();
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(df.getFormat("d-mmm-yy"));
XSSFCell cell = sheet.createRow(0).createCell(0);
Calendar c = Calendar.getInstance();
c.set(2012,3-1,18);
cell.setCellValue( c.getTime() );
cell.setCellStyle(cs);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:\\temp\\dates-sworkbook.xlsx");
wb.write(fileOut);
fileOut.close();
我应该使用哪种格式?
谢谢
【问题讨论】:
标签: java excel apache-poi