List of usage examples for org.apache.poi.ss.usermodel Cell getRichStringCellValue
RichTextString getRichStringCellValue();
For numeric cells we throw an exception.
From source file:com.pe.nisira.movil.view.action.RegistroPaleta.java
public void getExcelTablePaletaList(List sheetData) { try {/*from w w w.j ava2 s . co m*/ if (sheetData.size() > 0) { List<Paleta> lista = new ArrayList<Paleta>(); Paleta paleta = new Paleta(); for (int i = 1; i < sheetData.size(); i++) { List list = (List) sheetData.get(i); paleta = new Paleta(); for (int j = 0; j < list.size(); j++) { Cell cell = (Cell) list.get(j); if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { System.out.print(cell.getNumericCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { System.out.print(cell.getRichStringCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { System.out.print(cell.getBooleanCellValue()); } switch (j) { case 0: paleta.setIdempresa(cell.getRichStringCellValue().getString()); break; case 1: paleta.setIdregistropaleta(cell.getRichStringCellValue().getString()); break; case 2: paleta.setIdproducto(cell.getRichStringCellValue().getString()); break; case 3: paleta.setNropaleta(cell.getRichStringCellValue().getString()); break; case 4: paleta.setDescproducto(cell.getRichStringCellValue().getString()); break; case 5: paleta.setIdmedida(cell.getRichStringCellValue().getString()); break; case 6: paleta.setCantidad(cell.getRichStringCellValue().getString()); break; case 7: paleta.setPeso(Double.parseDouble(cell.getRichStringCellValue().getString())); break; case 8: paleta.setIdlotep(cell.getRichStringCellValue().getString()); break; case 9: paleta.setIdcliente(cell.getRichStringCellValue().getString()); break; case 10: paleta.setIdclieprov(cell.getRichStringCellValue().getString()); break; case 11: paleta.setIdenvase(cell.getRichStringCellValue().getString()); break; case 12: paleta.setDescenvase(cell.getRichStringCellValue().getString()); break; case 13: paleta.setCerrado(cell.getRichStringCellValue().getString()); break; case 14: paleta.setNromanual(cell.getRichStringCellValue().getString()); break; } if (j < list.size() - 1) { System.out.print(", "); } } lista.add(paleta); System.out.println(""); } System.out.println("Terminado ..."); this.listPaleta = lista; RequestContext.getCurrentInstance().update("datos:tbl"); } } catch (Exception ex) { } }
From source file:com.quanticate.opensource.spreadsheetexcerpt.excerpt.POIExcerpterAndMerger.java
License:Apache License
private void excerpt(Workbook wb, List<Sheet> sheetsToKeep, OutputStream output) throws IOException { // Make the requested sheets be read only Set<String> keepNames = new HashSet<String>(); for (Sheet s : sheetsToKeep) { keepNames.add(s.getSheetName()); for (Row r : s) { for (Cell c : r) { if (c.getCellType() == Cell.CELL_TYPE_FORMULA) { switch (c.getCachedFormulaResultType()) { case Cell.CELL_TYPE_NUMERIC: double vd = c.getNumericCellValue(); c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellValue(vd); break; case Cell.CELL_TYPE_STRING: RichTextString vs = c.getRichStringCellValue(); c.setCellType(Cell.CELL_TYPE_STRING); c.setCellValue(vs); break; case Cell.CELL_TYPE_BOOLEAN: boolean vb = c.getBooleanCellValue(); c.setCellType(Cell.CELL_TYPE_BOOLEAN); c.setCellValue(vb); break; case Cell.CELL_TYPE_ERROR: c.setCellType(Cell.CELL_TYPE_BLANK); break; }// w ww. java 2 s . c om } } } } // Remove all the other sheets // Note - work backwards! Avoids order changing under us for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) { String name = wb.getSheetName(i); if (!keepNames.contains(name)) { wb.removeSheetAt(i); } } // Save wb.write(output); }
From source file:com.runwaysdk.dataaccess.io.excel.AttributeColumn.java
License:Open Source License
/** * Excel contains several different types of cells, with different getters. * This method checks the expected type, calls the appropriate getter on the * cell, then wraps the result in the correct java type for use in the * typesafe setter methods./*from ww w . j av a 2 s . c om*/ * * @param cell * @param column * @return */ public Object getValue(Cell cell) throws Exception { String type = this.javaType(); if (this.isEnum()) { String cellValue = ExcelUtil.getString(cell); Class<?> enumClass = LoaderDecorator.load(type); BusinessEnumeration[] values = (BusinessEnumeration[]) enumClass.getMethod("values").invoke(null); for (BusinessEnumeration value : values) { if (cellValue.equalsIgnoreCase(value.getDisplayLabel())) { return value; } } // We did not find a matching enum value. That is a problem. MdAttributeEnumerationDAO mdAttribute = (MdAttributeEnumerationDAO) this.getMdAttribute() .getMdAttributeConcrete(); throw new InvalidEnumerationName("devMessage", cellValue, mdAttribute.getMdEnumerationDAO()); } /* * Check for null values */ if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { return null; } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { switch (cell.getCachedFormulaResultType()) { case Cell.CELL_TYPE_STRING: String value = cell.getRichStringCellValue().getString(); if (value == null || value.length() == 0) { return null; } break; case Cell.CELL_TYPE_BLANK: return null; } } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { String value = cell.getRichStringCellValue().getString(); if (value == null || value.length() == 0) { return null; } } if (type.equals(String.class.getName())) { return ExcelUtil.getString(cell); } else if (type.equals(Long.class.getName())) { return new Long(new Double(cell.getNumericCellValue()).longValue()); } else if (type.equals(Float.class.getName())) { return new Float(new Double(cell.getNumericCellValue()).floatValue()); } else if (type.equals(Double.class.getName())) { return new Double(cell.getNumericCellValue()); } else if (type.equals(BigDecimal.class.getName())) { return new BigDecimal(cell.getNumericCellValue()); } else if (type.equals(Integer.class.getName())) { return new Integer(new Double(cell.getNumericCellValue()).intValue()); } else if (type.equals(Boolean.class.getName())) { return ExcelUtil.getBoolean(cell, (MdAttributeBooleanDAOIF) this.getMdAttribute().getMdAttributeConcrete()); } else if (type.equals(java.util.Date.class.getName())) { return cell.getDateCellValue(); } String error = "The type [" + type + "] is not supported as a parameter."; throw new ProgrammingErrorException(error); }
From source file:com.runwaysdk.dataaccess.io.excel.ErrorSheet.java
License:Open Source License
public void addRow(Row _row) { Row row = this.errorSheet.createRow(count++); row.setZeroHeight(_row.getZeroHeight()); row.setHeight(_row.getHeight());/*w ww .j a v a 2 s .c o m*/ CellStyle style = _row.getRowStyle(); if (style != null) { Workbook workbook = row.getSheet().getWorkbook(); CellStyle clone = workbook.createCellStyle(); clone.cloneStyleFrom(style); row.setRowStyle(clone); } Iterator<Cell> cellIterator = _row.cellIterator(); while (cellIterator.hasNext()) { Cell oldCell = cellIterator.next(); Cell newCell = row.createCell(oldCell.getColumnIndex()); int cellType = oldCell.getCellType(); if (cellType == Cell.CELL_TYPE_FORMULA) { cellType = oldCell.getCachedFormulaResultType(); } switch (cellType) { case Cell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getRichStringCellValue()); break; } } }
From source file:com.runwaysdk.dataaccess.io.excel.ExcelUtil.java
License:Open Source License
private static Boolean getBooleanInternal(Cell cell, String positiveLabel, String negativeLabel) { if (cell == null) { return Boolean.FALSE; }//from w ww. j a v a 2s . co m int cellType = cell.getCellType(); // In the case of formula, find out what type the formula will produce if (cellType == Cell.CELL_TYPE_FORMULA) { cellType = cell.getCachedFormulaResultType(); } if (cellType == Cell.CELL_TYPE_STRING) { String value = cell.getRichStringCellValue().getString().trim(); if (value.equalsIgnoreCase(positiveLabel) || value.equalsIgnoreCase("y") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("t") || value.equalsIgnoreCase("true") || value.equalsIgnoreCase("x")) { return Boolean.TRUE; } if (value.equalsIgnoreCase(negativeLabel) || value.equalsIgnoreCase("false") || value.equalsIgnoreCase("f") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("n") || value.length() == 0) { return Boolean.FALSE; } throw new AttributeValueException("[" + value + "] is not a recognized boolean in excel", value); } else if (cellType == Cell.CELL_TYPE_NUMERIC) { Double value = new Double(cell.getNumericCellValue()); return value.equals(new Double(1)); } else { return Boolean.valueOf(cell.getBooleanCellValue()); } }
From source file:com.runwaysdk.dataaccess.io.excel.ExcelUtil.java
License:Open Source License
public static String getString(Cell cell) { if (cell == null) { return null; }// www.jav a 2 s .com int cellType = cell.getCellType(); // In the case of formula, find out what type the formula will produce if (cellType == Cell.CELL_TYPE_FORMULA) { cellType = cell.getCachedFormulaResultType(); } if (cellType == Cell.CELL_TYPE_BLANK) { return ""; } else if (cellType == Cell.CELL_TYPE_NUMERIC) { return (new BigDecimal(cell.getNumericCellValue())).toString(); } else if (cellType == Cell.CELL_TYPE_BOOLEAN) { return new Boolean(cell.getBooleanCellValue()).toString(); } else { return cell.getRichStringCellValue().getString().trim(); } }
From source file:com.runwaysdk.dataaccess.io.excel.ExcelUtil.java
License:Open Source License
public static Integer getInteger(Cell cell) { if (cell == null) { return null; }/*from w w w.j a v a 2s . c om*/ int cellType = cell.getCellType(); // In the case of formula, find out what type the formula will produce if (cellType == Cell.CELL_TYPE_FORMULA) { cellType = cell.getCachedFormulaResultType(); } if (cellType == Cell.CELL_TYPE_BLANK) { return null; } else if (cellType == Cell.CELL_TYPE_NUMERIC) { return (new BigDecimal(cell.getNumericCellValue())).intValue(); } else { return Integer.parseInt(cell.getRichStringCellValue().getString().trim()); } }
From source file:com.runwaysdk.dataaccess.io.excel.FieldColumn.java
License:Open Source License
public Object getValue(Cell cell) throws Exception { int fieldType = this.getExpectedFieldType(); if (cell.getCellType() == fieldType) { return this.getCellValue(cell); } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { return null; } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { String value = cell.getRichStringCellValue().getString(); if (value == null || value.length() == 0) { return null; }/*from w w w . j a v a 2 s . c om*/ } int row = cell.getRowIndex(); String attributeName = this.getAttributeName(); String msg = "Conversion exception on row (" + row + ", " + attributeName + ") expected type [" + fieldType + "] actual type [" + cell.getCellType() + "]"; throw new FieldConversionException(msg, this); }
From source file:com.siberhus.tdfl.excel.DefaultExcelRowReader.java
License:Apache License
private Object getCellValue(Cell cell) { if (cell == null) return null; switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: RichTextString rts = cell.getRichStringCellValue(); if (rts != null) { return rts.getString(); }//from w w w .j av a2 s .c om return null; case Cell.CELL_TYPE_NUMERIC: String value = cell.toString(); /* * In POI we cannot know which cell is date or number because both * cells have numeric type To fix this problem we need to call * toString if it's number cell we can parse it but if it's date * cell we cannot parse the value with number parser */ try { return new BigDecimal(value); } catch (Exception e) { return cell.getDateCellValue(); } case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); } return null; }
From source file:com.ssy.havefun.f3d.F3DDaoImpl.java
public String getCellValue(Cell cell) { String ret = ""; switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: ret = ""; break;/*from ww w .ja va2 s .com*/ case Cell.CELL_TYPE_BOOLEAN: ret = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: ret = null; break; case Cell.CELL_TYPE_FORMULA: Workbook wb = cell.getSheet().getWorkbook(); CreationHelper crateHelper = wb.getCreationHelper(); FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator(); ret = getCellValue(evaluator.evaluateInCell(cell)); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { // Date theDate = cell.getDateCellValue(); // ret = simpleDateFormat.format(theDate); } else { ret = NumberToTextConverter.toText(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: ret = cell.getRichStringCellValue().getString(); break; default: ret = null; } return ret; //?trim }