List of usage examples for org.apache.poi.ss.usermodel CellValue getNumberValue
public double getNumberValue()
From source file:com.miraisolutions.xlconnect.data.ColumnBuilder.java
License:Open Source License
public Column buildBooleanColumn() { boolean[] colValues = new boolean[values.size()]; boolean[] missing = new boolean[values.size()]; Iterator<CellValue> it = values.iterator(); int counter = 0; while (it.hasNext()) { CellValue cv = it.next(); if (cv == null) { missing[counter] = true;// ww w. j av a 2 s.co m } else { switch (detectedTypes.get(counter)) { case Boolean: colValues[counter] = cv.getBooleanValue(); break; case Numeric: if (forceConversion) { colValues[counter] = cv.getNumberValue() > 0; } else { missing[counter] = true; } break; case String: if (forceConversion) { colValues[counter] = Boolean.parseBoolean(cv.getStringValue().toLowerCase()); } else { missing[counter] = true; } break; case DateTime: missing[counter] = true; addWarning("Cell " + CellUtils.formatAsString(cells.get(counter)) + " cannot be converted from DateTime to Boolean - returning NA"); break; default: throw new IllegalArgumentException("Unknown data type detected!"); } } ++counter; } return new Column(colValues, missing, DataType.Boolean); }
From source file:com.miraisolutions.xlconnect.data.ColumnBuilder.java
License:Open Source License
public Column buildDateTimeColumn() { Date[] colValues = new Date[values.size()]; boolean[] missing = new boolean[values.size()]; Iterator<CellValue> it = values.iterator(); Iterator<Cell> jt = cells.iterator(); int counter = 0; while (it.hasNext()) { CellValue cv = it.next(); Cell cell = jt.next();/*from w w w . j a v a 2 s . c o m*/ if (cv == null) { missing[counter] = true; } else { switch (detectedTypes.get(counter)) { case Boolean: missing[counter] = true; addWarning("Cell " + CellUtils.formatAsString(cells.get(counter)) + " cannot be converted from Boolean to DateTime - returning NA"); break; case Numeric: if (forceConversion) { if (DateUtil.isValidExcelDate(cv.getNumberValue())) { colValues[counter] = DateUtil.getJavaDate(cv.getNumberValue()); } else { missing[counter] = true; addWarning("Cell " + CellUtils.formatAsString(cells.get(counter)) + " cannot be converted from Numeric to DateTime - returning NA"); } } else { missing[counter] = true; } break; case String: if (forceConversion) { try { colValues[counter] = Workbook.dateTimeFormatter.parse(cv.getStringValue(), dateTimeFormat); } catch (Exception e) { missing[counter] = true; addWarning("Cell " + CellUtils.formatAsString(cells.get(counter)) + " cannot be converted from String to DateTime - returning NA"); } } else { missing[counter] = true; } break; case DateTime: colValues[counter] = cell.getDateCellValue(); break; default: throw new IllegalArgumentException("Unknown data type detected!"); } } ++counter; } return new Column(colValues, missing, DataType.DateTime); }
From source file:com.miraisolutions.xlconnect.data.ColumnBuilder.java
License:Open Source License
public Column buildNumericColumn() { double[] colValues = new double[values.size()]; boolean[] missing = new boolean[values.size()]; Iterator<CellValue> it = values.iterator(); int counter = 0; while (it.hasNext()) { CellValue cv = it.next(); if (cv == null) { missing[counter] = true;/*from ww w. j ava2 s .c o m*/ } else { switch (detectedTypes.get(counter)) { case Boolean: colValues[counter] = cv.getBooleanValue() ? 1.0 : 0.0; break; case Numeric: colValues[counter] = cv.getNumberValue(); break; case String: if (forceConversion) { try { colValues[counter] = Double.parseDouble(cv.getStringValue()); } catch (NumberFormatException e) { missing[counter] = true; addWarning("Cell " + CellUtils.formatAsString(cells.get(counter)) + " cannot be converted from String to Numeric - returning NA"); } } else { missing[counter] = true; } break; case DateTime: if (forceConversion) { colValues[counter] = cv.getNumberValue(); } else { missing[counter] = true; } break; default: throw new IllegalArgumentException("Unknown data type detected!"); } } ++counter; } return new Column(colValues, missing, DataType.Numeric); }
From source file:com.miraisolutions.xlconnect.data.ColumnBuilder.java
License:Open Source License
public Column buildStringColumn() { String[] colValues = new String[values.size()]; boolean[] missing = new boolean[values.size()]; Iterator<CellValue> it = values.iterator(); Iterator<Cell> jt = cells.iterator(); DataFormatter fmt = new DataFormatter(); int counter = 0; while (it.hasNext()) { CellValue cv = it.next(); Cell cell = jt.next();// w w w .jav a2 s . c o m if (cv == null) { missing[counter] = true; } else { switch (detectedTypes.get(counter)) { case Boolean: case Numeric: // format according to Excel format colValues[counter] = fmt.formatCellValue(cell, this.evaluator); break; case DateTime: // format according to dateTimeFormatter colValues[counter] = Workbook.dateTimeFormatter .format(DateUtil.getJavaDate(cv.getNumberValue()), dateTimeFormat); break; case String: colValues[counter] = cv.getStringValue(); break; default: throw new IllegalArgumentException("Unknown data type detected!"); } } ++counter; } return new Column(colValues, missing, DataType.String); }
From source file:com.miraisolutions.xlconnect.data.DefaultColumnBuilder.java
License:Open Source License
protected void handleCell(Cell c, CellValue cv) { String msg;//from ww w . ja va 2 s . c o m // Determine (evaluated) cell data type switch (cv.getCellType()) { case Cell.CELL_TYPE_BLANK: addMissing(); return; case Cell.CELL_TYPE_BOOLEAN: addValue(c, cv, DataType.Boolean); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(c)) addValue(c, cv, DataType.DateTime); else { boolean missing = false; for (int i = 0; i < missingValueNumbers.length; i++) { if (cv.getNumberValue() == missingValueNumbers[i]) { missing = true; break; } } if (missing) addMissing(); else addValue(c, cv, DataType.Numeric); } break; case Cell.CELL_TYPE_STRING: boolean missing = false; for (int i = 0; i < missingValueStrings.length; i++) { if (cv.getStringValue() == null || cv.getStringValue().equals(missingValueStrings[i])) { missing = true; break; } } if (missing) addMissing(); else addValue(c, cv, DataType.String); break; case Cell.CELL_TYPE_FORMULA: msg = "Formula detected in already evaluated cell " + CellUtils.formatAsString(c) + "!"; cellError(msg); break; case Cell.CELL_TYPE_ERROR: msg = "Error detected in cell " + CellUtils.formatAsString(c) + " - " + CellUtils.getErrorMessage(cv.getErrorValue()); cellError(msg); break; default: msg = "Unexpected cell type detected for cell " + CellUtils.formatAsString(c) + "!"; cellError(msg); } }
From source file:com.miraisolutions.xlconnect.utils.CellUtils.java
License:Open Source License
public static boolean isCellValueOfType(CellValue cv, DataType type) { switch (cv.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return DataType.Boolean.equals(type); case Cell.CELL_TYPE_NUMERIC: return DataType.Numeric.equals(type) || (DataType.DateTime.equals(type) && DateUtil.isValidExcelDate(cv.getNumberValue())); case Cell.CELL_TYPE_STRING: return DataType.String.equals(type); default:/*from w w w .j a va2 s. c o m*/ return false; } }
From source file:com.phucdk.emailsender.utils.ExcelUtils.java
public static String getCellValueAsString(int row, int column, XSSFWorkbook myWorkBook) { XSSFSheet mySheet = myWorkBook.getSheetAt(1); Cell cell = getCell(row, column, mySheet); String strCellValue = ""; FormulaEvaluator evaluator = myWorkBook.getCreationHelper().createFormulaEvaluator(); if (cell != null) { CellValue cellValue = null; try {//from w w w. j a va2s.c om cellValue = evaluator.evaluate(cell); } catch (Exception ex) { log.error("Error when evaluate cell value", ex); } if (cellValue != null) { switch (cellValue.getCellType()) { case Cell.CELL_TYPE_NUMERIC: strCellValue = String.valueOf(cellValue.getNumberValue()); break; case Cell.CELL_TYPE_BOOLEAN: strCellValue = String.valueOf(cellValue.getBooleanValue()); break; case Cell.CELL_TYPE_STRING: strCellValue = String.valueOf(cellValue.getStringValue()); break; case Cell.CELL_TYPE_FORMULA: //strCellValue = String.valueOf(cellValue.get()); break; } } } return strCellValue; }
From source file:com.phucdk.emailsender.utils.ExcelUtils.java
public static Object getCellValue(int row, int column, XSSFWorkbook myWorkBook) { XSSFSheet mySheet = myWorkBook.getSheetAt(1); Cell cell = getCell(row, column, mySheet); Object cellValueObject = ""; FormulaEvaluator evaluator = myWorkBook.getCreationHelper().createFormulaEvaluator(); if (cell != null) { CellValue cellValue = null; try {/*from w w w.j a va2 s. c om*/ cellValue = evaluator.evaluate(cell); } catch (Exception ex) { log.error("Error when evaluate cell value", ex); } if (cellValue != null) { switch (cellValue.getCellType()) { case Cell.CELL_TYPE_NUMERIC: cellValueObject = cellValue.getNumberValue(); break; case Cell.CELL_TYPE_BOOLEAN: cellValueObject = cellValue.getBooleanValue(); break; case Cell.CELL_TYPE_STRING: cellValueObject = cellValue.getStringValue(); break; case Cell.CELL_TYPE_FORMULA: //strCellValue = cellValue.getErrorValue(); break; } } } return cellValueObject; }
From source file:csv.impl.ExcelReader.java
License:Open Source License
/** * Returns the evaluated cell content./* w w w . java 2s . c o m*/ * This assumes the cell contains a formula. * @param cell cell to evaluate * @return cell value */ public Object evaluateCellValue(Cell cell) { FormulaEvaluator evaluator = getFormulaEvaluator(); CellValue value = evaluator.evaluate(cell); switch (value.getCellType()) { case Cell.CELL_TYPE_STRING: return value.getStringValue(); case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return DateUtil.getJavaDate(value.getNumberValue()); } else { return value.getNumberValue(); } case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: return value.getBooleanValue(); case Cell.CELL_TYPE_ERROR: return value.getErrorValue(); default: System.out.println("type=" + cell.getCellType()); } return cell.getCellFormula(); }
From source file:de.bund.bfr.knime.pmm.common.XLSReader.java
License:Open Source License
private String getData(Cell cell) { if (cell != null) { if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { CellValue value = evaluator.evaluate(cell); switch (value.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return value.getBooleanValue() + ""; case Cell.CELL_TYPE_NUMERIC: return value.getNumberValue() + ""; case Cell.CELL_TYPE_STRING: return value.getStringValue(); default: return ""; }// www. ja v a 2 s .c o m } else { return cell.toString().trim(); } } return null; }