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.blackducksoftware.tools.commonframework.standard.datatable.reader.DataTableReaderExcel.java

License:Apache License

private void readCell(Record rec, FieldDef fieldDef, Cell cell) throws Exception {
    if (cell == null) {
        return;/*from  ww  w. j a v  a  2s. com*/
    }
    switch (fieldDef.getType()) {
    case STRING:
        String cellStringValue = "";
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            cellStringValue = String.valueOf(cell.getNumericCellValue());
        } else {
            cellStringValue = cell.getStringCellValue();
        }
        log.debug("String cell; value: " + cellStringValue);
        rec.setFieldValue(fieldDef.getName(), cellStringValue);
        break;
    case DATE:
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            break;
        case Cell.CELL_TYPE_STRING:
            break;
        case Cell.CELL_TYPE_NUMERIC:
            Date cellDateValue = cell.getDateCellValue();
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(cellDateValue);
            rec.setFieldValue(fieldDef.getName(), cal);
            break;
        }
        break;
    case HYPERLINK:
        throw new Exception("DataTableReaderExcel does not yet support HYPERLINK field type");
    default:
    }
}

From source file:com.blackducksoftware.tools.commonframework.standard.workbook.CsvWriter.java

License:Apache License

@Override
public void write(Workbook wb) throws IOException {

    int numSheets = wb.getNumberOfSheets();
    for (int i = 0; i < numSheets; i++) {

        File curOutputFile = getCurrentOutputFile(filePath, numSheets, i);

        CSVWriter pw = new CSVWriter(new OutputStreamWriter(new FileOutputStream(curOutputFile)),
                CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, "\r\n");

        try {/*from ww  w . ja  v a  2  s. co  m*/
            Sheet sheet = wb.getSheetAt(i);
            for (Row row : sheet) {
                List<String> cells = new ArrayList<String>();
                String cellValue = "";
                for (Cell cell : row) {
                    int cellType = cell.getCellType();
                    switch (cellType) {
                    case Cell.CELL_TYPE_BLANK:
                        cellValue = "";
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        boolean cellValueBoolean = cell.getBooleanCellValue();
                        cellValue = cellValueBoolean ? "true" : "false";
                        break;
                    case Cell.CELL_TYPE_ERROR:
                        cellValue = "<error: " + cell.getErrorCellValue() + ">";
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        cellValue = cell.getCellFormula();
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        double cellValueDouble = cell.getNumericCellValue();
                        cellValue = Double.toString(cellValueDouble);
                        break;
                    case Cell.CELL_TYPE_STRING:
                        cellValue = cell.getStringCellValue();
                        break;
                    default:
                        break;
                    }

                    cells.add(cellValue);
                }
                String[] typeExample = new String[cells.size()];
                String[] cellArray = cells.toArray(typeExample);
                pw.writeNext(cellArray);
            }
        } finally {
            pw.close();
        }
    }
}

From source file:com.blackducksoftware.tools.commonframework.test.TestUtils.java

License:Apache License

private static String getCellValueString(Cell cell) {
    String cellValueString;//from ww  w. j a v  a2 s  .co  m

    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        cellValueString = String.valueOf(cell.getNumericCellValue());
    } else {
        cellValueString = cell.getStringCellValue();
    }
    return cellValueString.trim();
}

From source file:com.canoo.webtest.plugins.exceltest.ExcelCellUtils.java

License:Open Source License

public static String getCellValueAt(final Cell cell) {
    if (null == cell) {
        return "";
    }/*from   w  w  w .  j  a v  a 2  s .c  o  m*/
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        return asStringTrimInts(cell.getNumericCellValue());
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case Cell.CELL_TYPE_ERROR:
        return "Error Code " + String.valueOf(cell.getErrorCellValue() & 0xFF);
    ///CLOVER:OFF there are currently no other types.  Potentially more in future?
    default:
        LOG.warn("Cell Type not supported: " + cell.getCellType());
        return "";
    ///CLOVER:ON
    }
}

From source file:com.canoo.webtest.plugins.exceltest.ExcelVerifyCellSum.java

License:Open Source License

