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

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

Introduction

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

Prototype

double getNumericCellValue();

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:eionet.gdem.conversion.excel.reader.ExcelReader.java

License:Mozilla Public License

/**
 * Reads cell value and formats it according to element type defined in XML Schema. If the cell contains formula,
 * then calculated value is returned.//ww  w  . j  av a2  s.c o  m
 *
 * @param cell       Spreadsheet Cell object.
 * @param schemaType XML Schema data type for given cell.
 * @return string value of the cell.
 */
protected String cellValueToString(Cell cell, String schemaType) {
    String value = "";

    if (cell != null) {
        switch (evaluator.evaluateInCell(cell).getCellType()) {
        case HSSFCell.CELL_TYPE_NUMERIC:
            if (HSSFDateUtil.isCellDateFormatted(cell) && !isYearValue(cell.getNumericCellValue())) {
                Date dateValue = cell.getDateCellValue();
                value = Utils.getFormat(dateValue, DEFAULT_DATE_FORMAT);
            } else if (HSSFDateUtil.isValidExcelDate(cell.getNumericCellValue()) && schemaType != null
                    && schemaType.equals("xs:date") && !isYearValue(cell.getNumericCellValue())) {
                Date dateValue = cell.getDateCellValue();
                value = Utils.getFormat(dateValue, DEFAULT_DATE_FORMAT);
            } else {
                value = formatter.formatCellValue(cell);
            }
            break;
        case HSSFCell.CELL_TYPE_STRING:
            RichTextString richText = cell.getRichStringCellValue();
            value = richText.toString();
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            value = Boolean.toString(cell.getBooleanCellValue());
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            break;
        default:
            break;
        }
    }
    return StringUtils.strip(value.trim(), String.valueOf(NON_BREAKING_SPACE)).trim();
}

From source file:Ekon.PanelVypisFirem.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    JFileChooser fch = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("objects xls xlsx", "xls", "xlsx");
    fch.setFileFilter(filter);/*from  w w w . j av a2 s. c o m*/
    int returnVal = fch.showOpenDialog(this);

    try {
        File nazevSouboru = fch.getSelectedFile();
        ArrayList<Firma> list = new ArrayList<>();
        FileInputStream file = new FileInputStream(nazevSouboru.getAbsoluteFile());
        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbookV = new XSSFWorkbook(nazevSouboru.getAbsolutePath());

        //Get first/desired sheet from the workbook
        XSSFSheet sheetV = workbookV.getSheetAt(0);

        Iterator<Row> rowIT = sheetV.iterator();

        while (rowIT.hasNext()) {
            Row row = rowIT.next();
            Iterator<Cell> cellIt = row.cellIterator();
            ArrayList<String> listO = new ArrayList<>();
            while (cellIt.hasNext()) {
                Cell cell = cellIt.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    // System.out.print(cell.getNumericCellValue() + " ");

                    listO.add(String.valueOf((int) cell.getNumericCellValue()));
                    break;
                case Cell.CELL_TYPE_STRING:
                    listO.add(cell.getStringCellValue());
                    //System.out.print(cell.getStringCellValue() + " ");
                    break;
                }

            }

            vytvorFirmy.pridejFirmu(new Firma(String.valueOf(listO.get(0)), String.valueOf(listO.get(1)),
                    String.valueOf(listO.get(2)), String.valueOf(listO.get(3)), String.valueOf(listO.get(4)),
                    Integer.valueOf(listO.get(5)), Integer.valueOf(listO.get(6)), String.valueOf(listO.get(7)),
                    Integer.valueOf(listO.get(8))));
        }
        file.close();
        DefaultTableModel model = (DefaultTableModel) tableFirmy.getModel();
        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
        Iterator it = vytvorFirmy.dejIterator();
        Iterator itN = poleNazvu.iterator();
        Firma f;
        try {

            for (Iterator it1 = it; it1.hasNext();) {
                f = (Firma) it1.next();
                model.addRow(new Object[] { f.getNazevFirmy(), f.getMesto(), f.getUlice(), f.getKraj(),
                        f.getPsc(), f.getKontakt(), f.getEmail(), f.getIco(), f.getDico() });
            }
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(this, "seznam firem je prazdny", "Chyba", 1);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Ekon.PanelVypisZamestnancu.java

private void btnNactiXLSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNactiXLSActionPerformed
    JFileChooser fch = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("objects xls xlsx", "xls", "xlsx");
    fch.setFileFilter(filter);//from ww w  . j ava  2s . co m
    int returnVal = fch.showOpenDialog(this);

    try {
        File nazevSouboru = fch.getSelectedFile();
        ArrayList<Zamestnanec> list = new ArrayList<>();
        FileInputStream file = new FileInputStream(nazevSouboru.getAbsoluteFile());
        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbookV = new XSSFWorkbook(nazevSouboru.getAbsolutePath());

        //Get first/desired sheet from the workbook
        XSSFSheet sheetV = workbookV.getSheetAt(0);

        Iterator<Row> rowIT = sheetV.iterator();
        rowIT.next();
        while (rowIT.hasNext()) {
            Row row = rowIT.next();
            Iterator<Cell> cellIt = row.cellIterator();
            ArrayList<String> listO = new ArrayList<>();
            while (cellIt.hasNext()) {
                Cell cell = cellIt.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    // System.out.print(cell.getNumericCellValue() + " ");

                    listO.add(String.valueOf((int) cell.getNumericCellValue()));
                    break;
                case Cell.CELL_TYPE_STRING:
                    listO.add(cell.getStringCellValue());
                    // System.out.print(cell.getStringCellValue() + " ");
                    break;
                }
            }
            aktualniFirma.pridejZamestnance(new Zamestnanec(listO.get(0), listO.get(1), listO.get(2),
                    listO.get(3), Integer.valueOf(listO.get(4)), listO.get(5), listO.get(6), listO.get(7),
                    listO.get(8), Integer.valueOf(listO.get(9)), Integer.valueOf(listO.get(10)), listO.get(11),
                    listO.get(12)));
        }
        file.close();
        DefaultTableModel model = (DefaultTableModel) tableZamestnanci.getModel();
        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
        Iterator it = aktualniFirma.dejIteratorZamestnancu();
        try {
            for (Iterator it1 = it; it1.hasNext();) {
                Zamestnanec z = (Zamestnanec) it1.next();
                model.addRow(new Object[] { z.getJmeno(), z.getPrijmeni(), z.getTitul(), z.getMesto(),
                        z.getUlice(), z.getPsc(), z.getKraj(), z.getDatumNarozeni(), z.getRodneCislo(),
                        z.getEmail(), z.getTelefoniCislo(), z.getNarodnost(), z.getPozice() });
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "seznam zamestnancu je prazdny", "Chyba", 1);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    Iterator itF = vytvorFirmy.dejIterator();
    for (Iterator it1 = itF; it1.hasNext();) {
        Firma f = (Firma) it1.next();
        if (aktualniFirma.getNazevFirmy().equals(f.getNazevFirmy())) {
            f.setListZamestanancu(aktualniFirma.getListZamestanancu());
        }
    }
}

From source file:energy.usef.pbcfeeder.PbcFeeder.java

License:Apache License

private void fillColStubInputMap(HSSFSheet pbcDataSheet) {
    stubColInputMap = new HashMap<>();
    for (Cell topRowCell : pbcDataSheet.getRow(0)) {
        if (topRowCell.getStringCellValue().equals("")) {
            break;
        }/*from w w  w. j a v a 2 s  .  c  o m*/
        String columnName = topRowCell.getStringCellValue();
        List<Double> colValues = new ArrayList<>();
        for (Row r : pbcDataSheet) {
            if (r.getRowNum() == 0) {
                continue;
            }
            Cell c = r.getCell(topRowCell.getColumnIndex());
            if (c != null) {
                if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    colValues.add(c.getNumericCellValue());
                }
            }
        }
        stubColInputMap.put(columnName, colValues);
    }
}

