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.dituiba.excel.ImportTableService.java
License:Apache License
/** * ExcelTableBean/*from w w w . j a va 2 s . co m*/ */ public void doImport() { int rowNum = sheet.getLastRowNum() + 1; int columnNum = 0; for (int i = 0; i < rowNum; i++) { if (sheet.getRow(i) != null) { int last = sheet.getRow(i).getLastCellNum(); columnNum = last > columnNum ? last : columnNum; } } tableBean = new TableBean(rowNum, columnNum); Collection<CellBean> cellBeans = new ArrayList<CellBean>(); for (int r = startRow; r < rowNum; r++) { Row row = sheet.getRow(r); if (row != null) { for (int c = 0; c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null) { String cellValue = null; if (cellHandlerMap.containsKey(c)) { cellValue = cellHandlerMap.get(c).readCell(cell) + ""; } else { cell.setCellType(Cell.CELL_TYPE_STRING); Integer type = forceCellType.get(c); if (type != null) { cell.setCellType(type); } if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) { cellValue = cell.getBooleanCellValue() + ""; } else if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) { try { cellValue = String.valueOf(cell.getNumericCellValue()); } catch (IllegalStateException e) { cellValue = String.valueOf(cell.getRichStringCellValue()).trim(); } } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { if (DateUtil.isCellDateFormatted(cell)) { Date date2 = cell.getDateCellValue(); SimpleDateFormat dff = new SimpleDateFormat(dateFormat); cellValue = dff.format(date2); // } else { cellValue = String.valueOf(cell.getNumericCellValue()); } } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) { cellValue = cell.getStringCellValue(); } if (cellValue != null && cellValue instanceof String) { cellValue = cellValue.toString().trim(); } } CellBean cellBean = new CellBean(cellValue, r, c); cellBean.setCell(cell); cellBeans.add(cellBean); } } } } tableBean.setCellBeans(cellBeans); }
From source file:com.fjn.helper.common.io.file.office.excel.ListExcelSheetReader.java
License:Apache License
private <T> T getBean(Class<T> clazz, Row row, List<String> fieldList) { Field[] fields = new Field[fieldList.size()]; for (int i = 0; i < fields.length; i++) { Field field = null;//from w w w.jav a 2s . com try { field = clazz.getDeclaredField(fieldList.get(i)); // field.getAnnotation(); fields[i] = field; field.setAccessible(true); } catch (Exception e) { e.printStackTrace(); } } T t = null; try { t = clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < fields.length; i++) { Cell cell = null; cell = row.getCell(i); if (cell != null) { int cellType = cell.getCellType(); Object value = null; switch (cellType) { case Cell.CELL_TYPE_STRING: value = cell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = cell.getDateCellValue(); } else { value = cell.getNumericCellValue(); Class fieldClass = fields[i].getType(); if (fieldClass == Integer.class || fieldClass == int.class) { value = ((Double) value).intValue(); } else if (fieldClass == Short.class || fieldClass == short.class) { value = ((Double) value).shortValue(); } else if (fieldClass == Byte.class || fieldClass == byte.class) { value = ((Double) value).byteValue(); } else if (fieldClass == Long.class || fieldClass == long.class) { value = ((Double) value).longValue(); } else if (fieldClass == Float.class || fieldClass == float.class) { value = ((Double) value).floatValue(); } else if (fieldClass == Double.class || fieldClass == double.class) { value = (Double) value; } } break; case Cell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_FORMULA: value = cell.getCellFormula(); break; default: break; } try { fields[i].set(t, value); } catch (Exception e) { e.printStackTrace(); } } } return t; }
From source file:com.FuntionLibrary.java
public static String[] getData(int r, XSSFSheet sheet) { String[] str = new String[6]; XSSFRow row = sheet.getRow(r);// w ww .jav a2 s . c o m int i = 0; for (Cell cell : row) { str[i] = cell.getRichStringCellValue().getString().trim(); i++; if (i == 6) break; } return str; }
From source file:com.FuntionLibrary.java
public static int findRow(XSSFSheet sheet, String cellContent) { try {//w w w . ja va 2s.c o m for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) { return row.getRowNum(); } } else { JOptionPane.showMessageDialog(null, cellContent + " is not a valid Please try again !"); } } } } catch (Exception e) { JOptionPane.showMessageDialog(null, cellContent + " is not a valid Please try again !"); } return 0; }
From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetBooleanAdapter.java
License:Apache License
@Override public Boolean fromSpreadsheet(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: final String value = cell.getRichStringCellValue().getString().trim(); Boolean ret = null;/*from w ww .java 2 s . c om*/ for (final String str : TRUE_VALUES) { if (str.equalsIgnoreCase(value)) { ret = Boolean.TRUE; break; } } if (ret == null) { for (final String str : FALSE_VALUES) { if (str.equalsIgnoreCase(value)) { ret = Boolean.FALSE; break; } } } if (ret == null) { throw new InvalidCellValueException(); } return ret; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { throw new InvalidCellValueException(); } return cell.getNumericCellValue() > 0 ? Boolean.TRUE : Boolean.FALSE; case Cell.CELL_TYPE_BOOLEAN: return Boolean.valueOf(cell.getBooleanCellValue()); case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_ERROR: case Cell.CELL_TYPE_FORMULA: default: throw new InvalidCellFormatException( "The cell type: " + cell.getCellType() + " is either not supported or not possible"); } }
From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetDateAdapter.java
License:Apache License
@Override public Date fromSpreadsheet(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return cell.getDateCellValue(); } else {/* w w w .ja va2s . c o m*/ throw new InvalidCellValueException(); } case Cell.CELL_TYPE_STRING: for (final SimpleDateFormat format : DATE_PATTERNS) { try { return format.parse(cell.getRichStringCellValue().getString().trim()); } catch (final ParseException e) { continue; } } throw new InvalidCellValueException(); case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: throw new InvalidCellValueException(); case Cell.CELL_TYPE_ERROR: case Cell.CELL_TYPE_FORMULA: default: throw new InvalidCellFormatException( "The cell type: " + cell.getCellType() + " is either not supported or not possible"); } }
From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetDoubleAdapter.java
License:Apache License
@Override public Double fromSpreadsheet(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: try {//from w ww. j av a 2 s . c o m // remove trailing spaces and replace comma with period final String value = cell.getRichStringCellValue().getString().trim().replace(',', '.'); return Double.valueOf(value); } catch (final NumberFormatException e) { throw new InvalidCellValueException(); } case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { throw new InvalidCellValueException(); } else { return Double.valueOf(cell.getNumericCellValue()); } case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: throw new InvalidCellValueException(); case Cell.CELL_TYPE_ERROR: case Cell.CELL_TYPE_FORMULA: default: throw new InvalidCellFormatException( "The cell type: " + cell.getCellType() + " is either not supported or not possible"); } }
From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetIntegerAdapter.java
License:Apache License
@Override public Integer fromSpreadsheet(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: try {//from ww w .j a v a2s . c o m return Integer.valueOf(cell.getRichStringCellValue().getString().trim()); } catch (final NumberFormatException e) { throw new InvalidCellValueException(); } case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { throw new InvalidCellValueException(); } else { final double value = cell.getNumericCellValue(); final double floor = Math.floor(value); if (value == floor) { return Integer.valueOf((int) value); } else { throw new InvalidCellValueException(); } } case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: case Cell.CELL_TYPE_ERROR: case Cell.CELL_TYPE_FORMULA: default: throw new InvalidCellFormatException( "The cell type: " + cell.getCellType() + " is either not supported or not possible"); } }
From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetLongAdapter.java
License:Apache License
@Override public Long fromSpreadsheet(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: try {/*from www .j a va 2 s . c om*/ return Long.valueOf(cell.getRichStringCellValue().getString().trim()); } catch (final NumberFormatException e) { throw new InvalidCellValueException(); } case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { throw new InvalidCellValueException(); } else { final double value = cell.getNumericCellValue(); final double floor = Math.floor(value); if (value == floor) { return Long.valueOf((long) value); } else { throw new InvalidCellValueException(); } } case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: case Cell.CELL_TYPE_ERROR: case Cell.CELL_TYPE_FORMULA: default: throw new InvalidCellFormatException( "The cell type: " + cell.getCellType() + " is either not supported or not possible"); } }
From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetStringAdapter.java
License:Apache License
@Override public String fromSpreadsheet(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: final RichTextString text = cell.getRichStringCellValue(); final String value = text.getString().trim(); return !value.isEmpty() ? value : null; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return String.valueOf(SpreadsheetDateAdapter.DATE_PATTERNS[0].format(cell.getDateCellValue())); } else {/*from ww w.jav a 2 s. c om*/ return decimalFormat.format(cell.getNumericCellValue()); } case Cell.CELL_TYPE_BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_ERROR: case Cell.CELL_TYPE_FORMULA: default: throw new InvalidCellFormatException( "The cell type: " + cell.getCellType() + " is either not supported or not possible"); } }