Getting date in double format 43318.4847916667 instead of 06-08-2018 11:38:06

1k Views Asked by At

I am writing a java code to generate an excel file using HSSFWorkbook(.xls). I need to write date in one column in this format 06-08-2018 11:38:06 but it is generating like this 43318.4847916667.
Here is the code snippet i used. Please help me how can i successfully write

Workbook workbook = new HSSFWorkbook();
Sheet excelSheet = workbook.createSheet();
Row dataRow = excelSheet.createRow(1);;
Cell dataCell = dataRow.createCell(1);;
CellStyle cStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();         
cStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
dataCell.setCellValue(new Date());
dataCell.setCellStyle(cStyle);
3

There are 3 best solutions below

2
On BEST ANSWER

Cannot reproduce the problem.

The following complete example results in an Excel workbook having a worksheet and a proper formatted date in cell B2.

Code:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelDate {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new HSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("./Excel.xls") ) {

   CellStyle cStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();         
   cStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

   Sheet excelSheet = workbook.createSheet();
   Row dataRow = excelSheet.createRow(1);;
   Cell dataCell = dataRow.createCell(1);;
   dataCell.setCellValue(new java.util.Date());
   dataCell.setCellStyle(cStyle);

   excelSheet.setColumnWidth(1, 25 * 256);

   workbook.write(fileout);
  }

 }
}

Result:

enter image description here


From your comment it turns out that your question does not show the whole. There is a loop for rows and for each row a date shall be created. And that only works up to 42nd row.

Well that problem is well known. Excel has a limit for the count of cell styles per workbook. See: Excel specifications and limits.

So, if you create a new cell style over and over in the loop, that limit is sometimes reached. However, you do not always have to create new cell styles in the loop. Cell styles are stored at workbook level. Just create any needed cell style once outside the loop. Then apply only the previously created cell styles to the cell within the loop.

The following works for me and creates 1000 proper formatted dates:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelDate {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new HSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("./Excel.xls") ) {

   CellStyle cStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();         
   cStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

   Sheet excelSheet = workbook.createSheet();

   for (int r = 1; r < 1000; r++) {
    Row dataRow = excelSheet.createRow(r);;
    Cell dataCell = dataRow.createCell(1);;
    dataCell.setCellValue(new java.util.GregorianCalendar(2019, 9, r));
    dataCell.setCellStyle(cStyle);
   }

   excelSheet.setColumnWidth(1, 25 * 256);

   workbook.write(fileout);
  }

 }
}
0
On

Follow below code for printing date in DD-MM-YYYY HH:MM:ss.

String fileName = "myExcel.xls";
WritableWorkbook writableWorkbook = null;
response.setContentType("application/vnd.ms-excel");
writableWorkbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet excelOutputsheet = writableWorkbook.createSheet("Sheet1", 0);

DateFormat customDateFormatWithTime = new DateFormat("dd-MM-yyyy HH:mm:ss");
WritableCellFormat dateFormatWithTime = new 
WritableCellFormat(customDateFormatWithTime);

int row = 0;
int col = 0;
excelOutputsheet.setColumnView(col, 20);
Label lable1 = new Label(col, row, "Date and Time", cellFormat);
excelOutputsheet.addCell(lable1);

row = row + 1;
col = col + 1;
DateTime myDate = new jxl.write.DateTime(col, row, "Value", dateFormatWithTime);
excelOutputsheet.addCell(myDate);
1
On

If you want "06-08-2018 11:38:06" then your dateformat should be "dd-MM-YYYY HH:mm:ss". Otherwise you are just missing the FileOutputStream which you need to write the workbook to.

private static final String FILE_NAME = "./out/MyFirstExcel.xls";

public static void main(String ... args) {
    Workbook workbook = new HSSFWorkbook();
    Sheet excelSheet = workbook.createSheet();
    Row dataRow = excelSheet.createRow(1);
    Cell dataCell = dataRow.createCell(1);
    CellStyle cStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    cStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MM-YYYY HH:mm:ss"));
    dataCell.setCellValue(new Date());
    dataCell.setCellStyle(cStyle);

    try {
        File file = new File(FILE_NAME);
        if(!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}