From source file:energy.usef.pbcfeeder.PbcFeeder.java

License:Apache License

/**
 * This method fills in the map with CongestionPoint-->Lower Power Limit and the map CongestionPoint-->Upper Power Limit.
 *
 * @param pbcCongestionPointLimitsSheet the excel page with the power limits.
 *//*from   w w  w. j  a  v a2  s. co m*/
private void fillCongestionPointLimitsMaps(HSSFSheet pbcCongestionPointLimitsSheet) {
    congestionPointLowerLimitMap = new HashMap<>();
    congestionPointUpperLimitMap = new HashMap<>();
    for (Cell columnHeading : pbcCongestionPointLimitsSheet.getRow(0)) {
        // stop when one encounters the first empty cell.
        if (StringUtils.isBlank(columnHeading.getStringCellValue())) {
            break;
        }
        for (Row row : pbcCongestionPointLimitsSheet) {
            // skip first row.
            if (row.getRowNum() == 0) {
                continue;
            }
            String congestionPoint = row.getCell(0).getStringCellValue();
            Cell cell = row.getCell(columnHeading.getColumnIndex());
            if (LOWER_LIMIT.equals(columnHeading.getStringCellValue())) {
                congestionPointLowerLimitMap.put(congestionPoint, new BigDecimal(cell.getNumericCellValue()));
            }
            if (UPPER_LIMIT.equals(columnHeading.getStringCellValue())) {
                congestionPointUpperLimitMap.put(congestionPoint, new BigDecimal(cell.getNumericCellValue()));
            }
        }
    }
}

From source file:energy.usef.pbcfeeder.PbcFeeder.java

License:Apache License

private void fillPbcStubDataDto(Cell c, PbcStubDataDto row) {
    switch (c.getColumnIndex()) {
    case 0:/*  ww w.  j  a  va2  s  .  c  o  m*/
        row.setIndex((int) c.getNumericCellValue());
        break;
    case 1:
        row.setCongestionPointOne(c.getNumericCellValue());
        break;
    case 2:
        row.setCongestionPointTwo(c.getNumericCellValue());
        break;
    case 3:
        row.setCongestionPointThree(c.getNumericCellValue());
        break;
    case 4:
        row.setCongestionPointAvg(c.getNumericCellValue());
        break;
    case 5:
        row.setPvLoadForecast(c.getNumericCellValue());
        break;
    case 6:
        row.setPvLoadActual(c.getNumericCellValue());
        break;
    case 7:
        row.setApx(c.getNumericCellValue());
        break;
    default:
        break;
    }
}

