List of usage examples for org.apache.poi.ss.usermodel CellValue getBooleanValue
public boolean getBooleanValue()
From source file:org.drools.decisiontable.parser.xls.ExcelParser.java
License:Apache License
private String getCellValue(final CellValue cv) { switch (cv.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return Boolean.toString(cv.getBooleanValue()); case Cell.CELL_TYPE_NUMERIC: return String.valueOf(cv.getNumberValue()); }/*from www . jav a 2 s . co m*/ return cv.getStringValue(); }
From source file:org.geotools.data.excel.ExcelFeatureSource.java
License:Open Source License
/** * create a FeatureSource with the specified Query * /*www . ja v a 2 s.c om*/ * @param entry * @param query * - a query containing a filter that will be applied to the data */ public ExcelFeatureSource(ContentEntry entry, Query query) { super(entry, query); Date beginingOfExcelTime = HSSFDateUtil.getJavaDate(0); dataStore = (ExcelDataStore) entry.getDataStore(); sheet = dataStore.getSheet(); latCol = dataStore.getLatColumnIndex(); lonCol = dataStore.getLonColumnIndex(); int rows = sheet.getPhysicalNumberOfRows(); int start = dataStore.getHeaderRowIndex() + 1; latCol = dataStore.getLatColumnIndex(); lonCol = dataStore.getLonColumnIndex(); features = new ArrayList<SimpleFeature>(); filteredFeatures = new ArrayList<SimpleFeature>(); evaluator = dataStore.workbook.getCreationHelper().createFormulaEvaluator(); if (schema == null) { schema = getSchema(); } GeometryFactory geometryFactory = dataStore.getGeometryFactory(); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema); Row header = sheet.getRow(dataStore.getHeaderRowIndex()); for (int i = start; i < rows; i++) { Row data = sheet.getRow(i); double x = 0.0; double y = 0.0; for (int col = data.getFirstCellNum(); col < data.getLastCellNum(); col++) { final Cell cell = data.getCell(col); CellValue value = evaluator.evaluate(cell); if (col == latCol) { if (value.getCellType() == Cell.CELL_TYPE_NUMERIC) { y = value.getNumberValue(); } } else if (col == lonCol) { if (value.getCellType() == Cell.CELL_TYPE_NUMERIC) { x = value.getNumberValue(); } } else { // cast and handle final String name = header.getCell(col).getStringCellValue().trim(); switch (value.getCellType()) { case Cell.CELL_TYPE_NUMERIC: AttributeType type = schema.getType(name); Class<?> clazz = type.getBinding(); if (clazz == Double.class) { builder.set(name, value.getNumberValue()); } else if (clazz == java.sql.Date.class) { final java.util.Date javaDate = HSSFDateUtil.getJavaDate(value.getNumberValue()); final Calendar cal = Calendar.getInstance(); cal.clear(); cal.setTime(javaDate); java.sql.Date date = new java.sql.Date(cal.getTimeInMillis()); builder.set(name, date); } else if (clazz == java.util.Date.class) { final java.util.Date javaDate = HSSFDateUtil.getJavaDate(value.getNumberValue()); builder.set(name, javaDate); } else if (clazz == Time.class) { final java.util.Date javaDate = HSSFDateUtil.getJavaDate(value.getNumberValue()); final Calendar cal = Calendar.getInstance(); cal.clear(); cal.setTime(javaDate); cal.set(0, 0, 0); Time time = new Time(cal.getTimeInMillis()); builder.set(name, time); } break; case Cell.CELL_TYPE_STRING: builder.set(name, value.getStringValue().trim()); break; case Cell.CELL_TYPE_BOOLEAN: builder.set(name, value.getBooleanValue()); break; default: System.out.println("We don't handle " + cell.getCellType() + " type cells " + cell.getStringCellValue()); } } } Point p = geometryFactory.createPoint(new Coordinate(x, y)); builder.set("the_geom", p); SimpleFeature feature = builder.buildFeature(null); features.add(feature); } filterFeatures(query); }
From source file:org.is.jxlpoi.JXLPOIWorkbook.java
License:Apache License
public String getCellContentAsString(Cell c) { if (c == null) return null; switch (c.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: if (c.getBooleanCellValue()) return "Y"; else// www. j ava 2 s .c o m return "N"; case Cell.CELL_TYPE_NUMERIC: String result = ""; int datatype = c.getCellStyle().getDataFormat(); String formatString = c.getCellStyle().getDataFormatString(); if (datatype == 174 && "yyyy/mm/dd".equals(formatString)) { java.util.Date date = c.getDateCellValue(); return fmter.format(date); } else if (datatype == 49 || datatype == 0) { int d = (int) c.getNumericCellValue(); result = Integer.toString(d); } else { result = Double.toString(c.getNumericCellValue()); } //return Double.toString(c.getNumericCellValue()); //System.out.println(" number = "+c.getNumericCellValue()+" *** value ="+twoPlaces.format(c.getNumericCellValue())+""); //return twoPlaces.format(c.getNumericCellValue())+""; return result; case Cell.CELL_TYPE_STRING: return c.getStringCellValue(); case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_ERROR: return "#ERROR" + c.getErrorCellValue(); case Cell.CELL_TYPE_FORMULA: String formulaCellValue; if (formulaEvaluator == null) { formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator(); //formulaEvaluator.setIgnoreFormulaException(); //System.out.println(formulaEvaluator); } //formulaEvaluator.evaluateFormulaCell(c); //formulaEvaluator.evaluateInCell(c); CellValue cv = formulaEvaluator.evaluate(c); switch (cv.getCellType()) { //switch (formulaEvaluator.evaluateInCell(c).getCellType()) { case Cell.CELL_TYPE_BOOLEAN: if (cv.getBooleanValue()) formulaCellValue = "Y"; else formulaCellValue = "F"; break; case Cell.CELL_TYPE_NUMERIC: formulaCellValue = Double.toString(cv.getNumberValue()); break; case Cell.CELL_TYPE_STRING: formulaCellValue = cv.getStringValue(); break; case Cell.CELL_TYPE_BLANK: formulaCellValue = ""; break; case Cell.CELL_TYPE_ERROR: formulaCellValue = Byte.toString(cv.getErrorValue()); break; default: formulaCellValue = ""; break; }//switch return formulaCellValue; default: return ""; }//switch }
From source file:org.obiba.onyx.core.io.support.ExcelReaderSupport.java
License:Open Source License
public static Boolean getBooleanValue(HSSFFormulaEvaluator evaluator, Cell cell) { Boolean rvalue = false;/*from www . j a v a2 s.c o m*/ if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { evaluator.evaluate(cell); CellValue cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: rvalue = cellValue.getBooleanValue(); break; case Cell.CELL_TYPE_STRING: rvalue = Boolean.parseBoolean(cellValue.getStringValue()); break; } } else { switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: rvalue = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_STRING: rvalue = Boolean.parseBoolean(cell.getRichStringCellValue().getString()); break; } } if (rvalue == null) { throw new IllegalArgumentException("Unexpected cell type"); } return rvalue; }
From source file:org.openconcerto.erp.importer.DataImporter.java
License:Open Source License
public ArrayTableModel createModelFromXLS(File xlsFile, int sheetNumber) throws IOException { final InputStream inputStream = new FileInputStream(xlsFile); final POIFSFileSystem fileSystem = new POIFSFileSystem(new BufferedInputStream(inputStream)); final HSSFWorkbook workBook = new HSSFWorkbook(fileSystem); final HSSFSheet sheet = workBook.getSheetAt(sheetNumber); Iterator<Row> rowsIterator = sheet.rowIterator(); int columnCount = 0; int rowCount = 0; while (rowsIterator.hasNext()) { Row row = rowsIterator.next();/*from w w w. j a v a 2 s . co m*/ int i = row.getPhysicalNumberOfCells(); if (i > columnCount) { columnCount = i; } rowCount++; } // Extract data rowsIterator = sheet.rowIterator(); int start = 0; if (skipFirstLine) { start = 1; rowsIterator.next(); } final List<List<Object>> rows = new ArrayList<List<Object>>(rowCount - start); FormulaEvaluator evaluator = workBook.getCreationHelper().createFormulaEvaluator(); while (rowsIterator.hasNext()) { final Row row = rowsIterator.next(); final List<Object> rowData = new ArrayList<Object>(); for (int i = 0; i < columnCount; i++) { final Cell cell = row.getCell(i); if (cell == null) { rowData.add(""); } else { CellValue cellValue = evaluator.evaluate(cell); if (cellValue == null) { rowData.add(""); } else { switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: rowData.add(Boolean.valueOf(cellValue.getBooleanValue())); break; case Cell.CELL_TYPE_NUMERIC: rowData.add(Double.valueOf(cellValue.getNumberValue())); break; case Cell.CELL_TYPE_STRING: rowData.add(cellValue.getStringValue()); break; case Cell.CELL_TYPE_FORMULA: rowData.add(cell.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: rowData.add(""); break; default: rowData.add(cellValue.getStringValue()); break; } } } } rows.add(rowData); } inputStream.close(); return new ArrayTableModel(rows); }
From source file:org.paxml.table.excel.ExcelFile.java
License:Open Source License
public Object getCellValue(Cell cell) { CellValue cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return cellValue.getBooleanValue(); case Cell.CELL_TYPE_NUMERIC: return cellValue.getNumberValue(); case Cell.CELL_TYPE_STRING: return cellValue.getStringValue(); case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_ERROR: return cellValue.getError(cell.getErrorCellValue()).getStringValue(); // CELL_TYPE_FORMULA will never happen case Cell.CELL_TYPE_FORMULA: throw new PaxmlRuntimeException("Internal error: invalid case"); default:/*from w ww . j a v a 2s . com*/ return null; } }
From source file:org.talend.dataprep.schema.xls.XlsUtils.java
License:Open Source License
/** * * @param cell/*from w ww. j a v a 2s . c o m*/ * @param cellValue * @return internal method which switch on the formula result value type then return a String value */ private static String getCellValueAsString(Cell cell, CellValue cellValue) { if (cellValue == null) { return StringUtils.EMPTY; } switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_BOOLEAN: return cellValue.getBooleanValue() ? Boolean.TRUE.toString() : Boolean.FALSE.toString(); case Cell.CELL_TYPE_ERROR: return "Cell Error type"; case Cell.CELL_TYPE_NUMERIC: return getNumericValue(cell, cellValue, cellValue != null); case Cell.CELL_TYPE_STRING: return StringUtils.trim(cell.getStringCellValue()); default: return "Unknown Cell Type: " + cell.getCellType(); } }
From source file:xqt.adapters.csv.reader.RowBuilder.java
public static String[] createRowArray(Row row, FormulaEvaluator evaluator) { String[] cellValues = new String[row.getLastCellNum() + 1]; for (int cellIndex = 0; cellIndex <= row.getLastCellNum(); cellIndex++) { CellValue cellValue = evaluator.evaluate(row.getCell(cellIndex)); if (cellValue != null) { switch (cellValue.getCellType()) { // what about the DATE type case Cell.CELL_TYPE_NUMERIC: //System.out.print(cellValue.getNumberValue() + "\t"); cellValues[cellIndex] = String.valueOf(cellValue.getNumberValue()); break; case Cell.CELL_TYPE_STRING: //System.out.print(cellValue.getStringValue() + "\t"); cellValues[cellIndex] = cellValue.getStringValue(); break; case Cell.CELL_TYPE_BOOLEAN: //System.out.println(cellValue.getBooleanValue() + "\t"); cellValues[cellIndex] = String.valueOf(cellValue.getBooleanValue()); break; case Cell.CELL_TYPE_FORMULA: // should not happen. It is evaluated by the evaluator case Cell.CELL_TYPE_BLANK: case Cell.CELL_TYPE_ERROR: cellValues[cellIndex] = ""; break; }/*from ww w .java 2s . c o m*/ } } return cellValues; }