简体   繁体   English

从数据库导入日期到Excel工作表

[英]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 : 我正在尝试获取数据库数据并将数据传递到Excel工作表中,但是问题是我在DB中有一个日期格式的列,当我将其传递给Excel时,它给了我一个奇怪的数字,例如41793,在这里是我的代码的一点:

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? 我应该怎么做才能避免这个问题?

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM