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:com.web.mavenproject6.other.XLSParser.java

private static String readCell(Row row) {
    String buf = "";
    Iterator<Cell> cells = row.iterator();
    while (cells.hasNext()) {
        Cell cell = cells.next();
        int cellType = cell.getCellType();
        if (cell.getCellStyle().getLocked() == false) {
            switch (cellType) {
            case Cell.CELL_TYPE_STRING:
                buf += cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                buf += (int) cell.getNumericCellValue();
                break;

            case Cell.CELL_TYPE_FORMULA:
                buf += cell.getNumericCellValue();
                break;
            default:
                buf += " ";
                break;
            }/*from   w  w  w.  j a  v a2 s  .c o  m*/
        }
    }
    return buf;
}

From source file:com.web.mavenproject6.other.XLSParser.java

private static String readCell(Row row, int offset, int count) {
    String buf = "";
    Iterator<Cell> cells = row.iterator();
    int index = 0;
    while (cells.hasNext()) {
        Cell cell = cells.next();

        int cellType = cell.getCellType();
        if (cell.getCellStyle().getLocked() == false) {
            if (index >= offset && index < offset + count) {
                switch (cellType) {
                case Cell.CELL_TYPE_STRING:
                    buf += cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    buf += (int) cell.getNumericCellValue();
                    break;

                case Cell.CELL_TYPE_FORMULA:
                    buf += cell.getNumericCellValue();
                    break;

                }/*from w  ww . ja  va 2s .  com*/
            }
            index++;
        }
    }
    return buf;
}

From source file:com.xl.main.ReadExcelSampleSilk.java

public static String read(String filename) {
    Gson gson = new Gson();
    Map<String, List<SampleSinkBean>> values = new HashMap<String, List<SampleSinkBean>>();
    List<SampleSinkBean> byRow = new ArrayList<SampleSinkBean>();
    try {// ww  w .j a  va2  s.co m

        FileInputStream file = null;
        if (filename == null) {
            file = new FileInputStream(new File("H:\\anil\\sample-sink.xlsx"));
        } else {
            file = new FileInputStream(new File(filename));
        }

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        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();

            if (row.getRowNum() > 0 && row.getRowNum() < 20) {
                SampleSinkBean sb = new SampleSinkBean();
                //System.out.println("row value" + sheet.getRow(3).getCell(3));
                while (cellIterator.hasNext()) {//
                    Cell cell = cellIterator.next();

                    String cellString = " ";
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        cellString = cell.getNumericCellValue() + "";
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        cellString = cell.getStringCellValue() + "";

                        break;
                    case Cell.CELL_TYPE_ERROR:
                        cellString = cell.getErrorCellValue() + "";
                        break;
                    default:
                        cellString = cell.getStringCellValue() + "";

                    }

                    switch (cell.getColumnIndex()) {
                    case 0:
                        sb.setFrYear(cellString);
                        break;
                    case 1:
                        sb.setVpmod(cellString);
                    case 2:
                        sb.setProjectName(cellString);
                    case 3:
                        sb.setProjectWorktype(cellString);
                    case 4:
                        sb.setBusinessObjective(cellString);

                    }

                }
                byRow.add(sb);

            }
            // System.out.println("");

        }
        values.put("sink", byRow);
        System.out.println("output *********" + gson.toJson(values));

        file.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return gson.toJson(values);
}

From source file:com.xn.interfacetest.service.impl.TestCaseServiceImpl.java

License:Open Source License

/**
 * ?Cell?/*from www .j  a v a 2  s . c om*/
 * @param cell
 * @return
 */
private Object getCellFormatValue(Cell cell) {
    if (null == cell) {
        return "";
    }
    DataFormatter formatter = new DataFormatter();
    switch (cell.getCellTypeEnum()) {
    case STRING:
        return cell.getRichStringCellValue().getString();
    case NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {
            return Math.round(cell.getNumericCellValue());
        }
    case BOOLEAN:
        return cell.getBooleanCellValue();
    case FORMULA:
        return cell.getCellFormula();
    case BLANK:
        return "";
    default:
        return "";
    }

}

From source file:com.yqboots.initializer.core.builder.excel.factory.DomainMetadataFactory.java

License:Apache License

/**
 * Gets the properties./*  w  w  w .  j  av  a2  s  . com*/
 *
 * @param row the row of one sheet in the Excel
 * @return one DomainMetadataProperty
 */
