List of usage examples for org.apache.poi.ss.usermodel Cell getNumericCellValue
double getNumericCellValue();
From source file:com.femsa.kof.csi.util.XlsAnalizer.java
/** * Mtodo encargado de la lectura y anlisis de una hoja del archivo excel * cargado en la interfaz grfica correspondiente a Rolling * * * @param rowIterator lista de renglones contenidos en la hoja de excel * @param usuario usuario que realiza el anlisis * @param sheetName nombre de la hoja de excel * @return Regresa una lista con los registros a ser almacenados en base de * datos/* ww w . j a va 2s . c o m*/ */ private List<Xtmpinddl> analizeSheetIndi(Iterator<Row> rowIterator, DcsUsuario usuario, String sheetName, List<DcsCatPais> paises, List<DcsCatIndicadores> indicadores) throws DCSException { int numRow = 0; List<Xtmpinddl> cargas = new ArrayList<Xtmpinddl>(); Xtmpinddl indi; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Calendar calendarioActual = Calendar.getInstance(); Calendar calendario = Calendar.getInstance(); end: while (rowIterator != null && rowIterator.hasNext()) { Row row = rowIterator.next(); Cell cell; if (numRow == 0) { } else { indi = new Xtmpinddl(); cell = row.getCell(0); if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { calendario.setTime(cell.getDateCellValue()); if ("User".equalsIgnoreCase(usuario.getFkIdRol().getRol()) && (calendarioActual.get(Calendar.YEAR) != calendario.get(Calendar.YEAR) || calendarioActual.get(Calendar.MONTH) != calendario.get(Calendar.MONTH))) { throw new DCSException( "Error: You can not load information of a different month of the current"); } indi.setFecha(sdf.format(cell.getDateCellValue())); } else if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) { try { calendario.setTime(sdf.parse(cell.getStringCellValue().trim())); if ("User".equalsIgnoreCase(usuario.getFkIdRol().getRol()) && (calendarioActual .get(Calendar.YEAR) != calendario.get(Calendar.YEAR) || calendarioActual.get(Calendar.MONTH) != calendario.get(Calendar.MONTH))) { throw new DCSException( "Error: You can not load information of a different month of the current"); } indi.setFecha(cell.getStringCellValue().trim()); } catch (ParseException ex) { Logger.getLogger(XlsAnalizer.class.getName()).log(Level.SEVERE, null, ex); errors.add("Approximately " + Character.toString((char) (65 + 0)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } } else { numRow++; continue; } cell = row.getCell(1); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) { indi.setGrupoInd(cell != null ? cell.getStringCellValue().trim() : null); } else { errors.add("Approximately " + Character.toString((char) (65 + 1)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(2); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) { if (indicadores.contains( new DcsCatIndicadores(cell != null ? cell.getStringCellValue().trim() : null))) { indi.setIndicador(cell != null ? cell.getStringCellValue().trim() : null); } else { errors.add("Approximately " + Character.toString((char) (65 + 2)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "], indicator not found."); cargas.clear(); break; } } else { errors.add("Approximately " + Character.toString((char) (65 + 2)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(3); if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) { if ("KOF".equalsIgnoreCase(cell.getStringCellValue().trim()) || paises.contains(new DcsCatPais(cell.getStringCellValue().trim()))) { indi.setPais(cell.getStringCellValue().trim()); } else { errors.add("Approximately " + Character.toString((char) (65 + 3)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } if ("User".equalsIgnoreCase(usuario.getFkIdRol().getRol()) && (!usuario.getPais().equalsIgnoreCase(indi.getPais()))) { throw new DCSException("Error: you can not load information from other country"); } } else { errors.add("Approximately " + Character.toString((char) (65 + 3)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(4); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING || cell.getCellType() == Cell.CELL_TYPE_BLANK) { indi.setCentro(cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK ? cell.getStringCellValue().trim() : null); } else { errors.add("Approximately " + Character.toString((char) (65 + 4)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(5); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING || cell.getCellType() == Cell.CELL_TYPE_BLANK) { indi.setRuta(cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK ? cell.getStringCellValue().trim() : null); } else { errors.add("Approximately " + Character.toString((char) (65 + 5)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(6); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { indi.setValorMensual(cell != null ? (float) cell.getNumericCellValue() : 0); } else { errors.add("Approximately " + Character.toString((char) (65 + 6)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(7); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { indi.setValorAcumulado(cell != null ? (float) cell.getNumericCellValue() : 0); } else { errors.add("Approximately " + Character.toString((char) (65 + 7)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cargas.add(indi); } numRow++; } return cargas; }
From source file:com.femsa.kof.csi.util.XlsAnalizer.java
/** * Mtodo encargado de la lectura y anlisis de una hoja del archivo excel * cargado en la interfaz grfica correspondiente a flota * * * @param rowIterator lista de renglones contenidos en la hoja de excel * @param usuario usuario que realiza el anlisis * @param sheetName nombre de la hoja de excel * @return Regresa una lista con los registros a ser almacenados en base de * datos/*from w w w .ja v a2 s .c om*/ */ private List<XtmpinddlFlota> analizeSheetFlota(Iterator<Row> rowIterator, DcsUsuario usuario, String sheetName, List<DcsCatPais> paises) throws DCSException { int numRow = 0; List<XtmpinddlFlota> cargas = new ArrayList<XtmpinddlFlota>(); XtmpinddlFlota flota; SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); Calendar calendario = Calendar.getInstance(); end: while (rowIterator != null && rowIterator.hasNext()) { Row row = rowIterator.next(); Cell cell; if (numRow == 0) { } else { flota = new XtmpinddlFlota(); cell = row.getCell(0); if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { try { calendario.setTime(sdf.parse(((int) cell.getNumericCellValue()) + "")); } catch (ParseException ex) { Logger.getLogger(XlsAnalizer.class.getName()).log(Level.SEVERE, null, ex); errors.add("Approximately " + Character.toString((char) (65 + 0)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } flota.setAnio(calendario.get(Calendar.YEAR)); } else if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) { try { calendario.setTime(sdf.parse(cell.getStringCellValue().trim())); } catch (ParseException ex) { Logger.getLogger(XlsAnalizer.class.getName()).log(Level.SEVERE, null, ex); errors.add("Approximately " + Character.toString((char) (65 + 0)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } flota.setAnio(calendario.get(Calendar.YEAR)); } else { numRow++; continue; } cell = row.getCell(1); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) { if ("KOF".equalsIgnoreCase(cell != null ? cell.getStringCellValue().trim() : "") || paises .contains(new DcsCatPais(cell != null ? cell.getStringCellValue().trim() : ""))) { flota.setPais(cell != null ? cell.getStringCellValue().trim() : null); } else { errors.add("Approximately " + Character.toString((char) (65 + 1)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } if ("User".equalsIgnoreCase(usuario.getFkIdRol().getRol()) && (!usuario.getPais().equalsIgnoreCase(flota.getPais()))) { throw new DCSException("Error: you can not load information from other country"); } } else { errors.add("Approximately " + Character.toString((char) (65 + 1)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(2); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) { flota.setTipo(cell != null ? cell.getStringCellValue().trim() : null); } else { errors.add("Approximately " + Character.toString((char) (65 + 2)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(3); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) { flota.setEdad(cell != null ? cell.getStringCellValue().trim() : null); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { flota.setEdad((int) cell.getNumericCellValue() + ""); } else { errors.add("Approximately " + Character.toString((char) (65 + 3)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cell = row.getCell(4); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) { flota.setCantidad(cell != null ? Integer.parseInt(cell.getStringCellValue().trim()) : null); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { flota.setCantidad((int) cell.getNumericCellValue()); } else { errors.add("Approximately " + Character.toString((char) (65 + 4)) + "" + (numRow + 1) + " cell in " + sheetName + " sheet have a invalid value [" + cell + "]."); cargas.clear(); break; } cargas.add(flota); } numRow++; } return cargas; }
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 ww w . ja v a 2 s . c o m 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.framework.common.ExcelSpreadsheet.java
public List<String> getSpreadSheetAsArray() { List<String> cellValue = new ArrayList<String>(); // Iterate through each rows from first sheet Iterator<Row> rowIterator = requiredWorksheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next();/*from w w w. j a v a 2 s. co m*/ // 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"); if (cell.getBooleanCellValue()) { cellValue.add("TRUE"); } else { cellValue.add("FALSE"); } break; case Cell.CELL_TYPE_NUMERIC: // System.out.print(cell.getNumericCellValue() + "\t\t"); if (HSSFDateUtil.isCellDateFormatted(cell)) { Format formatter = new SimpleDateFormat("yyyy-MM-dd"); String s = formatter.format(cell.getDateCellValue()); cellValue.add(s); } else { double value = cell.getNumericCellValue(); cellValue.add(Double.toString(value)); } break; case Cell.CELL_TYPE_STRING: // System.out.print(cell.getStringCellValue() + "\t\t"); cellValue.add(cell.getStringCellValue()); break; } } // System.out.println(""); } return cellValue; }
From source file:com.FuntionLibrary.java
public Boolean isCellContentPresent(XSSFSheet sheet, String content) { Boo = false;// w ww. j a va 2 s. c o m try { String CellContent = null; // Get iterator to all the rows in current sheet Iterator<Row> rowIterator = sheet.iterator(); // Traversing over each row of XLSX file while (rowIterator.hasNext()) { if (Boo == true) break; Row row = rowIterator.next(); // For each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { if (Boo == true) break; Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t"); CellContent = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); CellContent = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t"); CellContent = cell.getStringCellValue(); break; default: //CellContent = null; } if (CellContent.equalsIgnoreCase(content)) { if (MainGui.EditExistingButtonClicked == true) { break; } Boo = true; break; //else { // throw new Exception("Data Already exists"); // } } else { Boo = false; } } System.out.println(""); if (MainGui.EditExistingButtonClicked == true && CellContent.equalsIgnoreCase(content)) { break; } } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } return Boo; }
From source file:com.funtl.framework.smoke.core.commons.excel.ImportExcel.java
License:Apache License
/** * ??//from w ww. j a va 2 s .c om * * @param row ? * @param column ??? * @return ? */ @SuppressWarnings("deprecation") public Object getCellValue(Row row, int column) { Object val = ""; try { Cell cell = row.getCell(column); if (cell != null) { if (cell.getCellTypeEnum() == CellType.NUMERIC) { val = cell.getNumericCellValue(); } else if (cell.getCellTypeEnum() == CellType.STRING) { val = cell.getStringCellValue(); } else if (cell.getCellTypeEnum() == CellType.FORMULA) { val = cell.getCellFormula(); } else if (cell.getCellTypeEnum() == CellType.BOOLEAN) { val = cell.getBooleanCellValue(); } else if (cell.getCellTypeEnum() == CellType.ERROR) { val = cell.getErrorCellValue(); } } } catch (Exception e) { return val; } return val; }
From source file:com.github.camaral.sheeco.exceptions.SpreadsheetViolation.java
License:Apache License
private static Object getCellValue(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return cell.getDateCellValue(); }//from w w w. j av a2 s. c o m return cell.getNumericCellValue(); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_ERROR: return cell.getErrorCellValue(); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); default: throw new UnsupportedOperationException("CellType " + cell.getCellType() + " is invalid"); } }
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 www .ja v a2 s . c o m*/ 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.SpreadsheetDoubleAdapter.java
License:Apache License
@Override public Double fromSpreadsheet(final Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: try {/*w w w. jav a2s . 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 {/*w w w . ja v a 2 s.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"); } }