Example usage for org.apache.poi.ss.usermodel Cell getDateCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getDateCellValue

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getDateCellValue.

Prototype

Date getDateCellValue();

Source Link

Document

Get the value of the cell as a date.

Usage

From source file:org.riflemansd.businessprofit.excel.MyExcelDocument.java

License:Open Source License

public Date getDate(int nsheet, int nrow, int ncolumn) {
    Date value;/*  w ww  .  j  a  va2  s  .  c om*/

    org.apache.poi.ss.usermodel.Cell cell = getCell(nsheet, nrow, ncolumn);
    value = cell.getDateCellValue();

    return value;
}

From source file:org.seasar.fisshplate.util.FPPoiUtil.java

License:Apache License

private static Object getValueFromNumericCell(Cell cell) {
    String str = cell.toString();
    if (str.matches("\\d+-.+-\\d+")) {
        return cell.getDateCellValue();
    } else {//from   www  .  ja va  2  s . c o m
        return Double.valueOf(cell.getNumericCellValue());
    }
}

From source file:org.semtix.gui.auszahlung.auszahlungsmodul.Datenabgleich.java

License:Open Source License

/**
 * Liest eine XLS Datei ein und gleicht sie mit der Datenbank ab
 *
 * @param path /Pfad/angabe/zur/Datei.xyz 
 *//*from www. j  a va2s.  co m*/
public void einlesen(String path) {

    try {

        FileInputStream file = new FileInputStream(new File(path));

        // Get the workbook instance for XLS file
        HSSFWorkbook workbook = new HSSFWorkbook(file);

        // Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);

        // Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            Cell[] cells = new Cell[row.getLastCellNum()];
            int i = 0;
            boolean exmatrikuliert = false;
            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();
                cells[i] = cell;

                i++;

                if (i == 5 && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getStringCellValue().equalsIgnoreCase("X")) {
                        // if this cell contains an X we have to see if a)
                        // Antrag is invalid b) Person is Teilimmatrikuliert
                        // c) How many months Teilimmatrikuliert

                        exmatrikuliert = true;

                    }
                }
            }

            // we have to see if a) Antrag is invalid b) Person is
            // Teilimmatrikuliert c) How many months Teilimmatrikuliert
            if (exmatrikuliert) {
                Cell semesterCell = cells[6];

                // Semester aufschlsseln nach Jahr und Typ
                String semesterJahrPerson = null;
                String semesterTypPerson = null;
                if (semesterCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    String semester = "" + semesterCell.getNumericCellValue();
                    semesterJahrPerson = semester.substring(0, 4).trim();
                    semesterTypPerson = semester.substring(4, 5).trim();
                    if (semesterTypPerson.equals("1")) {
                        semesterTypPerson = "S";
                    } else if (semesterTypPerson.equals("2")) {
                        semesterTypPerson = "W";
                    }
                }

                // get current selected Semester
                String semesterTypAktuell = SemesterConf.getSemester().getSemesterArt().getBuchstabe().trim();
                String semesterJahrAktuell = SemesterConf.getSemester().getSemesterJahr().trim();

                // if its really the same semester get the date of
                // exmatriculation and check how many months
                if (semesterJahrAktuell.equalsIgnoreCase(semesterJahrPerson)
                        && semesterTypAktuell.equalsIgnoreCase(semesterTypPerson)) {
                    // get cell with exmatriculation date
                    Cell exmatriculationDateCell = cells[5];

                    // Find out date of exmatriculation / round up date to next month
                    int exmatriculationmonth = 0;
                    if (HSSFDateUtil.isCellDateFormatted(exmatriculationDateCell)) {
                        Date date = exmatriculationDateCell.getDateCellValue();

                        //                     Calendar cal = Calendar.getInstance();
                        //                     cal.setTime(date);
                        //                     int monat = cal.get(Calendar.MONTH);

                        SimpleDateFormat df = new SimpleDateFormat("MM");
                        exmatriculationmonth = Integer.parseInt(df.format(date));
                        exmatriculationmonth++;
                    }

                    int monateZuschuss = 0;

                    // Sommersemester
                    if (semesterTypAktuell.equals("S")) {

                        monateZuschuss = 6 - (10 - exmatriculationmonth);

                        // Wintersemester
                    } else if (semesterTypAktuell.equals("W")) {
                        if (exmatriculationmonth > 4) {
                            monateZuschuss = 6 - (16 - exmatriculationmonth);
                        } else {
                            monateZuschuss = 6 - (4 - exmatriculationmonth);
                        }
                    }

                    if (monateZuschuss < 6) {

                        Cell manrCell = cells[0];
                        Cell nachnameCell = cells[2];
                        Cell vornameCell = cells[3];

                        String matrikelnummer = "" + manrCell.getNumericCellValue();
                        matrikelnummer = matrikelnummer.substring(0, matrikelnummer.indexOf('.'));

                        String nachname = getStringFromCell(nachnameCell);
                        String vorname = getStringFromCell(vornameCell);

                        // write Teilzuschuss to DB
                        dbhandler.setAntragToTeilzuschuss(monateZuschuss, matrikelnummer);
                    }
                } else {

                    Cell manrCell = cells[0];
                    Cell nachnameCell = cells[2];
                    Cell vornameCell = cells[3];

                    String matrikelnummer = "" + manrCell.getNumericCellValue();
                    matrikelnummer = matrikelnummer.substring(0, matrikelnummer.indexOf('.'));

                    String nachname = getStringFromCell(nachnameCell);
                    String vorname = getStringFromCell(vornameCell);

                    //
                    dbhandler.denyAntrag(AntragAblehnungsgrund.EXMATRIKULIERT.getBegruendung(), matrikelnummer);

                }

            }

        }
        file.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.shareok.data.documentProcessor.ExcelHandler.java

