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:Logic.ReadDoctorsFromExcel.java

public void readFromExcel(String file, JTable table) throws IOException {

    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));

    XSSFSheet sheet = wb.getSheetAt(0);/*from  ww  w.jav  a  2 s  . co  m*/
    Iterator<Row> it = sheet.iterator();
    while (it.hasNext()) {
        Row row = it.next();
        Iterator<Cell> cells = row.iterator();
        while (cells.hasNext()) {
            Cell cell = cells.next();
            int cellIndex = cell.getColumnIndex();

            switch (cellIndex) {
            case 0:
                name = cell.getStringCellValue();
                break;
            case 1:
                if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                    snils = String.valueOf((int) cell.getNumericCellValue());
                    break;
                }
                if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
                    snils = cell.getStringCellValue();
                    break;
                }
                break;

            case 2:
                if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                    v002 = String.valueOf((int) cell.getNumericCellValue());
                    break;
                }
                if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
                    v002 = cell.getStringCellValue();
                    break;
                }
                break;
            case 3:
                if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                    v015 = String.valueOf((int) cell.getNumericCellValue());
                    break;
                }
                if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
                    v015 = cell.getStringCellValue();
                    break;
                }
                break;

            default:
                System.out.print("|");
                break;
            }
        }
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        String[] data = { name, snils, v002, v015 };
        model.addRow(data);

        removeAllFields();
    }

}

From source file:Logica.L_Exel.java

public String Importar(File archivo, JTable tablaD) {
    String respuesta = "Revisr";
    DefaultTableModel model = new DefaultTableModel();
    tablaD.setModel(model);//from   ww  w .  j a  va 2  s .c  o m
    try {
        wb = WorkbookFactory.create(new FileInputStream(archivo));
        Sheet Hoja = wb.getSheetAt(0);
        Iterator filaIterator = Hoja.rowIterator();
        int indiceFila = -1;
        while (filaIterator.hasNext()) {
            indiceFila++;
            Row fila = (Row) filaIterator.next();
            Iterator columnaIterator = fila.cellIterator();
            Object[] ListaColumna = new Object[7];
            int indicecolumna = -1;
            while (columnaIterator.hasNext()) {
                indicecolumna++;
                Cell celda = (Cell) columnaIterator.next();
                if (indiceFila == 0) {
                    model.addColumn(celda.getStringCellValue());
                } else {
                    if (celda != null) {
                        switch (celda.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            ListaColumna[indicecolumna] = (int) Math.round(celda.getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            ListaColumna[indicecolumna] = celda.getStringCellValue();
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            ListaColumna[indicecolumna] = celda.getBooleanCellValue();
                            break;
                        default:
                            ListaColumna[indicecolumna] = celda.getDateCellValue();
                            break;
                        }
                    }
                }
            }
            if (indiceFila != 0)
                model.addRow(ListaColumna);
        }
        respuesta = "Funciona";
    } catch (Exception e) {
    }
    return respuesta;
}

From source file:LogicModel.excel_Manage.java

public static void showExelData(List sheetsData) {
    ////from   w  w w . j a  v  a  2  s. c om
    // Recorre la lista que contiene las hojas del libro de excel 
    //
    for (int i = 0; i < sheetsData.size(); i++) {
        List list = (List) sheetsData.get(i);
        for (int j = 0; j < list.size(); j++) {
            Cell cell = (Cell) list.get(j);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                System.out.print(cell.getNumericCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                System.out.print(cell.getRichStringCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                System.out.print(cell.getBooleanCellValue());
            }
            if (j < list.size() - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("");
    }
}

From source file:lp.XLSXhandler.java

public Object[] opener(File uploaded, String name) {

    double[][] data = new double[0][0];
    String[] dmuNames = new String[0];
    String[] variable_names = new String[0];
    Object[] obj = new Object[5];

    try {/*from   w w  w. ja va2s  .c  o m*/

        OPCPackage pkg = OPCPackage.open(uploaded);
        XSSFWorkbook wb = new XSSFWorkbook(pkg);
        XSSFSheet sheet1 = wb.getSheetAt(0);

        //I find the number of the rows in the file! (0-based)
        int rows = sheet1.getLastRowNum();
        System.out.println("Total Rows of DATA in the file: " + rows);

        //I find the number of columns! (1-based)
        int columns = sheet1.getRow(0).getLastCellNum();
        System.out.println("Total Columns of DATA in the file: " + columns);

        data = new double[rows][columns - 1];
        dmuNames = new String[rows];
        variable_names = new String[columns];

        Row row_trav;
        Cell cell_trav;

        // Retrieve data from file to array
        for (int i = 0; i <= rows; i++) {
            row_trav = sheet1.getRow(i);

            for (int k = 0; k < columns; k++) {
                cell_trav = row_trav.getCell(k);

                if (i == 0) {
                    //we are at line 0 of the uploaded file
                    variable_names[k] = cell_trav.getStringCellValue();
                }

                if (k == 0 && i < rows) {
                    //we are at column 0 of the uploaded file

                    Row row_name = sheet1.getRow(i + 1);
                    cell_trav = row_name.getCell(0);
                    dmuNames[i] = cell_trav.getStringCellValue();
                }
                if (i > 0 && k > 0) {
                    data[i - 1][k - 1] = cell_trav.getNumericCellValue();
                }
            }
        }

        obj[0] = data;
        obj[1] = rows;
        obj[2] = columns;
        obj[3] = variable_names;
        obj[4] = dmuNames;

    } catch (InvalidFormatException e) {
    } catch (IOException e) {
    }

    return obj;
}

From source file:magicware.scm.redmine.tools.util.ExcelUtils.java

License:Apache License

public static String getCellContent(Cell cell, FormulaEvaluator evaluator) {

    String result = null;// w w  w . j  a va 2  s.  c o m

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        result = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            result = Constants.DATE_FORMAT.format(cell.getDateCellValue());
        } else {
            result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        switch (evaluator.evaluateFormulaCell(cell)) {
        case Cell.CELL_TYPE_BOOLEAN:
            result = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                result = Constants.DATE_FORMAT.format(cell.getDateCellValue());
            } else {
                result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
            }
            break;
        case Cell.CELL_TYPE_STRING:
            result = String.valueOf(cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BLANK:
            break;
        case Cell.CELL_TYPE_ERROR:
            result = String.valueOf(cell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            break;
        }
        break;
    default:
        break;
    }
    return result;
}

From source file:magic_trainer.SpreadSheet.java

public void printWorkSheet() {
    Iterator<Row> rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();//from w w w .  j  a va  2 s. c  o  m
        //For each row, iterate through all the columns
        Iterator<Cell> cellIterator = row.cellIterator();

        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            //Check the cell type and format accordingly
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                System.out.print(cell.getNumericCellValue() + " : ");
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print(cell.getStringCellValue() + " : ");
                break;
            }
        }
        System.out.println("");
    }
}

From source file:magic_trainer.SpreadSheet.java

public ArrayList<Object> getRowData() {

    // clears the data stored in Row data 
    this.RowData.clear();

    Iterator<Cell> cellIterator = this.currentRow.cellIterator();

    while (cellIterator.hasNext()) {
        Cell cell = cellIterator.next();
        //Check the cell type and format accordingly
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            this.RowData.add(cell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            this.RowData.add(cell.getStringCellValue());
            break;
        }/*from  ww  w  .j  a v a 2s.  c om*/
    }

    return this.RowData;
}

From source file:magic_trainer.SpreadSheet.java

public void printCurrentRow() {

    String toPrint = "";
    Iterator<Cell> cellIterator = this.currentRow.cellIterator();

    while (cellIterator.hasNext()) {
        Cell cell = cellIterator.next();
        //Check the cell type and format accordingly
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            System.out.print(cell.getNumericCellValue() + " : ");
            break;
        case Cell.CELL_TYPE_STRING:
            System.out.print(cell.getStringCellValue() + " : ");
            break;
        }/*www .j a v a2s  .c  o m*/
    }
    System.out.println("");
}

