Reputation: 1553
I need some help in Apache poi
I am generating excel using Apache POI
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(2);
HSSFCell cell = row.createCell(1);
cell.setCellValue(someService.getDate());
I am getting this Date in DateFormat with TimeStamp MM/dd/yyyy 00:00:00 AM
but after generating Excel, it's not showing its proper value in DateFormat.
Any solution?
Upvotes: 0
Views: 4147
Reputation: 48356
You need to tell Excel what format to apply to the cell, so it knows how to format it
You probably want something like
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
CreationHelper createHelper = wb.getCreationHelper();
// Cell styles can be re-used, create only once
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
....
// In your loop
Row row = sheet.createRow(i);
Cell cell = row.createCell(1);
Date value = service.getDate();
cell.setCellValue(value);
cell.setCellStyle(cellStyle);
Upvotes: 4
Reputation: 46
Simplest solution is to set date value as formatted string, I think.
...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
cell.setCellValue(simpleDateFormat.format(someService.getDate()));
Upvotes: 3