How to read Excel cell having Date with Apache POI?

前端 未结 8 2237
面向向阳花
面向向阳花 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:25
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.CellType;
    import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    
    
    Row row = sheet.getRow(0);
    Cell cell = row.getCell(0);
    if(cell.getCellTypeEnum() == CellType.NUMERIC||cell.getCellTypeEnum() == CellType.FORMULA)
       {
        
    
     String cellValue=String.valueOf(cell.getNumericCellValue());
         if(HSSFDateUtil.isCellDateFormatted(cell))
          {
              DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
              Date date = cell.getDateCellValue();
              cellValue = df.format(date);
           }
              System.out.println(cellValue);
        }
    
    0 讨论(0)
  • 2020-11-28 04:25

    For reading date cells this method has proven to be robust so far:

    private LocalDate readCellAsDate(final Row row, final int pos) {
        if (pos == -1) {
            return null;
        }
        final Cell cell = row.getCell(pos - 1);
        if (cell != null) {
            cell.setCellType(CellType.NUMERIC);
        } else {
            return null;
        }
        if (DateUtil.isCellDateFormatted(cell)) {
            try {
                return cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            } catch (final NullPointerException e) {
                logger.error(e.getMessage());
                return null;
            }
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-11-28 04:27

    You can use CellDateFormatter to fetch the Date in the same format as in excel cell. See the following code:

    CellValue cv = formulaEv.evaluate(cell);
    double dv = cv.getNumberValue();
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
        Date date = HSSFDateUtil.getJavaDate(dv);
    
        String df = cell.getCellStyle().getDataFormatString();
    
        strValue = new CellDateFormatter(df).format(date); 
    }
    
    0 讨论(0)
  • 2020-11-28 04:31

    You need the DateUtils: see this article for details.

    Or, better yet, use Andy Khan's JExcel instead of POI.

    0 讨论(0)
  • 2020-11-28 04:31

    If you know the cell number, then i would recommend using getDateCellValue() method Here's an example for the same that worked for me - java.util.Date date = row.getCell().getDateCellValue(); System.out.println(date);

    0 讨论(0)
  • 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<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            // For each row, iterate through all the columns
            Iterator<Cell> 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.

    0 讨论(0)
提交回复
热议问题