List of usage examples for org.apache.poi.ss.usermodel Cell getBooleanCellValue
boolean getBooleanCellValue();
From source file:org.ujmp.poi.AbstractMatrixExcelImporter.java
License:Open Source License
public DenseObjectMatrix2D importFromSheet(final Sheet sheet) throws InvalidFormatException, IOException { final int rowCount = sheet.getLastRowNum(); int columnCount = 0; Iterator<Row> rowIterator = sheet.rowIterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next();/* ww w .ja va2 s . c o m*/ if (row.getLastCellNum() > columnCount) { columnCount = row.getLastCellNum(); } } final DefaultDenseObjectMatrix2D matrix = new DefaultDenseObjectMatrix2D(rowCount, columnCount); matrix.setLabel(sheet.getSheetName()); for (int r = 0; r < rowCount; r++) { Row row = sheet.getRow(r); if (row != null) { for (int c = 0; c < columnCount; c++) { Cell cell = row.getCell(c); if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: matrix.setAsBoolean(cell.getBooleanCellValue(), r, c); break; case Cell.CELL_TYPE_ERROR: break; case Cell.CELL_TYPE_FORMULA: matrix.setAsString(cell.getCellFormula(), r, c); break; case Cell.CELL_TYPE_NUMERIC: matrix.setAsDouble(cell.getNumericCellValue(), r, c); break; case Cell.CELL_TYPE_STRING: matrix.setAsString(cell.getStringCellValue(), r, c); break; default: break; } } } } } return matrix; }
From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java
public static Object getCelValue(Cell cell) { Object cellValue = null;//from w w w . j a v a 2 s . c om switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cellValue = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { cellValue = cell.getDateCellValue(); } else { cellValue = cell.getNumericCellValue(); } break; case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BLANK: cellValue = new String(""); break; case Cell.CELL_TYPE_ERROR: cellValue = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_FORMULA: cellValue = cell.getCellFormula(); break; } return cellValue; }
From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java
public static Object getWithIntCelValue(Cell cell) { Object cellValue = null;// ww w. ja v a 2 s .c o m switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cellValue = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { cellValue = cell.getDateCellValue(); } else { cellValue = cell.getNumericCellValue(); Double i = (Double) cellValue; cellValue = i.intValue(); } break; case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BLANK: cellValue = new String(""); break; case Cell.CELL_TYPE_ERROR: cellValue = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_FORMULA: cellValue = cell.getCellFormula(); break; } return cellValue; }
From source file:org.unhcr.eg.odk.utilities.xlsform.excel.ExcelFileUtility.java
protected static void copyContent(Sheet sheetSource, Sheet sheetDestination) { //Iterate through each rows from first sheet Iterator<Row> rowIterator = sheetSource.iterator(); int i = 0;//from w w w . j av a 2s .c om while (rowIterator.hasNext()) { Row row = rowIterator.next(); Row rowDestination = sheetDestination.createRow(i); i++; //For each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); int j = 0; while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); Cell cellDestination = rowDestination.createCell(j); j++; cellDestination.setCellComment(cell.getCellComment()); // cellDestination.setCellStyle(cell.getCellStyle()); cellDestination.setCellType(cell.getCellType()); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cellDestination.setCellValue(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { cellDestination.setCellValue(cell.getDateCellValue()); } else { cellDestination.setCellValue(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: cellDestination.setCellValue(cell.getRichStringCellValue()); break; case Cell.CELL_TYPE_BLANK: cellDestination.setCellValue(cell.getStringCellValue()); break; case Cell.CELL_TYPE_ERROR: cellDestination.setCellValue(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: cellDestination.setCellFormula(cell.getCellFormula()); break; } } } }
From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java
License:Open Source License
protected String getCellValueAsString(Cell cell, int type) { if (cell != null) { switch (type) { case Cell.CELL_TYPE_ERROR: { return "ERROR" + cell.getErrorCellValue(); }/* w w w . j a v a2s . com*/ case Cell.CELL_TYPE_BOOLEAN: { return "" + cell.getBooleanCellValue(); } case Cell.CELL_TYPE_NUMERIC: { if (DateUtil.isCellDateFormatted(cell)) { return dateFormat.format(cell.getDateCellValue()); } else { double value = cell.getNumericCellValue(); String formatString = cell.getCellStyle().getDataFormatString(); int formatIndex = cell.getCellStyle().getDataFormat(); return formatter.formatRawCellContents(value, formatIndex, formatString); } } case Cell.CELL_TYPE_STRING: { return cell.getRichStringCellValue().getString(); } } } return null; }
From source file:org.waterforpeople.mapping.dataexport.FixedFormatRawDataImporter.java
License:Open Source License
@Override public void executeImport(File file, String serverBase, Map<String, String> criteria) { try {/*from w w w. jav a2 s . c o m*/ DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z"); setSurveyId(criteria); Sheet sheet1 = getDataSheet(file); for (Row row : sheet1) { String localeId = null; String dateString = null; StringBuilder sb = new StringBuilder(); StringBuilder valueBuilder = new StringBuilder(); int valueCount = 0; sb.append("action=" + RawDataImportRequest.SAVE_FIXED_FIELD_SURVEY_INSTANCE_ACTION + "&" + RawDataImportRequest.SURVEY_ID_PARAM + "=" + getSurveyId() + "&"); for (Cell cell : row) { if (cell.getColumnIndex() == 0 && cell.getRowIndex() > 0) { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { localeId = new Double(cell.getNumericCellValue()).intValue() + ""; sb.append(RawDataImportRequest.LOCALE_ID_PARAM + "=" + localeId + "&"); } } if (cell.getColumnIndex() == 1 && cell.getRowIndex() > 0) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { dateString = cell.getStringCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()); dateString = df.format(date); } if (dateString != null) { sb.append(RawDataImportRequest.COLLECTION_DATE_PARAM + "=" + URLEncoder.encode(dateString, "UTF-8") + "&"); } } String value = null; boolean hasValue = false; if (cell.getRowIndex() > 0 && cell.getColumnIndex() > 1) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { value = cell.getStringCellValue().trim(); if (value.contains("|")) { value = value.replaceAll("\\|", "^^"); } sb.append(URLEncoder.encode(value, "UTF-8")); hasValue = true; } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { value = new Double(cell.getNumericCellValue()).toString().trim(); hasValue = true; } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { value = new Boolean(cell.getBooleanCellValue()).toString().trim(); hasValue = true; } } if (hasValue) { if (valueCount > 0) { valueBuilder.append(RawDataImportRequest.FIELD_VAL_DELIMITER); } valueBuilder.append(value); valueCount++; } } if (valueCount > 0) { sb.append(RawDataImportRequest.FIXED_FIELD_VALUE_PARAM + "=" + valueBuilder.toString()); invokeUrl(serverBase, sb.toString(), true, criteria.get(KEY_PARAM)); } } } catch (Exception e) { e.printStackTrace(); } finally { cleanup(); } }
From source file:org.waterforpeople.mapping.dataexport.RawDataSpreadsheetImporter.java
License:Open Source License
private String parseCellAsString(Cell cell) { String val = null; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: val = cell.getBooleanCellValue() + ""; break; case Cell.CELL_TYPE_NUMERIC: val = cell.getNumericCellValue() + ""; break; default:/*from www .ja va 2s. c o m*/ val = cell.getStringCellValue(); break; } } return val; }
From source file:org.waterforpeople.mapping.dataexport.SurveySpreadsheetImporter.java
License:Open Source License
/** * validates a boolean field. We have to try reading it as both a boolean and a string column * because once we encounter 1 non-boolean, it changes the underlying model for the remainder of * the spreadsheet.// w w w . j av a2s . c o m * * @param cell * @return */ private boolean validateBooleanField(Cell cell) { try { cell.getBooleanCellValue(); } catch (Exception e) { try { if (cell.getStringCellValue().trim().length() > 0) { if (!("TRUE".equalsIgnoreCase(cell.getStringCellValue().trim()) || "FALSE".equalsIgnoreCase(cell.getStringCellValue().trim()))) { return false; } } } catch (Exception ex) { ex.printStackTrace(); } } return true; }
From source file:org.works.batch.item.excel.poi.PoiSheet.java
License:Apache License
/** * {@inheritDoc}//from w w w . j a v a2s .c o m */ public String[] getRow(final int rowNumber) { int previousCell = -1; int currentCell = 0; if (rowNumber > this.delegate.getLastRowNum()) { return null; } final Row row = this.delegate.getRow(rowNumber); final List<String> cells = new LinkedList<String>(); final Iterator<Cell> cellIter = row.iterator(); while (cellIter.hasNext()) { final Cell cell = cellIter.next(); currentCell = cell.getColumnIndex(); if (previousCell == currentCell - 1) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: cells.add(String.valueOf(cell.getNumericCellValue())); break; case Cell.CELL_TYPE_BOOLEAN: cells.add(String.valueOf(cell.getBooleanCellValue())); break; case Cell.CELL_TYPE_STRING: cells.add(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BLANK: cells.add(cell.getStringCellValue()); break; default: cells.add(StringUtils.EMPTY); } } else { cells.add(StringUtils.EMPTY); } previousCell = currentCell; } return cells.toArray(new String[cells.size()]); }
From source file:org.wso2.carbon.dataservices.core.description.query.ExcelQuery.java
License:Open Source License
private String[] extractRowData(Row row) { if (row == null || row.getLastCellNum() == -1) { return null; }//from w ww. j a v a2 s.co m String[] data = new String[row.getLastCellNum()]; Cell cell; for (int i = 0; i < data.length; i++) { cell = row.getCell(i); if (cell == null) { data[i] = ""; continue; } switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: data[i] = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_BLANK: data[i] = ""; break; case HSSFCell.CELL_TYPE_BOOLEAN: data[i] = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: data[i] = "{formula}"; break; case HSSFCell.CELL_TYPE_NUMERIC: data[i] = processNumericValue(cell.getNumericCellValue()); break; } } return data; }