From source file:main.KeywordList.java

private String getCellValue(Cell cell) {
    switch (cell.getCellType()) {
    case CELL_TYPE_NUMERIC:
        return Double.toString(cell.getNumericCellValue());
    case CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case CELL_TYPE_BLANK:
        return "";
    case CELL_TYPE_BOOLEAN:
        return Boolean.toString(cell.getBooleanCellValue());
    case CELL_TYPE_ERROR:
        return Byte.toString(cell.getErrorCellValue());
    default:/*from   www .j a  v a2s  .  c o m*/
        return "";
    }
}

From source file:math.page.KnapsackTest.java

License:Apache License

public static void test3() throws InvalidFormatException, IOException {
    String path = "d:" + File.separator + "price.xlsx";
    File file = new File(path);
    Workbook workbook = WorkbookFactory.create(file);
    Sheet sheet = workbook.getSheetAt(0);
    List<Knapsack> bags = new ArrayList<Knapsack>();
    try {/*from w  w w .j a  v a 2s  . c  o m*/
        for (int row = 1; row <= sheet.getLastRowNum(); row++) {
            Row row2 = sheet.getRow(row);
            Cell cell0 = row2.getCell(0);
            Cell cell1 = row2.getCell(1);
            // 
            // System.out.print(cell.toString() + "  ");
            // System.out.println(cell0.getCellComment().toString());

            // Integer integer = Double.valueOf(cell0.getNumericCellValue())
            // .intValue();
            Integer integer = null;
            switch (cell0.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell0)) {

                } else {
                    cell0.setCellType(Cell.CELL_TYPE_STRING);
                    String temp = cell0.getStringCellValue();
                    // ??????????Double
                    if (temp.indexOf(".") > -1) {
                        integer = Double.valueOf(temp).intValue();
                    } else {
                        integer = Integer.valueOf(temp).intValue();
                    }
                }
                break;
            case Cell.CELL_TYPE_STRING:
                integer = Integer.valueOf(cell0.getStringCellValue()).intValue();
                break;
            default:
                break;
            }
            Knapsack knapsack = new Knapsack(integer, integer);
            knapsack.setNo(Double.valueOf(cell1.getNumericCellValue()).intValue());

            bags.add(knapsack);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    List<Total> list = test4();

    write(list, bags);

}