/**
 * Reads out the data in an excel file and stores data in a hashmap<p>
 * The cell data has the ending of "--type" to label the data type
 * /*from   www.ja  v a  2  s .  co  m*/
 * @throws Exception
 */
@Override
public void readData() {

    String name = fileName;
    Sheet sheet = null;

    try {
        if (null == name || "".equals(name)) {
            throw new FileNameException("File name is not specified!");
        }

        FileInputStream file = new FileInputStream(new File(name));

        String extension = DocumentProcessorUtil.getFileExtension(name);

        String[] excelTypes = router.loadOfficeFileType("excel");

        if (null == excelTypes || excelTypes.length == 0) {
            throw new FileTypeException("The file types are empty!");
        }

        HashMap<String, String> typeMap = new HashMap<>();
        for (String s : excelTypes) {
            typeMap.put(s, s);
        }

        if (typeMap.containsKey(extension)) {
            if (extension.equals("xlsx")) {

            }
        }

        sheet = getWorkbookSheet(extension, file);
        int maxNumOfCells = sheet.getRow(0).getLastCellNum();
        Iterator<Row> rowIterator = sheet.iterator();
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        int rowCount = 0;
        //int colCount = 0;

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            //while(cellIterator.hasNext()) {
            for (int colCount = 0; colCount < maxNumOfCells; colCount++) {

                //Cell cell = cellIterator.next();
                Cell cell = row.getCell(colCount);
                if (null == cell) {
                    cell = row.createCell(colCount);
                }
                String key = Integer.toString(rowCount) + "-" + Integer.toString(colCount);
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    data.put(key, Boolean.toString(cell.getBooleanCellValue()) + "---bool");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (isCellDateFormatted(cell)) {
                        data.put(key, df.format(cell.getDateCellValue()) + "---dat");
                    } else {
                        data.put(key, Double.toString(cell.getNumericCellValue()) + "---num");
                    }
                    break;
                case Cell.CELL_TYPE_STRING:
                    data.put(key, cell.getStringCellValue() + "---str");
                    break;
                case Cell.CELL_TYPE_BLANK:
                    data.put(key, "");
                    break;
                case Cell.CELL_TYPE_ERROR:
                    data.put(key, "ERROR_VALUE");
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper()
                            .createFormulaEvaluator();
                    //handleCell(cell.getCachedFormulaResultType(), cell, evaluator);
                    data.put(key, String.valueOf(cell.getCachedFormulaResultType()));
                    break;
                default:
                    data.put(key, cell.getRichStringCellValue() + "---def");
                    break;
                }
                //    colCount++;
            }
            rowCount++;
            //colCount = 0;
        }
        file.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.shareok.data.msofficedata.ExcelHandler.java

/**
 *
 * @throws Exception/*from   w w w  . ja va  2s . c  o m*/
 */