private static DomainMetadataProperty getMetadataProperty(final Row row) {
    final DomainMetadataProperty result = new DomainMetadataProperty();

    Cell cell = row.getCell(3);
    if (cell != null) {
        result.setDbColumn(cell.getStringCellValue());
    }
    cell = row.getCell(4);
    if (cell != null) {
        result.setClassField(cell.getStringCellValue());
    }
    cell = row.getCell(5);
    if (cell != null) {
        result.setFieldType(cell.getStringCellValue());
    }
    cell = row.getCell(6);
    if (cell != null && cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        result.setNullable(cell.getBooleanCellValue());
    }
    cell = row.getCell(7);
    if (cell != null && cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        result.setUnique(cell.getBooleanCellValue());
    }
    cell = row.getCell(8);
    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            result.setDefaultValue(Double.toString(cell.getNumericCellValue()));
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            result.setDefaultValue(Boolean.toString(cell.getBooleanCellValue()));
            break;
        default:
            result.setDefaultValue(cell.getStringCellValue());
        }
    }

    return result;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

/**
 * ?excel//from  www  .  j a v a  2 s.c om
 * @param cell
 * @return
 */
private static String getCellValue(Cell cell) {
    String cellValue = "";
    DecimalFormat df = new DecimalFormat("#");
    if (cell == null || cell.equals("") || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        System.out.println(cellValue);
        return cellValue;
    }
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        cellValue = cell.getRichStringCellValue().getString().trim();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            cellValue = cell.getDateCellValue().toString();
        } else {
            cellValue = df.format(cell.getNumericCellValue()).toString();
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
        break;
    default:
        cellValue = "";
    }
    return cellValue;
}

From source file:com.zlfun.framework.excel.ExcelUtils.java

private static <T> Map<String, String> genRowMap(Row row, List<String> cols, FormulaEvaluator evaluator) {
    Map<String, String> map = new HashMap<String, String>();
    int j = 0;/*  ww  w  .j  a va 2 s  . c  o m*/
    for (String col : cols) {
        Cell cell = row.getCell(j);// ??
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            map.put(col, String.valueOf(cell.getBooleanCellValue()));
            break;
        case Cell.CELL_TYPE_NUMERIC:
            map.put(col, String.valueOf(cell.getNumericCellValue()));

            break;
        case Cell.CELL_TYPE_STRING:

            map.put(col, String.valueOf(cell.getStringCellValue()));
            break;
        case Cell.CELL_TYPE_BLANK:
            map.put(col, String.valueOf(""));

            break;
        case Cell.CELL_TYPE_ERROR:
            map.put(col, String.valueOf("error"));
            break;

        // CELL_TYPE_FORMULA will never occur  
        case Cell.CELL_TYPE_FORMULA:

            evaluator.evaluateFormulaCell(cell);
            map.put(col, String.valueOf(cell.getNumericCellValue()));
            break;

        }
        j++;
    }

    return map;
}

From source file:common.ReadExcelData.java

License:Apache License

public Integer findRow(HSSFSheet sheet, int cellContent) {
    for (Row row : sheet) {
        for (Cell cell : row) {
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                Double d = cell.getNumericCellValue();
                if (d.intValue() == cellContent) {
                    return row.getRowNum();
                }//from w  ww. j  a  va2s.c  o  m
            }
        }
    }
    return null;
}

From source file:common.ReadExcelData.java

License:Apache License

public ArrayList<String> getRowValue(String sheetName, int row) {
    HSSFSheet sheet = workbook.getSheet(sheetName);
    ArrayList<String> list = new ArrayList<String>();
    Row r = sheet.getRow(row);/*  www  .j a va2 s .  co  m*/
    for (Cell c : r) {
        if (c.getCellType() == Cell.CELL_TYPE_STRING)
            list.add(c.getStringCellValue());
        else if (c.getCellType() == Cell.CELL_TYPE_NUMERIC)
            list.add(String.valueOf(c.getNumericCellValue()));
    }
    return list;
}

From source file:common.ReadExcelData.java

License:Apache License

public String getCellValue(int index, String heading) {
    String cellValue = "";
    try {/*from  w w  w. j  av  a2s  .  c o  m*/
        sheet = workbook.getSheet(sheetName);
        row = sheet.getRow(0);
        int cellNumber = 0;
        for (Cell cell : row) {
            if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                if (cell.getRichStringCellValue().getString().trim().equals(heading)) {
                    cellNumber = cell.getColumnIndex();
                }
            }
        }
        row = sheet.getRow(findRow(sheet, index));
        cell = row.getCell(cellNumber);
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            cellValue = String.valueOf(((long) cell.getNumericCellValue()));
            break;
        case Cell.CELL_TYPE_STRING:
            cellValue = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cellValue = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_BLANK:
            cellValue = null;
        }
    } catch (NullPointerException e) {
        cellValue = null;
    }
    return cellValue;
}