private void checkLiteralValue(final Cell excelCell) {
    final double cellValue = excelCell.getNumericCellValue();
    final int colon = getRange().indexOf(':');
    final CellReference start = ExcelCellUtils.getCellReference(this, getRange().substring(0, colon));
    final CellReference end = ExcelCellUtils.getCellReference(this, getRange().substring(colon + 1));
    double sum = 0;
    for (int row = start.getRow(); row <= end.getRow(); row++) {
        for (short col = start.getCol(); col <= end.getCol(); col++) {
            final Cell excelCellAt = ExcelCellUtils.getExcelCellAt(this, row, col);
            if (excelCellAt == null || excelCellAt.getCellType() == Cell.CELL_TYPE_BLANK) {
                continue;
            }/*from  w w w  . java 2s .co  m*/
            if (excelCellAt.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                sum += excelCellAt.getNumericCellValue();
            } else {
                throw new StepFailedException(
                        "Cell " + ((char) ('A' + col)) + (row + 1) + " does not contain a numeric value.");
            }
        }
    }
    if (Math.abs(cellValue - sum) > 0.01) {
        throw new StepFailedException(
                "Unexpected sum of cells from range " + fRange + " in cell " + getCellReferenceStr(),
                String.valueOf(sum), String.valueOf(cellValue));
    }
}

From source file:com.celtris.exparse.parser.ExcelReader.java

License:Apache License

public List<SheetData<T>> readExcel(String absolutePath, Class<T> excelModelClass, boolean headerExtraction)
        throws IOException, InstantiationException, IllegalAccessException {

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

    // Create Workbook instance holding reference to .xlsx file
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    DataFormatter objDefaultFormat = new DataFormatter();
    FormulaEvaluator objFormulaEvaluator = new XSSFFormulaEvaluator(workbook);

    Iterator<Sheet> sheetIterator = workbook.iterator();
    List<SheetData<T>> sheetDataList = new ArrayList<SheetData<T>>(workbook.getNumberOfSheets());
    int sheetCount = 0;
    while (sheetIterator.hasNext()) {
        sheetCount++;/*from www  .jav  a2 s  .  co  m*/

        ExcelParser<T> excelParser = new ExcelParser<T>(headerExtraction, excelModelClass);
        Sheet sheet = sheetIterator.next();
        Iterator<Row> rowIterator = sheet.iterator();

        int rowCount = 0;

        // Evaluating header
        if (headerExtraction) {
            if (rowIterator.hasNext()) {

                rowCount++;

                Field[] fields = excelModelClass.getFields();
                List<String> heaaderStr = new ArrayList<String>(fields.length);

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

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    String cellStrValue = cell.getStringCellValue();

                    heaaderStr.add(cellStrValue);
                }
                excelParser.processFieldAccordingToHeader(heaaderStr, sheet.getSheetName());
            }
        }

        while (rowIterator.hasNext()) {
            rowCount++;
            Row row = rowIterator.next();
            // For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            List<String> rowStr = new ArrayList<String>(excelParser.parameterCount());
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellStrValue = "";
                switch (cell.getCellTypeEnum()) {
                case STRING:
                    cellStrValue = cell.getStringCellValue();
                    break;
                case NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        objFormulaEvaluator.evaluate(cell);
                        cellStrValue = objDefaultFormat.formatCellValue(cell, objFormulaEvaluator);
                    } else {

                        cellStrValue = Double.toString(cell.getNumericCellValue());
                    }
                    break;
                case BOOLEAN:
                    cellStrValue = Boolean.toString(cell.getBooleanCellValue());
                    break;
                case FORMULA:
                    cellStrValue = cell.getStringCellValue();
                    break;
                case BLANK:

                default:
                    break;
                }
                rowStr.add(cellStrValue);
            }

            excelParser.processRow(rowStr, rowCount, sheet.getSheetName());
        }

        SheetData<T> sheetData = new SheetData<T>(excelParser.getParsedObject(), sheet.getSheetName(),
                sheetCount);
        sheetDataList.add(sheetData);
    }

    file.close();
    workbook.close();
    return sheetDataList;
}

From source file:com.citrix.g2w.webdriver.util.ReadExcelReport.java

License:Open Source License