@Override
public void readData() {

    String name = fileName;
    Sheet sheet = null;

    try {
        if (null == name || "".equals(name)) {
            throw new Exception("File name is not specified!");
        }

        FileInputStream file = new FileInputStream(new File(name));

        String extension = FileUtil.getFileExtension(name);

        String[] excelTypes = router.loadOfficeFileType("excel");

        if (null == excelTypes || excelTypes.length == 0) {
            throw new Exception("The file types are empty!");
        }

        HashMap<String, String> typeMap = new HashMap<>();
        for (String s : excelTypes) {
            typeMap.put(s, s);
        }

        if (typeMap.containsKey(extension)) {
            if (extension.equals("xlsx")) {

            }
        }

        sheet = getWorkbookSheet(extension, file);
        Iterator<Row> rowIterator = sheet.iterator();
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        int rowCount = 0;
        int colCount = 0;

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();
                String key = Integer.toString(rowCount) + "-" + Integer.toString(colCount);
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    data.put(key, Boolean.toString(cell.getBooleanCellValue()) + "---bool");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (isCellDateFormatted(cell)) {
                        data.put(key, df.format(cell.getDateCellValue()) + "---dat");
                    } else {
                        data.put(key, Double.toString(cell.getNumericCellValue()) + "---num");
                    }
                    break;
                case Cell.CELL_TYPE_STRING:
                    data.put(key, cell.getStringCellValue() + "---str");
                    break;
                default:
                    data.put(key, cell.getRichStringCellValue() + "---def");
                    break;
                }

                colCount++;
            }
            rowCount++;
            colCount = 0;
        }
        file.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException ex) {
        Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.spdx.spdxspreadsheet.DocumentInfoSheet.java

License:Apache License

protected Date getDataCellDateValue(int colNum) {
    Cell cell = getDataRow().getCell(colNum);
    if (cell == null) {
        return null;
    } else {/* www .  j  a  v  a  2  s . c  o  m*/
        return cell.getDateCellValue();
    }
}

From source file:org.spdx.spdxspreadsheet.LicenseSheet.java

License:Apache License

public LicenseSheet(Workbook workbook, String sheetName, File workbookFile) {
    super(workbook, sheetName);
    workbookPath = workbookFile.getParent();
    Row firstDataRow = sheet.getRow(firstRowNum + 1);
    if (firstDataRow != null) {
        // fill in versions
        Cell versionCell = firstDataRow.getCell(COL_VERSION);
        if (versionCell != null) {
            if (versionCell.getCellType() == Cell.CELL_TYPE_STRING) {
                version = versionCell.getStringCellValue();
            } else if (versionCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                version = String.valueOf(versionCell.getNumericCellValue());
            }//  w ww  .  j a  v a 2 s.c  o m
        }
        Cell releaseDateCell = firstDataRow.getCell(COL_RELEASE_DATE);
        if (releaseDateCell != null) {
            if (releaseDateCell.getCellType() == Cell.CELL_TYPE_STRING) {
                this.releaseDate = releaseDateCell.getStringCellValue();
            } else if (releaseDateCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                this.releaseDate = dateFormat.format(releaseDateCell.getDateCellValue());
            }
        }
    }
}

From source file:org.spdx.spdxspreadsheet.OriginsSheet.java

License:Apache License

private Date getDataCellDateValue(int colNum) {
    Cell cell = getDataRow().getCell(colNum);
    if (cell == null) {
        return null;
    } else {//w w  w . jav  a 2  s . c o m
        return cell.getDateCellValue();
    }
}

From source file:org.spdx.spdxspreadsheet.ReviewersSheet.java

License:Apache License

public Date getReviewerTimestamp(int rowNum) {
    Row row = sheet.getRow(rowNum);/*from  www . j a va 2 s.c  o m*/
    if (row == null) {
        return null;
    }
    Cell tsCell = row.getCell(TIMESTAMP_COL);
    if (tsCell == null) {
        return null;
    }
    return tsCell.getDateCellValue();
}

From source file:org.springframework.batch.item.excel.poi.ArrayPoiSheet.java

License:Apache License

/**
 * {@inheritDoc}// w w w . jav a2  s . c  o m
 */
@Override
public Object[] getRow(final int rowNumber) {
    final Row row = this.delegate.getRow(rowNumber);
    if (row == null) {
        return null;
    }
    final List<Object> cells = new LinkedList<Object>();

    for (int i = 0; i < getNumberOfColumns(); i++) {
        Cell cell = row.getCell(i);
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                cells.add(cell.getDateCellValue());
            } else {
                cells.add(cell.getNumericCellValue());
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cells.add(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
        case Cell.CELL_TYPE_BLANK:
            cells.add(cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            cells.add(getFormulaEvaluator().evaluate(cell).formatAsString());
            break;
        default:
            throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType());
        }
    }
    return cells.toArray(new Object[cells.size()]);
}