From source file:es.SSII2.manager.ExcelManagerAccount.java

public void readAccountExcel() throws FileNotFoundException, IOException {

    FileInputStream file;/* w ww . j a v  a2s .c  o  m*/
    file = new FileInputStream(new File(excel));

    try (XSSFWorkbook workbook = new XSSFWorkbook(file)) {
        XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        Row row;
        // Recorremos todas las filas para mostrar el contenido de cada celda
        while (rowIterator.hasNext()) {
            row = rowIterator.next();

            // Obtenemos el iterator que permite recorres todas las celdas de una fila
            Iterator<Cell> cellIterator = row.cellIterator();
            Cell celda;

            double val;
            DecimalFormat df = new DecimalFormat("#");

            while (cellIterator.hasNext()) {

                celda = cellIterator.next();

                if (celda.getRowIndex() >= 1 && celda.getColumnIndex() == 8 && celda.getCellType() != 3) {

                    val = celda.getNumericCellValue();
                    String stringPOI = NumberToTextConverter.toText(val);

                    //anadir la cuenta al arraylist y las posiciones
                    account.addAccount(stringPOI);
                    account.addAccountPos(celda.getRowIndex() + "-" + celda.getColumnIndex());

                }
            }
        }

    }
}

From source file:eu.alpinweiss.filegen.command.steps.impl.ReadInputParametersStepImpl.java

License:Apache License

private int readSheetCount(XSSFSheet sheet) {
    XSSFRow row = sheet.getRow(3);//from  w  w  w  .j a  v  a  2 s  . c  o m
    Cell cell = row.getCell(1);
    return (int) cell.getNumericCellValue();
}

From source file:eu.alpinweiss.filegen.command.steps.impl.ReadInputParametersStepImpl.java

License:Apache License

private int readIterationCount(XSSFSheet sheet) {
    XSSFRow row = sheet.getRow(0);/*from ww  w.j a  v a2 s. com*/
    Cell cell = row.getCell(1);
    return (int) cell.getNumericCellValue();
}

From source file:eu.learnpad.ontology.kpi.data.ExcelParser.java

public List<List<String>> getDataTable() throws IOException, InvalidFormatException {
    List<List<String>> dataTable = new ArrayList<>();
    Integer rowNumber = -2;/*  w  w  w. ja v  a 2 s. c  o  m*/

    Workbook wb = WorkbookFactory.create(excelFile);

    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    for (Sheet sheet : wb) {
        if (sheet.getSheetName().equals(SHEETNAME)) {
            for (Row row : sheet) {
                //stop with the first empty row
                if (row.getCell(0) == null) {
                    break;
                }
                if (rowNumber >= -1) {
                    rowNumber++;
                    dataTable.add(new ArrayList<String>());
                }
                for (Cell cell : row) {
                    String sheetName = sheet.getSheetName();
                    String cellRow = "Row:" + cell.getRowIndex();
                    String cellColumn = "Column:" + cell.getColumnIndex();
                    Object[] o = new Object[] { sheetName, cellRow, cellColumn };
                    LOGGER.log(Level.INFO, "Processing: Sheet={0} celladress={1}", o);
                    if (rowNumber <= -1 && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                        continue;
                    }
                    if (rowNumber == -2 && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        if (cell.getRichStringCellValue().getString().equals(DATACELLNAME)) {
                            rowNumber = -1;
                            continue;
                        }
                    }
                    //Attributes (column headers)
                    if (rowNumber == 0) {
                        dataTable.get(rowNumber).add(cell.getRichStringCellValue().getString());
                    }

                    if (rowNumber >= 1) {

                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            dataTable.get(rowNumber).add(cell.getRichStringCellValue().getString());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                dataTable.get(rowNumber).add(cell.getDateCellValue().toString());
                            } else {
                                dataTable.get(rowNumber).add(Double.toString(cell.getNumericCellValue()));
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            dataTable.get(rowNumber).add(Boolean.toString(cell.getBooleanCellValue()));
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            switch (cell.getCachedFormulaResultType()) {
                            case Cell.CELL_TYPE_STRING:
                                dataTable.get(rowNumber).add(cell.getRichStringCellValue().getString());
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                if (DateUtil.isCellDateFormatted(cell)) {
                                    dataTable.get(rowNumber).add(cell.getDateCellValue().toString());
                                } else {
                                    dataTable.get(rowNumber).add(Double.toString(cell.getNumericCellValue()));
                                }
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                dataTable.get(rowNumber).add(Boolean.toString(cell.getBooleanCellValue()));
                                break;
                            default:
                                dataTable.get(rowNumber).add("");
                            }
                            break;
                        default:
                            dataTable.get(rowNumber).add("");
                        }
                    }
                }
            }
        }
    }

    return dataTable;
}