/**
 * Method to get the cell value and convert to String.
 * //from   ww  w . ja  va  2 s.  c  om
 * @param cell
 * @return String
 */
private String getValue(Cell cell) {

    switch (cell.getCellType()) {

    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue() + "";

    case Cell.CELL_TYPE_NUMERIC:
        return ((int) cell.getNumericCellValue()) + "";

    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();

    case Cell.CELL_TYPE_BLANK:
        return "";

    default:
        return "";
    }
}

From source file:com.clican.pluto.dataprocess.engine.processes.ExcelProcessor.java

License:LGPL

public void readExcel(ProcessorContext context, ExcelExecBean execBean) throws Exception {
    InputStream is = new AutoDecisionResource(execBean.getResource()).getInputStream();
    try {/*from w ww.  j  av a 2  s . c o m*/
        Workbook book = WorkbookFactory.create(is);
        Sheet sheet = book.getSheet(execBean.getSheetName());
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        List<String> names = new ArrayList<String>();
        Map<String, String> typeMap = execBean.getTypeMap();
        int firstRow = sheet.getFirstRowNum(), lastRow = sheet.getLastRowNum();
        for (int rowIdx = firstRow; rowIdx < lastRow; rowIdx++) {
            Row excelRow = sheet.getRow(rowIdx);

            short minColIx = excelRow.getFirstCellNum();
            short maxColIx = excelRow.getLastCellNum();

            Map<String, Object> row = new HashMap<String, Object>();

            for (int colIdx = minColIx; colIdx < maxColIx; colIdx++) {
                Cell cell = excelRow.getCell(colIdx, Row.CREATE_NULL_AS_BLANK);

                if (rowIdx == 0) {
                    names.add(cell.getStringCellValue());
                } else {
                    String type = null;
                    if (names.size() > colIdx) {
                        type = typeMap.get(names.get(colIdx));
                    }
                    if (StringUtils.isNotEmpty(type)) {
                        if (type.equals("string")) {
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            row.put(names.get(colIdx), cell.getStringCellValue().trim());
                        } else if (type.equals("double")) {
                            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                            row.put(names.get(colIdx), cell.getNumericCellValue());
                        } else if (type.equals("int")) {
                            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                            row.put(names.get(colIdx), (int) cell.getNumericCellValue());
                        } else if (type.equals("date")) {
                            row.put(names.get(colIdx), cell.getDateCellValue());
                        } else {
                            throw new DataProcessException("??Excel?");
                        }
                    }
                }
            }
            if (rowIdx != 0) {
                result.add(row);
            }
        }
        context.setAttribute(execBean.getResultName(), result);
    } finally {
        if (is != null) {
            is.close();
        }
    }

}

From source file:com.cloudera.sa.ExcelRecordReader.java

License:Apache License

private Text getCellValue(Cell cell) {
    Text out = new Text();
    CellType cellType = cell.getCellTypeEnum();

    if (cellType == CellType.STRING) {
        out.set(cell.getStringCellValue());
    } else if (cellType == CellType.NUMERIC) {
        out.set(String.valueOf(cell.getNumericCellValue()));
    } else if (cellType == CellType.FORMULA) {
        out.set(cell.getCellFormula());//from   ww w . j  a  va  2s.c  o m
    } else if (cellType == CellType.ERROR) {
        out.set(String.valueOf(cell.getErrorCellValue()));
    } else if (cellType == CellType.BOOLEAN) {
        out.set(String.valueOf(cell.getBooleanCellValue()));
    } else {
        out.set("");
    }

    return out;
}

From source file:com.cmcti.cmts.domain.service.impl.CustomerMacMappingLocalServiceImpl.java

License:Open Source License

private String getStringCellValue(Cell cell) {
    String value = null;/*www . j ava2  s . c  om*/
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        value = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        break;
    case Cell.CELL_TYPE_FORMULA:
        break;
    case Cell.CELL_TYPE_STRING:
        value = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        double doubleValue = cell.getNumericCellValue();
        if (doubleValue == (int) doubleValue) {
            value = String.format("%d", (int) doubleValue);
        } else {
            value = String.format("%s", doubleValue);
        }

        break;
    }

    return value;
}