user261002
user261002

Reputation: 2252

import Date from database into an excel sheet

I am trying to get a DB data and pass the data into an excel sheet, but the problem is that I have a column with Date format in DB, whcih when I pass it into the excel it gives me wierd number like this 41793, here is a pice of my code :

for (Defect defect : r5Defects) {
            r = s.createRow(rowNum++);

            r.createCell(0).setCellValue(defect.getId());
            ...
            r.createCell(5).setCellValue(HSSFDateUtil.getExcelDate(defect.getCreated()));       
            ...
        }

What should I do to avoid this problem?

Upvotes: 0

Views: 73

Answers (1)

user261002
user261002

Reputation: 2252

I have created the following method and it works fine :

HSSFCellStyle cellStyleDate = (HSSFCellStyle) wb.createCellStyle();
        cellStyleDate.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
public void cell(Workbook wb, Row r, Date value,HSSFCellStyle cellStyleDate, int n) {

        HSSFCell cell = (HSSFCell) r.createCell(n);
        cell.setCellStyle(cellStyleDate);
        cell.setCellValue(value);
    }

Upvotes: 1

Related Questions