List of usage examples for org.apache.poi.ss.usermodel Cell getBooleanCellValue
boolean getBooleanCellValue();
From source file:com.ncc.excel.ExcelUtil.java
License:Apache License
public static String getCellValue(Cell cell) { // private String getCellValue(Cell cell){ String str = null;//from ww w .ja v a2 s .c o m if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: //0 //?? 1. 2. if (HSSFDateUtil.isCellDateFormatted(cell)) { //?? str = DateUtil.formatDateForExcelDate(cell.getDateCellValue()); } else { double dValue = cell.getNumericCellValue(); //E if (String.valueOf(dValue).contains("E")) { str = new DecimalFormat("#").format(dValue); } else { str = String.valueOf(dValue); } } break; case Cell.CELL_TYPE_STRING: //1 str = cell.getStringCellValue(); break; case Cell.CELL_TYPE_FORMULA: //2 ? str = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_BLANK: //3 str = ""; break; case Cell.CELL_TYPE_BOOLEAN: //4 str = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: //5 str = ""; break; default: str = null; break; } } return str; }
From source file:com.ncc.excel.ExcelUtil2.java
License:Apache License
public static String getCellValue(Cell cell) { // private String getCellValue(Cell cell){ String str = null;//www . ja va2 s. com if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: //0 //?? 1. 2. if (HSSFDateUtil.isCellDateFormatted(cell)) { //?? str = DateUtil.formatDateForExcelDate(cell.getDateCellValue()); } else { double dValue = cell.getNumericCellValue(); //E if (String.valueOf(dValue).contains("E")) { str = new DecimalFormat("#").format(dValue); } else { str = String.valueOf(dValue); } } break; case Cell.CELL_TYPE_STRING: //1 str = cell.getStringCellValue(); break; case Cell.CELL_TYPE_FORMULA: //2 ? str = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_BLANK: //3 str = ""; break; case Cell.CELL_TYPE_BOOLEAN: //4 str = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: //5 str = ""; break; default: str = null; break; } } return str; }
From source file:com.ncc.excel.test.ExcelUtil.java
License:Apache License
/*** * ?? // ww w. ja v a 2 s . c o m * * @Title: getCellValue * @Date : 2014-9-11 ?10:52:07 * @param cell * @return */ private String getCellValue(Cell cell) { Object result = ""; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: result = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_FORMULA: result = cell.getCellFormula(); break; case Cell.CELL_TYPE_ERROR: result = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_BLANK: break; default: break; } } return result.toString(); }
From source file:com.nikoo28.excel.parser.ExcelParser.java
License:Apache License
public String parseExcelData(InputStream is) { try {// w ww .j av a2 s . com HSSFWorkbook workbook = new HSSFWorkbook(is); // Taking first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0); // Iterate through each rows from first sheet Iterator<Row> rowIterator = sheet.iterator(); currentString = new StringBuilder(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); // 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: bytesRead++; currentString.append(cell.getBooleanCellValue() + "\t"); break; case Cell.CELL_TYPE_NUMERIC: bytesRead++; currentString.append(cell.getNumericCellValue() + "\t"); break; case Cell.CELL_TYPE_STRING: bytesRead++; currentString.append(cell.getStringCellValue() + "\t"); break; } } currentString.append("\n"); } is.close(); } catch (IOException e) { LOG.error("IO Exception : File not found " + e); } return currentString.toString(); }
From source file:com.ocs.dynamo.importer.impl.BaseXlsImporter.java
License:Apache License
/** * Extracts a boolean value from a cell/*from w w w. j av a 2s .com*/ * * @param cell * @return */ protected Boolean getBooleanValue(Cell cell) { if (cell != null && (Cell.CELL_TYPE_BOOLEAN == cell.getCellType())) { return cell.getBooleanCellValue(); } else if (cell != null && Cell.CELL_TYPE_STRING == cell.getCellType()) { return Boolean.valueOf(cell.getStringCellValue()); } return Boolean.FALSE; }
From source file:com.opendoorlogistics.core.tables.io.PoiIO.java
License:Open Source License
private static String getTextValue(Cell cell, int treatAsCellType) { if (cell == null) { return null; }//ww w. j a va 2s . c om switch (treatAsCellType) { case Cell.CELL_TYPE_STRING: return cell.getRichStringCellValue().getString(); case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (date != null) { Calendar cal = Calendar.getInstance(); cal.setTime(date); @SuppressWarnings("deprecation") int year = date.getYear(); if (year == -1) { // equivalent to 1899 which is the first data .. assume its a time String s = ODL_TIME_FORMATTER.format(date); return s; } // System.out.println(year); } return cell.getDateCellValue().toString(); } else { String ret = Double.toString(cell.getNumericCellValue()); if (ret.endsWith(".0")) { ret = ret.substring(0, ret.length() - 2); } return ret; } case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue() ? "T" : "F"; case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_BLANK: return null; } return ""; }
From source file:com.opengamma.financial.security.equity.GICSCodeDescription.java
License:Open Source License
/** * Get the value of the Apache POI Cell as a String. If the Cell type is numeric (always a double with POI), * the value is converted to an integer. The GCIS file does not contain any floating point values so (at this time) * this is a valid operation/*from www . ja va2 s .com*/ * * @param cell Apache POI Cell * @return String value */ static String getGICSCellValue(Cell cell) { if (cell == null) { return ""; } switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: return Integer.valueOf((int) cell.getNumericCellValue()).toString(); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case Cell.CELL_TYPE_BLANK: return ""; default: return "null"; } }
From source file:com.opengamma.integration.copier.sheet.reader.SimpleXlsSheetReader.java
License:Open Source License
private static String getCellAsString(Cell cell) { if (cell == null) { return ""; }/*from w w w. j a v a 2 s . c o m*/ switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: //return Double.toString(cell.getNumericCellValue()); return (new DecimalFormat("#.##")).format(cell.getNumericCellValue()); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); case Cell.CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case Cell.CELL_TYPE_BLANK: return ""; default: return null; } }
From source file:com.openitech.db.model.ExcelDataSource.java
License:Apache License
@Override public boolean loadData(boolean reload, int oldRow) { boolean result = false; if (isDataLoaded && !reload) { return false; }//from w w w . j a v a 2s. c o m if (sourceFile != null) { try { Workbook workBook = WorkbookFactory.create(new FileInputStream(sourceFile)); // HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(sourceFile)); Sheet sheet = workBook.getSheetAt(0); DataFormatter dataFormatter = new DataFormatter(Locale.GERMANY); FormulaEvaluator formulaEvaluator = workBook.getCreationHelper().createFormulaEvaluator(); int lastRowNum = sheet.getLastRowNum(); boolean isFirstLineHeader = true; //count = sheet. - (isFirstLineHeader ? 1 : 0); int tempCount = 0; for (int j = 0; j <= lastRowNum; j++) { //zane se z 0 Row row = row = sheet.getRow(j); if (row == null) { continue; } // display row number in the console. System.out.println("Row No.: " + row.getRowNum()); if (isFirstLineHeader && row.getRowNum() == 0) { populateHeaders(row); continue; } tempCount++; Map<String, DataColumn> values; if (rowValues.containsKey(row.getRowNum())) { values = rowValues.get(row.getRowNum()); } else { values = new HashMap<String, DataColumn>(); rowValues.put(row.getRowNum(), values); } // once get a row its time to iterate through cells. int lastCellNum = row.getLastCellNum(); for (int i = 0; i <= lastCellNum; i++) { DataColumn dataColumn = new DataColumn(); Cell cell = row.getCell(i); if (cell == null) { continue; } System.out.println("Cell No.: " + cell.getColumnIndex()); System.out.println("Value: " + dataFormatter.formatCellValue(cell)); if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { dataColumn = new DataColumn(dataFormatter.formatCellValue(cell, formulaEvaluator)); } else { dataColumn = new DataColumn(dataFormatter.formatCellValue(cell)); } switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: { // cell type numeric. System.out.println("Numeric value: " + cell.getNumericCellValue()); dataColumn = new DataColumn(dataFormatter.formatCellValue(cell)); break; } case Cell.CELL_TYPE_STRING: // cell type string. System.out.println("String value: " + cell.getStringCellValue()); dataColumn = new DataColumn(dataFormatter.formatCellValue(cell)); break; case Cell.CELL_TYPE_BOOLEAN: // cell type string. System.out.println("String value: " + cell.getBooleanCellValue()); dataColumn.setValue(cell.getBooleanCellValue(), Boolean.class); break; case Cell.CELL_TYPE_FORMULA: // cell type string. System.out.println( "Formula value: " + dataFormatter.formatCellValue(cell, formulaEvaluator)); dataColumn = new DataColumn(dataFormatter.formatCellValue(cell, formulaEvaluator)); break; default: dataColumn.setValue(cell.getStringCellValue(), String.class); break; } values.put(getColumnName(cell.getColumnIndex()).toUpperCase(), dataColumn); } } count = tempCount; isDataLoaded = true; //se postavim na staro vrstico ali 1 if (oldRow > 0) { absolute(oldRow); } else { first(); } result = true; } catch (Exception ex) { Logger.getLogger(ExcelDataSource.class.getName()).log(Level.SEVERE, null, ex); result = false; } } return result; }
From source file:com.ostrichemulators.semtool.poi.main.POIReader.java
License:Open Source License
/** * Always return a non-null string (will be "" for null cells). * * @param cell// www . ja v a2s. c om * @return */ private static String getString(Cell cell) { if (null == cell) { return ""; } switch (cell.getCellType()) { case NUMERIC: return Double.toString(cell.getNumericCellValue()); case BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case FORMULA: return cell.getCellFormula(); default: return cell.getStringCellValue(); } }