日期:2014-05-20  浏览次数:20752 次

java读取excel读取不到时间类型
OPENDATE CLOSEDATE
2012/4/7 0:00 2012/4/7 0:00
2012/4/7 0:00 2012/4/7 0:00
读出来后是
  41006.0, 41006.0
  41006.0, 41006.0

代码 if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
rowData = String.valueOf((int)xCell
.getNumericCellValue());
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
rowData = String.valueOf(xCell
.getBooleanCellValue());
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
rowData = String.valueOf(xCell
.getCellFormula());
}
else if (xCell.getCellType() == XSSFCell.CELL_TYPE_STRING){
rowData = xCell.getStringCellValue();
}else {
rowData = xCell.getDateCellValue().toLocaleString();
}

为什么判断不到时间类型 excel表里面也设置了时间类型的。


求高手帮忙!!!想把这个放工具类里面 总不能每次都去找到时间列然后
rowData = xCell.getDateCellValue().toLocaleString();
求帮忙!

------解决方案--------------------
Java code

    public static void main(String[] args) throws Exception{
        InputStream inp = new FileInputStream("C:\\Documents and Settings\\pKF58059\\桌面\\TestCaseAlloc\\TEST\\aa.csv");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        System.out.println(sheet.getFirstRowNum());
        System.out.println(sheet.getLastRowNum());
        for(int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++){
            Row row = sheet.getRow(i);
            Cell cell = row.getCell(3);
            Date date = cell.getDateCellValue();
            System.out.println(date);
        }
    }

//打印结果
0
0
Sat Apr 07 00:00:00 CST 2012