How to read Excel cell having Date with Apache POI?

前端 未结 8 2345
面向向阳花
面向向阳花 2020-11-28 03:57

I\'m using Apache POI 3.6, I want to read an excel file which has a date like this 8/23/1991.

 switch (cell.getCellType()) {

   ...
   ...

            


        
8条回答
  •  没有蜡笔的小新
    2020-11-28 04:37

    Try this code.

    XSSFWorkbook workbook = new XSSFWorkbook(new File(result));
        XSSFSheet sheet = workbook.getSheetAt(0);
    
        // Iterate through each rows one by one
        Iterator rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            // For each row, iterate through all the columns
            Iterator cellIterator = row.cellIterator();
    
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    if (cell.getNumericCellValue() != 0) {
                        //Get date
                        Date date = row.getCell(0).getDateCellValue();
    
    
    
                        //Get datetime
                        cell.getDateCellValue()
    
    
                        System.out.println(date.getTime());
                    }
                    break;
                }
            }
        }
    

    Hope is help.

提交回复
热议问题