List of usage examples for org.apache.poi.ss.usermodel Cell getRichStringCellValue
RichTextString getRichStringCellValue();
For numeric cells we throw an exception.
From source file:cn.study.innerclass.PoiUtil.java
License:Open Source License
public static Object getCellData(Cell cell, FormulaEvaluator formula) { if (cell == null) { return null; }//from w w w .j a va2 s .com switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println(cell.getRichStringCellValue().getString()); return cell.getRichStringCellValue().getString(); case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.println(cell.getDateCellValue()); return cell.getDateCellValue(); } else { System.out.println(cell.getNumericCellValue()); return cell.getNumericCellValue(); } case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); return cell.getBooleanCellValue(); case Cell.CELL_TYPE_FORMULA: System.out.println(cell.getStringCellValue()); switch (formula.evaluate(cell).getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println(formula.evaluate(cell).getStringValue()); return formula.evaluate(cell).getStringValue(); case Cell.CELL_TYPE_NUMERIC: System.out.println(formula.evaluate(cell).getNumberValue()); return formula.evaluate(cell).getNumberValue(); case Cell.CELL_TYPE_BOOLEAN: System.out.println(formula.evaluate(cell).getBooleanValue()); return formula.evaluate(cell); } default: return null; } }
From source file:co.foldingmap.data.ExcelDataConnector.java
License:Open Source License
/** * Returns a cell value as a DataCell object. * /*from w ww . ja v a2 s .c om*/ * @param cell * @return */ public DataCell getCellText(Cell cell) { DataCell cellText; switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: cellText = new DataCell(cell.getRichStringCellValue().getString()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { cellText = new DataCell(cell.getDateCellValue().toString()); } else { cellText = new DataCell(Double.toString(cell.getNumericCellValue())); } break; case Cell.CELL_TYPE_BOOLEAN: cellText = new DataCell(Boolean.toString(cell.getBooleanCellValue())); break; case Cell.CELL_TYPE_FORMULA: cellText = new DataCell(cell.getCellFormula()); break; default: cellText = new DataCell(""); } return cellText; }
From source file:com.alibaba.ims.platform.util.ExcelUtil.java
License:Open Source License
private static String getCellValue(Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: return cell.getRichStringCellValue().getString(); case Cell.CELL_TYPE_NUMERIC: if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) { return DateUtil.format(cell.getDateCellValue(), "yyyy-MM-dd"); } else {// w w w . jav a 2 s . c om return new DecimalFormat("0").format(cell.getNumericCellValue()); } case Cell.CELL_TYPE_BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); default: return null; } }
From source file:com.aurel.track.exchange.excel.ExcelImportBL.java
License:Open Source License
/** * Gets the string value of a cell//w ww . ja v a2 s .c o m * * @param cell * @return */ static String getStringCellValue(Cell cell) { try { int cellType = cell.getCellType(); switch (cellType) { case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case Cell.CELL_TYPE_ERROR: return String.valueOf(cell.getErrorCellValue()); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_NUMERIC: try { double doubleValue = cell.getNumericCellValue(); int intValue = (int) doubleValue; double fracPart = doubleValue - intValue; if (Math.abs(fracPart) <= Double.MIN_VALUE) { return String.valueOf(intValue); } else { return String.valueOf(doubleValue); } } catch (Exception e) { } case Cell.CELL_TYPE_STRING: RichTextString richTextString = cell.getRichStringCellValue(); if (richTextString != null) { return richTextString.getString(); } } } catch (Exception e) { LOGGER.debug("Getting the string value failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } return ""; }
From source file:com.aurel.track.exchange.excel.ExcelImportBL.java
License:Open Source License
/** * Gets the Integer value of a cell//from w ww .j av a2 s .c om * * @param cell * @return */ private static Integer getIntegerCellValue(Cell cell) throws ExcelImportInvalidCellValueException { Integer integerValue = null; int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_NUMERIC) { Double doubleValue = null; try { double numericValue = cell.getNumericCellValue(); doubleValue = new Double(numericValue); integerValue = new Integer(doubleValue.intValue()); } catch (Exception e) { if (doubleValue == null) { doubleValue = new Double(Double.NaN); } throw new ExcelImportInvalidCellValueException(doubleValue.toString()); } } else { if (cellType == Cell.CELL_TYPE_STRING) { RichTextString richTextString = null; richTextString = cell.getRichStringCellValue(); if (richTextString != null) { String stringValue = richTextString.getString(); if (stringValue != null && !"".equals(stringValue)) { stringValue = stringValue.trim(); if (stringValue != null) { try { integerValue = Integer.valueOf(stringValue); } catch (Exception e) { throw new ExcelImportInvalidCellValueException(stringValue); } } } } } else { throw new ExcelImportInvalidCellValueException(getStringCellValue(cell)); } } return integerValue; }
From source file:com.aurel.track.exchange.excel.ExcelImportBL.java
License:Open Source License
/** * Gets the Double value of a cell/* w w w. j a v a2s.co m*/ * * @param cell * @return */ private static Double getDoubleCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException { Double doubleValue = null; int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_NUMERIC) { double numericValue = cell.getNumericCellValue(); try { doubleValue = new Double(numericValue); } catch (Exception e) { throw new ExcelImportInvalidCellValueException(String.valueOf(numericValue)); } } else { if (cellType == Cell.CELL_TYPE_STRING) { RichTextString richTextString = cell.getRichStringCellValue(); if (richTextString != null) { String stringValue = richTextString.getString(); if (stringValue != null) { stringValue = stringValue.trim(); if (!"".equals(stringValue)) { doubleValue = DoubleNumberFormatUtil.getInstance().parseGUI(stringValue, locale); if (doubleValue == null) { doubleValue = DoubleNumberFormatUtil.parseISO(stringValue); if (doubleValue == null) { throw new ExcelImportInvalidCellValueException(stringValue); } } } } } } else { throw new ExcelImportInvalidCellValueException(getStringCellValue(cell)); } } return doubleValue; }
From source file:com.aurel.track.exchange.excel.ExcelImportBL.java
License:Open Source License
/** * Gets the Double value of a cell// w ww. j a v a2 s .c o m * * @param cell * @return */ private static Date getDateCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException { Date dateValue = null; int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_NUMERIC) { try { dateValue = cell.getDateCellValue(); } catch (Exception e) { throw new ExcelImportInvalidCellValueException(String.valueOf(cell.getNumericCellValue())); } } else { if (cellType == Cell.CELL_TYPE_STRING) { RichTextString richTextString = cell.getRichStringCellValue(); if (richTextString != null) { String stringValue = richTextString.getString(); if (stringValue != null) { stringValue = stringValue.trim(); if (!"".equals(stringValue)) { dateValue = DateTimeUtils.getInstance().parseGUIDate(stringValue, locale); if (dateValue == null) { dateValue = DateTimeUtils.getInstance().parseShortDate(stringValue, locale); if (dateValue == null) { dateValue = DateTimeUtils.getInstance().parseISODate(stringValue); if (dateValue == null) { throw new ExcelImportInvalidCellValueException(stringValue); } } } } } } } else { throw new ExcelImportInvalidCellValueException(getStringCellValue(cell)); } } return dateValue; }
From source file:com.aurel.track.exchange.excel.ExcelImportBL.java
License:Open Source License
/** * Gets the Double value of a cell/*ww w . ja v a 2s .c om*/ * * @param cell * @return */ private static Date getDateTimeCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException { Date dateValue = null; int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_NUMERIC) { try { dateValue = cell.getDateCellValue(); } catch (Exception e) { throw new ExcelImportInvalidCellValueException(String.valueOf(cell.getNumericCellValue())); } } else { if (cellType == Cell.CELL_TYPE_STRING) { RichTextString richTextString = cell.getRichStringCellValue(); if (richTextString != null) { String stringValue = richTextString.getString(); if (stringValue != null) { stringValue = stringValue.trim(); if (!"".equals(stringValue)) { dateValue = DateTimeUtils.getInstance().parseGUIDateTime(stringValue, locale); if (dateValue == null) { dateValue = DateTimeUtils.getInstance().parseShortDateTime(stringValue, locale); if (dateValue == null) { dateValue = DateTimeUtils.getInstance().parseISODateTime(stringValue); } } } } } } else { throw new ExcelImportInvalidCellValueException(getStringCellValue(cell)); } } return dateValue; }
From source file:com.aurel.track.exchange.excel.ExcelImportBL.java
License:Open Source License
/** * Gets the Double value of a cell/* ww w. j a v a 2s .com*/ * * @param cell * @return */ private static Boolean getBooleanCellValue(Cell cell) throws ExcelImportInvalidCellValueException { Boolean booleanValue = null; int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_BOOLEAN) { boolean boolCellValue = cell.getBooleanCellValue(); booleanValue = new Boolean(boolCellValue); } else { if (cellType == Cell.CELL_TYPE_STRING) { RichTextString richTextString = cell.getRichStringCellValue(); if (richTextString != null) { String stringValue = richTextString.getString(); if (stringValue != null) { stringValue = stringValue.trim(); if (!"".equals(stringValue)) { if ("true".equalsIgnoreCase(stringValue) || BooleanFields.TRUE_VALUE.equalsIgnoreCase(stringValue)) { booleanValue = new Boolean(true); } else { if ("false".equalsIgnoreCase(stringValue) || BooleanFields.FALSE_VALUE.equalsIgnoreCase(stringValue)) { booleanValue = new Boolean(false); } else { if (stringValue != null && !"".equals(stringValue.trim())) { throw new ExcelImportInvalidCellValueException(stringValue); } } } } } } } else { throw new ExcelImportInvalidCellValueException(getStringCellValue(cell)); } } return booleanValue; }
From source file:com.b2international.snowowl.snomed.core.refset.automap.XlsParser.java
License:Apache License
/** * @param cell//from ww w . jav a2s . c o m * @return the textual representation of the cell or empty string if the cell is empty (null) */ private String getStringValue(Cell cell) { String value = ""; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: value = cell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = cell.getDateCellValue().toString(); } else { value = Integer.toString((int) cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: value = Boolean.toString(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: value = cell.getCellFormula(); break; } } return value; }