List of usage examples for org.apache.poi.ss.usermodel Cell getBooleanCellValue
boolean getBooleanCellValue();
From source file:org.ramadda.plugins.media.TabularOutputHandler.java
License:Open Source License
/** * _more_//from www . ja v a 2s . c o m * * @param request _more_ * @param entry _more_ * @param suffix _more_ * @param inputStream _more_ * @param visitInfo _more_ * @param visitor _more_ * * @throws Exception _more_ */ private void visitXls(Request request, Entry entry, String suffix, InputStream inputStream, Visitor visitInfo, TabularVisitor visitor) throws Exception { // System.err.println("visitXls: making workbook"); Workbook wb = makeWorkbook(suffix, inputStream); // System.err.println("visitXls:" + visitInfo.getSkip() + " max rows:" + visitInfo.getMaxRows()+ " #sheets:" + wb.getNumberOfSheets()); int maxRows = visitInfo.getMaxRows(); for (int sheetIdx = 0; sheetIdx < wb.getNumberOfSheets(); sheetIdx++) { if (!visitInfo.okToShowSheet(sheetIdx)) { continue; } Sheet sheet = wb.getSheetAt(sheetIdx); // System.err.println("\tsheet:" + sheet.getSheetName() + " #rows:" + sheet.getLastRowNum()); List<List<Object>> rows = new ArrayList<List<Object>>(); int sheetSkip = visitInfo.getSkip(); for (int rowIdx = sheet.getFirstRowNum(); (rows.size() < maxRows) && (rowIdx <= sheet.getLastRowNum()); rowIdx++) { if (sheetSkip-- > 0) { continue; } Row row = sheet.getRow(rowIdx); if (row == null) { continue; } List<Object> cols = new ArrayList<Object>(); short firstCol = row.getFirstCellNum(); for (short col = firstCol; (col < MAX_COLS) && (col < row.getLastCellNum()); col++) { Cell cell = row.getCell(col); if (cell == null) { break; } Object value = null; int type = cell.getCellType(); if (type == cell.CELL_TYPE_NUMERIC) { value = new Double(cell.getNumericCellValue()); } else if (type == cell.CELL_TYPE_BOOLEAN) { value = new Boolean(cell.getBooleanCellValue()); } else if (type == cell.CELL_TYPE_ERROR) { value = "" + cell.getErrorCellValue(); } else if (type == cell.CELL_TYPE_BLANK) { value = ""; } else if (type == cell.CELL_TYPE_FORMULA) { value = cell.getCellFormula(); } else { value = cell.getStringCellValue(); } cols.add(value); } /** * ** TODO * org.ramadda.util.text.Row row = new Row(cols); * * if ( !visitInfo.rowOk(row)) { * if (rows.size() == 0) { * //todo: check for the header line * } else { * continue; * } * } */ rows.add(cols); } if (!visitor.visit(visitInfo, sheet.getSheetName(), rows)) { break; } } }
From source file:org.rdcit.tools.SpreadsheetReader.java
public String readCell(int sheetNum, int rowNum, int cellNum) { String cellValue = ""; Sheet sheet = workbook.getSheetAt(sheetNum); Row row = sheet.getRow(rowNum);/* ww w .ja v a 2s. c om*/ try { Cell cell = row.getCell(cellNum); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: cellValue = String.valueOf(isInteger(cell.getNumericCellValue())); break; case Cell.CELL_TYPE_BLANK: cellValue = ""; break; } return cellValue; } catch (Exception ex) { return "null"; } }
From source file:org.rdcit.tools.SpreadsheetReader.java
public String getCellValueAsString(Cell cell) { String cellContent = ""; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cellContent = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: cellContent = String.valueOf(isInteger(cell.getNumericCellValue())); break; case Cell.CELL_TYPE_STRING: cellContent = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BLANK: case Cell.CELL_TYPE_ERROR: case Cell.CELL_TYPE_FORMULA: break; }/*from www . ja v a 2 s.c o m*/ } return cellContent; }
From source file:org.seasar.fisshplate.util.FPPoiUtil.java
License:Apache License
/** * ?????????/*from w w w . java2 s . c om*/ * @param hssfCell * @return ? */ public static Object getCellValueAsObject(Cell hssfCell) { if (hssfCell == null) { return null; } int cellType = hssfCell.getCellType(); Object ret = null; switch (cellType) { case HSSFCell.CELL_TYPE_NUMERIC: ret = getValueFromNumericCell(hssfCell); break; case HSSFCell.CELL_TYPE_STRING: ret = hssfCell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_BOOLEAN: ret = Boolean.valueOf(hssfCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: ret = hssfCell.getCellFormula(); break; case HSSFCell.CELL_TYPE_ERROR: ret = new Byte(hssfCell.getErrorCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: break; default: return null; } return ret; }
From source file:org.shareok.data.documentProcessor.ExcelHandler.java
/** * Reads out the data in an excel file and stores data in a hashmap<p> * The cell data has the ending of "--type" to label the data type * // w ww . j ava 2s . c o m * @throws Exception */ @Override public void readData() { String name = fileName; Sheet sheet = null; try { if (null == name || "".equals(name)) { throw new FileNameException("File name is not specified!"); } FileInputStream file = new FileInputStream(new File(name)); String extension = DocumentProcessorUtil.getFileExtension(name); String[] excelTypes = router.loadOfficeFileType("excel"); if (null == excelTypes || excelTypes.length == 0) { throw new FileTypeException("The file types are empty!"); } HashMap<String, String> typeMap = new HashMap<>(); for (String s : excelTypes) { typeMap.put(s, s); } if (typeMap.containsKey(extension)) { if (extension.equals("xlsx")) { } } sheet = getWorkbookSheet(extension, file); int maxNumOfCells = sheet.getRow(0).getLastCellNum(); Iterator<Row> rowIterator = sheet.iterator(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); int rowCount = 0; //int colCount = 0; while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); //while(cellIterator.hasNext()) { for (int colCount = 0; colCount < maxNumOfCells; colCount++) { //Cell cell = cellIterator.next(); Cell cell = row.getCell(colCount); if (null == cell) { cell = row.createCell(colCount); } String key = Integer.toString(rowCount) + "-" + Integer.toString(colCount); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: data.put(key, Boolean.toString(cell.getBooleanCellValue()) + "---bool"); break; case Cell.CELL_TYPE_NUMERIC: if (isCellDateFormatted(cell)) { data.put(key, df.format(cell.getDateCellValue()) + "---dat"); } else { data.put(key, Double.toString(cell.getNumericCellValue()) + "---num"); } break; case Cell.CELL_TYPE_STRING: data.put(key, cell.getStringCellValue() + "---str"); break; case Cell.CELL_TYPE_BLANK: data.put(key, ""); break; case Cell.CELL_TYPE_ERROR: data.put(key, "ERROR_VALUE"); break; case Cell.CELL_TYPE_FORMULA: FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper() .createFormulaEvaluator(); //handleCell(cell.getCachedFormulaResultType(), cell, evaluator); data.put(key, String.valueOf(cell.getCachedFormulaResultType())); break; default: data.put(key, cell.getRichStringCellValue() + "---def"); break; } // colCount++; } rowCount++; //colCount = 0; } file.close(); } catch (FileNotFoundException ex) { Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.shareok.data.msofficedata.ExcelHandler.java
/** * * @throws Exception//from ww w . j av a 2s.c o m */ @Override public void readData() { String name = fileName; Sheet sheet = null; try { if (null == name || "".equals(name)) { throw new Exception("File name is not specified!"); } FileInputStream file = new FileInputStream(new File(name)); String extension = FileUtil.getFileExtension(name); String[] excelTypes = router.loadOfficeFileType("excel"); if (null == excelTypes || excelTypes.length == 0) { throw new Exception("The file types are empty!"); } HashMap<String, String> typeMap = new HashMap<>(); for (String s : excelTypes) { typeMap.put(s, s); } if (typeMap.containsKey(extension)) { if (extension.equals("xlsx")) { } } sheet = getWorkbookSheet(extension, file); Iterator<Row> rowIterator = sheet.iterator(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); int rowCount = 0; int colCount = 0; while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); String key = Integer.toString(rowCount) + "-" + Integer.toString(colCount); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: data.put(key, Boolean.toString(cell.getBooleanCellValue()) + "---bool"); break; case Cell.CELL_TYPE_NUMERIC: if (isCellDateFormatted(cell)) { data.put(key, df.format(cell.getDateCellValue()) + "---dat"); } else { data.put(key, Double.toString(cell.getNumericCellValue()) + "---num"); } break; case Cell.CELL_TYPE_STRING: data.put(key, cell.getStringCellValue() + "---str"); break; default: data.put(key, cell.getRichStringCellValue() + "---def"); break; } colCount++; } rowCount++; colCount = 0; } file.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException ex) { Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(ExcelHandler.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.smart.migrate.dao.impl.ExcelImportDao.java
@Override public List<String> findAllSourcePrimaryKeys(TableSetting tableSetting) { sourceData.clear();//from w w w . ja va 2s . co m List<String> pks = new ArrayList<String>(); Sheet sheet = sourceWorkbook.getSheet(tableSetting.getSourceTable()); if (sheet == null) { throw new RuntimeException("can not get sheet from " + tableSetting.getSourceTable()); } Row headRow = sheet.getRow(sheet.getFirstRowNum()); if (headRow != null) { int idColumn = ExcelUtils.cellIndexInRow("id", headRow); if (idColumn == -1) { throw new RuntimeException("sheet must have id column!"); } //initialize header map (key: column index,value: fieldname) Map<Integer, String> header = new HashMap<Integer, String>(); for (Cell cell : headRow) { header.put(cell.getColumnIndex(), cell.getStringCellValue()); } for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); String pk; if (row.getCell(idColumn).getCellType() == Cell.CELL_TYPE_NUMERIC) { Double did = row.getCell(idColumn).getNumericCellValue(); pk = String.valueOf(did.intValue()); } else { pk = row.getCell(idColumn).getStringCellValue(); } pks.add(pk); Map<String, Object> data = new HashMap<String, Object>(); for (Cell cell : row) { String fieldname = header.get(cell.getColumnIndex()); if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { Double dvalue = cell.getNumericCellValue(); String s = String.valueOf(dvalue); if (s.endsWith(".0")) { data.put(fieldname, dvalue.intValue()); } else { data.put(fieldname, dvalue); } } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { data.put(fieldname, cell.getBooleanCellValue()); } else { data.put(fieldname, cell.getStringCellValue()); } } sourceData.put(pk, data); } } return pks; }
From source file:org.spdx.spdxspreadsheet.PackageInfoSheetV2d1.java
License:Apache License
@SuppressWarnings("deprecation") private SpdxPackage getPackage(int rowNum, SpdxDocumentContainer container) throws SpreadsheetException { Row row = sheet.getRow(rowNum);//w w w .j av a 2 s .co m if (row == null) { return null; } Cell nameCell = row.getCell(NAME_COL); if (nameCell == null || nameCell.getStringCellValue().isEmpty()) { return null; } String error = validateRow(row); if (error != null && !error.isEmpty()) { throw (new SpreadsheetException(error)); } String declaredName = nameCell.getStringCellValue(); String id = row.getCell(ID_COL).getStringCellValue(); Cell machineNameCell = row.getCell(MACHINE_NAME_COL); String machineName = null; if (machineNameCell != null) { machineName = row.getCell(MACHINE_NAME_COL).getStringCellValue(); } Cell checksumsCell = row.getCell(PACKAGE_CHECKSUMS_COL); Checksum[] checksums = new Checksum[0]; if (checksumsCell != null) { try { checksums = CompareHelper.strToChecksums(checksumsCell.getStringCellValue()); } catch (InvalidSPDXAnalysisException e) { throw (new SpreadsheetException("Error converting file checksums: " + e.getMessage())); } } String sourceInfo; Cell sourceInfocol = row.getCell(SOURCE_INFO_COL); if (sourceInfocol != null) { sourceInfo = sourceInfocol.getStringCellValue(); } else { sourceInfo = ""; } AnyLicenseInfo declaredLicenses = LicenseInfoFactory .parseSPDXLicenseString(row.getCell(DECLARED_LICENSE_COL).getStringCellValue(), container); AnyLicenseInfo concludedLicense; Cell concludedLicensesCell = row.getCell(CONCLUDED_LICENSE_COL); if (concludedLicensesCell != null && !concludedLicensesCell.getStringCellValue().isEmpty()) { concludedLicense = LicenseInfoFactory.parseSPDXLicenseString(concludedLicensesCell.getStringCellValue(), container); } else { concludedLicense = new SpdxNoneLicense(); } Cell licenseInfoInFilesCell = row.getCell(LICENSE_INFO_IN_FILES_COL); String[] licenseStrings; if (licenseInfoInFilesCell != null) { licenseStrings = row.getCell(LICENSE_INFO_IN_FILES_COL).getStringCellValue().split(","); } else { licenseStrings = new String[0]; } AnyLicenseInfo[] licenseInfosFromFiles = new AnyLicenseInfo[licenseStrings.length]; for (int i = 0; i < licenseStrings.length; i++) { licenseInfosFromFiles[i] = LicenseInfoFactory.parseSPDXLicenseString(licenseStrings[i].trim(), container); } Cell licenseCommentCell = row.getCell(LICENSE_COMMENT_COL); String licenseComment; if (licenseCommentCell != null && !licenseCommentCell.getStringCellValue().isEmpty()) { licenseComment = licenseCommentCell.getStringCellValue(); } else { licenseComment = ""; } String declaredCopyright = row.getCell(DECLARED_COPYRIGHT_COL).getStringCellValue(); Cell shortDescCell = row.getCell(SHORT_DESC_COL); String shortDesc; if (shortDescCell != null && !shortDescCell.getStringCellValue().isEmpty()) { shortDesc = shortDescCell.getStringCellValue(); } else { shortDesc = ""; } Cell descCell = row.getCell(FULL_DESC_COL); String description; if (descCell != null && !descCell.getStringCellValue().isEmpty()) { description = descCell.getStringCellValue(); } else { description = ""; } String url; Cell downloadUrlCell = row.getCell(DOWNLOAD_URL_COL); if (downloadUrlCell != null) { url = downloadUrlCell.getStringCellValue(); } else { url = ""; } Cell packageVerificationCell = row.getCell(FILE_VERIFICATION_VALUE_COL); String packageVerificationValue; if (packageVerificationCell != null) { packageVerificationValue = packageVerificationCell.getStringCellValue(); } else { packageVerificationValue = ""; } String[] excludedFiles; Cell excludedFilesCell = row.getCell(VERIFICATION_EXCLUDED_FILES_COL); String excludedFilesStr = null; if (excludedFilesCell != null) { excludedFilesStr = excludedFilesCell.getStringCellValue(); } if (excludedFilesStr != null && !excludedFilesStr.isEmpty()) { excludedFiles = excludedFilesStr.split(","); for (int i = 0; i < excludedFiles.length; i++) { excludedFiles[i] = excludedFiles[i].trim(); } } else { excludedFiles = new String[0]; } Cell versionInfoCell = row.getCell(VERSION_COL); String versionInfo; if (versionInfoCell != null) { if (versionInfoCell.getCellTypeEnum() == CellType.STRING && !versionInfoCell.getStringCellValue().isEmpty()) { versionInfo = versionInfoCell.getStringCellValue(); } else if (versionInfoCell.getCellTypeEnum() == CellType.NUMERIC) { versionInfo = Double.toString(versionInfoCell.getNumericCellValue()); } else { versionInfo = ""; } } else { versionInfo = ""; } String supplier; Cell supplierCell = row.getCell(SUPPLIER_COL); if (supplierCell != null && !supplierCell.getStringCellValue().isEmpty()) { supplier = supplierCell.getStringCellValue(); } else { supplier = ""; } String originator; Cell originatorCell = row.getCell(ORIGINATOR_COL); if (originatorCell != null && !originatorCell.getStringCellValue().isEmpty()) { originator = originatorCell.getStringCellValue(); } else { originator = ""; } String homePage; Cell homePageCell = row.getCell(HOME_PAGE_COL); if (homePageCell != null && !homePageCell.getStringCellValue().isEmpty()) { homePage = homePageCell.getStringCellValue(); } else { homePage = ""; } boolean filesAnalyzed = true; Cell filesAnalyzedCell = row.getCell(FILES_ANALYZED_COL); if (filesAnalyzedCell != null) { if (filesAnalyzedCell.getCellTypeEnum() == CellType.BOOLEAN) { filesAnalyzed = filesAnalyzedCell.getBooleanCellValue(); } else { String filesAnalyzedStr = filesAnalyzedCell.getStringCellValue(); if (filesAnalyzedStr != null) { if (filesAnalyzedStr.toLowerCase().trim().equals("false")) { filesAnalyzed = false; } } } } SpdxPackageVerificationCode verificationCode = new SpdxPackageVerificationCode(packageVerificationValue, excludedFiles); SpdxPackage retval = new SpdxPackage(declaredName, "", new Annotation[0], new Relationship[0], concludedLicense, licenseInfosFromFiles, declaredCopyright, licenseComment, declaredLicenses, checksums, description, url, new SpdxFile[0], homePage, originator, machineName, verificationCode, sourceInfo, shortDesc, supplier, versionInfo, filesAnalyzed, new ExternalRef[0]); try { retval.setId(id); } catch (InvalidSPDXAnalysisException e) { throw (new SpreadsheetException("Unable to set package ID: " + e.getMessage())); } return retval; }
From source file:org.specrunner.source.excel.SourceFactoryExcel.java
License:Open Source License
/** * Extract the value of a given cell./*from w w w. j a va 2 s . c o m*/ * * @param cell * The cell. * @return The corresponding object. */ protected Object extractVal(Cell cell) { if (cell == null) { return ""; } Object val = null; switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: val = null; break; case Cell.CELL_TYPE_BOOLEAN: val = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_ERROR: val = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_FORMULA: val = cell.getCellFormula(); break; case Cell.CELL_TYPE_NUMERIC: double d = cell.getNumericCellValue(); String tmp = String.valueOf(d); if (tmp.endsWith(".0")) { val = tmp.substring(0, tmp.lastIndexOf('.')); } else { val = d; } break; case Cell.CELL_TYPE_STRING: val = cell.getStringCellValue(); break; default: } return val; }
From source file:org.springframework.batch.item.excel.poi.ArrayPoiSheet.java
License:Apache License
/** * {@inheritDoc}// w w w .ja v a 2 s. c o m */ @Override public Object[] getRow(final int rowNumber) { final Row row = this.delegate.getRow(rowNumber); if (row == null) { return null; } final List<Object> cells = new LinkedList<Object>(); for (int i = 0; i < getNumberOfColumns(); i++) { Cell cell = row.getCell(i); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { cells.add(cell.getDateCellValue()); } else { cells.add(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: cells.add(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_STRING: case Cell.CELL_TYPE_BLANK: cells.add(cell.getStringCellValue()); break; case Cell.CELL_TYPE_FORMULA: cells.add(getFormulaEvaluator().evaluate(cell).formatAsString()); break; default: throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType()); } } return cells.toArray(new Object[cells.size()]); }