List of usage examples for org.apache.poi.ss.usermodel Cell getBooleanCellValue
boolean getBooleanCellValue();
From source file:com.github.crab2died.utils.Utils.java
License:Open Source License
/** * ??/* ww w. j a v a 2 s. c om*/ * * @param c ? * @return ? */ public static String getCellValue(Cell c) { String o; switch (c.getCellTypeEnum()) { case BLANK: o = ""; break; case BOOLEAN: o = String.valueOf(c.getBooleanCellValue()); break; case FORMULA: o = calculationFormula(c); break; case NUMERIC: if (DateUtil.isCellDateFormatted(c)) { o = DateUtils.date2Str(c.getDateCellValue()); } else { o = String.valueOf(c.getNumericCellValue()); o = matchDoneBigDecimal(o); o = RegularUtils.converNumByReg(o); } break; case STRING: o = c.getStringCellValue(); break; default: o = null; break; } return o; }
From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java
License:Open Source License
/** {@inheritDoc} */ @Override//from www. j a va 2 s .co m public/*@Nullable*/Boolean getBoolean(final int r, final int c) { final Cell cell = this.getPOICell(r, c); if (XlsPoiWriter.nullCellOrInvalidType(cell, Cell.CELL_TYPE_BOOLEAN)) return null; return cell.getBooleanCellValue(); }
From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java
License:Open Source License
/** {@inheritDoc} */ @Override//ww w . j a va 2 s .c o m public/*@Nullable*/Object getCellContent(final int rowIndex, final int colIndex) { final Cell cell = this.getPOICell(rowIndex, colIndex); if (cell == null) return null; Object result; switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: result = null; break; case Cell.CELL_TYPE_BOOLEAN: result = Boolean.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: result = "#Err"; break; case Cell.CELL_TYPE_FORMULA: result = cell.getCellFormula(); break; case Cell.CELL_TYPE_NUMERIC: if (this.isDate(cell)) { result = cell.getDateCellValue(); } else { final double value = cell.getNumericCellValue(); if (value == Math.rint(value)) result = Integer.valueOf((int) value); else result = value; } break; case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; default: throw new IllegalArgumentException(String.format("Unknown type of cell %d", cell.getCellType())); } return result; }
From source file:com.github.xiilei.ecdiff.Processor.java
License:Apache License
public String getStringCellValue(Cell cell) { String value = ""; switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: value = ""; break;// w ww . j ava 2 s . c o m case Cell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_STRING: value = cell.getStringCellValue(); cell.getRichStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: value = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_ERROR: value = ""; break; } return value.trim().replace(" ", ""); }
From source file:com.gsecs.GSECSFrame.java
private Object getCellValue(Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_NUMERIC: DataFormatter df = new DataFormatter(); String stringCellValue = df.formatCellValue(cell); return stringCellValue; }//w w w . ja v a 2 s . co m return null; }
From source file:com.hauldata.dbpa.file.book.XlsxSourceSheet.java
License:Apache License
private Object fromXLSX(Cell cell) { if (cell == null) { return null; }//w ww . ja va 2 s. c o m switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_NUMERIC: double numericValue = cell.getNumericCellValue(); return DateUtil.isCellDateFormatted(cell) ? DateUtil.getJavaDate(numericValue) : (Double) numericValue; case Cell.CELL_TYPE_STRING: default: return cell.getStringCellValue(); } }
From source file:com.heimaide.server.common.utils.excel.ImportExcel.java
License:Open Source License
/** * ??// ww w. java2s .c o m * @param row ? * @param column ??? * @return ? */ public Object getCellValue(Row row, int column) { Object val = ""; try { Cell cell = row.getCell(column); if (cell != null) { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { val = cell.getNumericCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { val = cell.getStringCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { try { if (HSSFDateUtil.isCellDateFormatted(cell)) { val = cell.getDateCellValue(); } else { val = String.valueOf(cell.getNumericCellValue()); } } catch (IllegalStateException e) { val = String.valueOf(cell.getRichStringCellValue()); } } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { val = cell.getBooleanCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) { val = cell.getErrorCellValue(); } } } catch (Exception e) { return val; } return val; }
From source file:com.helger.poi.excel.ExcelReadUtils.java
License:Apache License
/** * Return the best matching Java object underlying the passed cell.<br> * Note: Date values cannot be determined automatically! * //from w ww . j av a2 s .c o m * @param aCell * The cell to be queried. May be <code>null</code>. * @return <code>null</code> if the cell is <code>null</code> or if it is of * type blank. */ @Nullable public static Object getCellValueObject(@Nullable final Cell aCell) { if (aCell == null) return null; final int nCellType = aCell.getCellType(); switch (nCellType) { case Cell.CELL_TYPE_NUMERIC: return _getAsNumberObject(aCell.getNumericCellValue()); case Cell.CELL_TYPE_STRING: return aCell.getStringCellValue(); case Cell.CELL_TYPE_BOOLEAN: return Boolean.valueOf(aCell.getBooleanCellValue()); case Cell.CELL_TYPE_FORMULA: final int nFormulaResultType = aCell.getCachedFormulaResultType(); switch (nFormulaResultType) { case Cell.CELL_TYPE_NUMERIC: return _getAsNumberObject(aCell.getNumericCellValue()); case Cell.CELL_TYPE_STRING: return aCell.getStringCellValue(); case Cell.CELL_TYPE_BOOLEAN: return Boolean.valueOf(aCell.getBooleanCellValue()); default: throw new IllegalArgumentException( "The cell formula type " + nFormulaResultType + " is unsupported!"); } case Cell.CELL_TYPE_BLANK: return null; default: throw new IllegalArgumentException("The cell type " + nCellType + " is unsupported!"); } }
From source file:com.helger.poi.excel.ExcelReadUtilsTest.java
License:Apache License
/** * Validate reference sheets// w ww. jav a 2s . c om * * @param aWB * Workbook to use */ private void _validateWorkbook(@Nonnull final Workbook aWB) { final Sheet aSheet1 = aWB.getSheet("Sheet1"); assertNotNull(aSheet1); assertNotNull(aWB.getSheet("Sheet2")); final Sheet aSheet3 = aWB.getSheet("Sheet3"); assertNotNull(aSheet3); assertNull(aWB.getSheet("Sheet4")); Cell aCell = aSheet1.getRow(0).getCell(0); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_STRING, aCell.getCellType()); assertEquals("A1", aCell.getStringCellValue()); aCell = aSheet1.getRow(1).getCell(1); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_STRING, aCell.getCellType()); assertEquals("B2", aCell.getStringCellValue()); aCell = aSheet1.getRow(2).getCell(2); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_STRING, aCell.getCellType()); assertEquals("C\n3", aCell.getStringCellValue()); aCell = aSheet1.getRow(3).getCell(3); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_NUMERIC, aCell.getCellType()); assertEquals(0.00001, 4.4, aCell.getNumericCellValue()); for (int i = 0; i < 6; ++i) { aCell = aSheet3.getRow(i).getCell(i); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_NUMERIC, aCell.getCellType()); assertEquals(0.00001, i + 1, aCell.getNumericCellValue()); } // ="abc" aCell = aSheet1.getRow(4).getCell(0); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_FORMULA, aCell.getCellType()); assertEquals("\"abc\"", aCell.getCellFormula()); assertEquals("abc", aCell.getStringCellValue()); CellValue aEvaluated = new ExcelFormulaEvaluator(aWB, IStabilityClassifier.TOTALLY_IMMUTABLE) .evaluate(aCell); assertEquals(Cell.CELL_TYPE_STRING, aEvaluated.getCellType()); assertEquals("abc", aEvaluated.getStringValue()); // =4711 aCell = aSheet1.getRow(5).getCell(1); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_FORMULA, aCell.getCellType()); assertEquals("4711", aCell.getCellFormula()); assertEquals(0.00001, 4711, aCell.getNumericCellValue()); aEvaluated = new ExcelFormulaEvaluator(aWB, IStabilityClassifier.TOTALLY_IMMUTABLE).evaluate(aCell); assertEquals(Cell.CELL_TYPE_NUMERIC, aEvaluated.getCellType()); assertEquals(0.00001, 4711, aEvaluated.getNumberValue()); // =TRUE aCell = aSheet1.getRow(6).getCell(2); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_FORMULA, aCell.getCellType()); assertEquals("TRUE", aCell.getCellFormula()); assertTrue(aCell.getBooleanCellValue()); aEvaluated = new ExcelFormulaEvaluator(aWB, IStabilityClassifier.TOTALLY_IMMUTABLE).evaluate(aCell); assertEquals(Cell.CELL_TYPE_BOOLEAN, aEvaluated.getCellType()); assertTrue(aEvaluated.getBooleanValue()); // Refers to cell at 6/2 aCell = aSheet1.getRow(7).getCell(3); assertNotNull(aCell); assertEquals(Cell.CELL_TYPE_FORMULA, aCell.getCellType()); assertEquals("C7", aCell.getCellFormula()); assertTrue(aCell.getBooleanCellValue()); aEvaluated = new ExcelFormulaEvaluator(aWB, IStabilityClassifier.TOTALLY_IMMUTABLE).evaluate(aCell); assertEquals(Cell.CELL_TYPE_BOOLEAN, aEvaluated.getCellType()); assertTrue(aEvaluated.getBooleanValue()); }
From source file:com.hurence.logisland.processor.excel.ExcelExtract.java
License:Apache License
/** * Handle row content and transform it into a {@link Record} * * @param row the {@link Row}/* ww w.j av a2 s .c om*/ * @return the transformed {@link Record} */ private Record handleRow(Row row, List<String> header) { Record ret = new StandardRecord().setTime(new Date()); int index = 0; for (Cell cell : row) { if (configuration.getFieldNames() != null && index >= configuration.getFieldNames().size()) { //we've reached the end of mapping. Go to next row. break; } if (configuration.getColumnsToSkip().contains(cell.getColumnIndex())) { //skip this cell. continue; } String fieldName = header != null ? header.get(cell.getColumnIndex()) : configuration.getFieldNames().get(index++); Field field; // Alternatively, get the value and format it yourself switch (cell.getCellTypeEnum()) { case STRING: field = new Field(fieldName, FieldType.STRING, cell.getStringCellValue()); break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { field = new Field(fieldName, FieldType.LONG, cell.getDateCellValue().getTime()); } else { field = new Field(fieldName, FieldType.DOUBLE, cell.getNumericCellValue()); } break; case BOOLEAN: field = new Field(fieldName, FieldType.BOOLEAN, cell.getBooleanCellValue()); break; case FORMULA: field = new Field(fieldName, FieldType.STRING, cell.getCellFormula()); break; default: //blank or unknown field = new Field(fieldName, FieldType.NULL, null); break; } ret.setField(field); } return ret; }