List of usage examples for org.apache.poi.ss.usermodel Cell getBooleanCellValue
boolean getBooleanCellValue();
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private String extractCellFormulaValue(Cell excelCell) { String value = null;//from w w w .jav a 2 s. co m switch (excelCell.getCachedFormulaResultType()) { case Cell.CELL_TYPE_NUMERIC: value = Double.toString(excelCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: value = excelCell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: value = Boolean.toString(excelCell.getBooleanCellValue()); break; } return value; }
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private String extractCellValue(Cell excelCell) { String value = null;//from ww w. ja v a 2 s .c o m switch (excelCell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(excelCell)) value = "DATE"; // TODO - ? else value = Double.toString(excelCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: value = excelCell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_BOOLEAN: value = Boolean.toString(excelCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: //value = excelCell.getCellFormula(); value = extractCellFormulaValue(excelCell); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_ERROR: break; } return value; }
From source file:se.softhouse.garden.oak.ExcelDecisionTableBuilder.java
License:Open Source License
private Object getCellValue(int type, Cell cell) { switch (type) { case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_NUMERIC: return getNumericValue(type, cell); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_FORMULA: return getCellValue(cell.getCachedFormulaResultType(), cell); }// w ww.j a v a2s . c om return null; }
From source file:Servelt.ExcelReader.java
private String cellToString(Cell cell) throws Exception { String data = null;//from w w w. j a v a 2s . c o m switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: data = cell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { data = cell.getDateCellValue().toString(); } else { data = String.valueOf(cell.getNumericCellValue()); while (data.endsWith("0") && data.contains(".")) { data = data.substring(0, data.length() - 1); } if (data.endsWith(".")) data = data.substring(0, data.length() - 1); } break; case Cell.CELL_TYPE_BOOLEAN: data = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: data = cell.getCellFormula(); break; case Cell.CELL_TYPE_BLANK: data = ""; break; case Cell.CELL_TYPE_ERROR: throw new Exception("CELL_TYPE_ERROR"); } return data; }
From source file:step.datapool.excel.ExcelFunctions.java
License:Open Source License
/** * Konvertiert unterschiedliche Formate in Strings. * * @param cell Excel Zelle/*from w ww .j a v a 2 s.c o m*/ * @param evaluator FormulaEvaluator * @return Wert der Zelle als String */ public static String getCellValueAsString(Cell cell, FormulaEvaluator evaluator) { boolean isFormulaPatched = false; String initialFormula = null; int chkTyp = cell.getCellType(); if (chkTyp == Cell.CELL_TYPE_FORMULA) { initialFormula = cell.getCellFormula(); // Some formula have to be changed before they can be evaluated in POI String formula = FormulaPatch.patch(initialFormula); if (!formula.equals(initialFormula)) { isFormulaPatched = true; cell.setCellFormula(formula); evaluator.notifySetFormula(cell); } } try { int typ = evaluateFormulaCell(cell, evaluator); if (typ == -1) typ = cell.getCellType(); switch (typ) { case Cell.CELL_TYPE_NUMERIC: /* Datum und Zeit (sind auch Zahlen) */ if (DateUtil.isCellDateFormatted(cell)) { Date dat = cell.getDateCellValue(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(dat); /* * In Excel beginnt die Zeitrechnung am 01.01.1900. Ein Datum ist immer als * double gespeichert. Dabei ist der Teil vor dem Dezimalpunkt das Datum * und der Teil nach dem Dezimalpunkt die Zeit (z.B. 1.5 entspricht 01.01.1900 12:00:00). * Falls der Tag 0 angegeben ist wird der Datumsanteil mit 31.12.1899 zurueck- * gegeben. Erhalten wir also ein Jahr kleiner als 1900, dann haben wir eine * Zeit. */ if (cal.get(Calendar.YEAR) < 1900) { // Zeitformat SimpleDateFormat STD_TIM = new SimpleDateFormat("kk:mm:ss"); return STD_TIM.format(dat); } SimpleDateFormat STD_DAT = new SimpleDateFormat("dd.MM.yyyy"); return STD_DAT.format(dat); // Datumsformat } else { /* int, long, double Formate */ double dbl = cell.getNumericCellValue(); int tryInt = (int) dbl; long tryLong = (long) dbl; if (tryInt == dbl) { return new Integer(tryInt).toString(); // int-Format } else if (tryLong == dbl) { return new Long(tryLong).toString(); // long-Format } // return new Double(dbl).toString(); // double-Format String numberValueString = new Double(dbl).toString(); // double-Format // always use decimal format try { // scale 14 to solve problem like value 0.22 --> 0.219999999999997 BigDecimal roundedBigDecimal = new BigDecimal(numberValueString).setScale(14, RoundingMode.HALF_UP); // use constructor BigDecimal(String)! String customValueString = getCustomDecimalFormat().format(roundedBigDecimal); if (!customValueString.equals(numberValueString)) { logger.debug("getCellValusAsString: Changing string value of double '{}' to '{}'", numberValueString, customValueString); numberValueString = customValueString; // bigdecimal-format } } catch (Exception e) { logger.error("An error occurred trying to convert the cell value number to decimal format " + numberValueString, e); } return numberValueString; } case Cell.CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case Cell.CELL_TYPE_FORMULA: /* Dieser Fall wird jetzt nie eintreffen, da im Falle einer Formel neu die * Berechnung zurueckgegeben wurde, die dann einen eigenen Typ hat. */ return cell.getCellFormula(); case Cell.CELL_TYPE_STRING: return cell.getRichStringCellValue().getString(); case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_ERROR: switch (cell.getErrorCellValue()) { case 1: return "#NULL!"; case 2: return "#DIV/0!"; case 3: return "#VALUE!"; case 4: return "#REF!"; case 5: return "#NAME?"; case 6: return "#NUM!"; case 7: return "#N/A"; default: return "#ERR!"; } default: return "ERROR: unknown Format"; } } finally { if (isFormulaPatched) { cell.setCellFormula(initialFormula); evaluator.notifySetFormula(cell); } } }
From source file:tech.tablesaw.io.xlsx.XlsxReader.java
License:Apache License
private Boolean isBlank(Cell cell) { switch (cell.getCellType()) { case STRING:/* w w w . ja va2 s . c o m*/ if (cell.getRichStringCellValue().length() > 0) { return false; } break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell) ? cell.getDateCellValue() != null : cell.getNumericCellValue() != 0) { return false; } break; case BOOLEAN: if (cell.getBooleanCellValue()) { return false; } break; case BLANK: return true; default: break; } return null; }
From source file:tech.tablesaw.io.xlsx.XlsxReader.java
License:Apache License
@SuppressWarnings("unchecked") private Column<?> appendValue(Column<?> column, Cell cell) { switch (cell.getCellType()) { case STRING:/* w w w .j av a 2 s .c o m*/ column.appendCell(cell.getRichStringCellValue().getString()); return null; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); // This will return inconsistent results across time zones, but that matches Excel's behavior LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); column.appendCell(localDate.toString()); return null; } else { double num = cell.getNumericCellValue(); if (column.type() == ColumnType.INTEGER) { Column<Integer> intColumn = (Column<Integer>) column; if ((int) num == num) { intColumn.append((int) num); return null; } else if ((long) num == num) { Column<Long> altColumn = LongColumn.create(column.name(), column.size()); altColumn = intColumn.mapInto(s -> (long) s, altColumn); altColumn.append((long) num); return altColumn; } else { Column<Double> altColumn = DoubleColumn.create(column.name(), column.size()); altColumn = intColumn.mapInto(s -> (double) s, altColumn); altColumn.append(num); return altColumn; } } else if (column.type() == ColumnType.LONG) { Column<Long> longColumn = (Column<Long>) column; if ((long) num == num) { longColumn.append((long) num); return null; } else { Column<Double> altColumn = DoubleColumn.create(column.name(), column.size()); altColumn = longColumn.mapInto(s -> (double) s, altColumn); altColumn.append(num); return altColumn; } } else if (column.type() == ColumnType.DOUBLE) { Column<Double> doubleColumn = (Column<Double>) column; doubleColumn.append(num); return null; } } break; case BOOLEAN: if (column.type() == ColumnType.BOOLEAN) { Column<Boolean> booleanColumn = (Column<Boolean>) column; booleanColumn.append(cell.getBooleanCellValue()); return null; } default: break; } return null; }
From source file:Test.LeerExcel.java
public static void main(String args[]) throws IOException { FileInputStream file = new FileInputStream(new File("C:\\Users\\f2 a55m-s1\\Documents\\baseSqlLite\\")); // Crear el objeto que tendra el libro de Excel HSSFWorkbook workbook = new HSSFWorkbook(file); /*/*from w ww.java 2s . c o m*/ * Obtenemos la primera pestaa a la que se quiera procesar indicando el indice. * Una vez obtenida la hoja excel con las filas que se quieren leer obtenemos el iterator * que nos permite recorrer cada una de las filas que contiene. */ HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); Row row; // Recorremos todas las filas para mostrar el contenido de cada celda while (rowIterator.hasNext()) { row = rowIterator.next(); // Obtenemos el iterator que permite recorres todas las celdas de una fila Iterator<Cell> cellIterator = row.cellIterator(); Cell celda; while (cellIterator.hasNext()) { celda = cellIterator.next(); // Dependiendo del formato de la celda el valor se debe mostrar como String, Fecha, boolean, entero... switch (celda.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(celda)) { System.out.println(celda.getDateCellValue()); } else { System.out.println(celda.getNumericCellValue()); } System.out.println(celda.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: System.out.println(celda.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: System.out.println(celda.getBooleanCellValue()); break; } } } // cerramos el libro excel workbook.close(); }
From source file:Test.LeerExcelXlsx.java
public static void main(String args[]) throws IOException { FileInputStream file = new FileInputStream( new File("C:\\Users\\f2 a55m-s1\\Documents\\baseSqlLite\\Libro1.xlsx")); // Crear el objeto que tendra el libro de Excel XSSFWorkbook workbook = new XSSFWorkbook(file); /*/*from www. j a va 2 s. c om*/ * Obtenemos la primera pestaa a la que se quiera procesar indicando el indice. * Una vez obtenida la hoja excel con las filas que se quieren leer obtenemos el iterator * que nos permite recorrer cada una de las filas que contiene. */ XSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); Row row; // Recorremos todas las filas para mostrar el contenido de cada celda while (rowIterator.hasNext()) { row = rowIterator.next(); // Obtenemos el iterator que permite recorres todas las celdas de una fila Iterator<Cell> cellIterator = row.cellIterator(); Cell celda; while (cellIterator.hasNext()) { celda = cellIterator.next(); // Dependiendo del formato de la celda el valor se debe mostrar como String, Fecha, boolean, entero... switch (celda.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(celda)) { System.out.print(celda.getDateCellValue()); } else { System.out.print(celda.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: System.out.print(celda.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(celda.getBooleanCellValue()); break; } } System.out.println(""); } // cerramos el libro excel workbook.close(); }
From source file:testpoi.POI_POC.java
/** * @param args the command line arguments *//* w w w.j a v a 2 s . c o m*/ public static void main(String[] args) { try { FileInputStream file = new FileInputStream( new File("/home/chandni/Documents/HMS Docs/01.01.2014.xlsx")); //Get the workbook instance for XLS file XSSFWorkbook workbook = new XSSFWorkbook(file); //Get first sheet from the workbook XSSFSheet sheet = workbook.getSheetAt(0); //Get iterator to all the rows in current sheet Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); //For each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t\t"); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t\t"); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t\t"); break; } } System.out.println(""); } file.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }