List of usage examples for org.apache.poi.ss.usermodel Cell getStringCellValue
String getStringCellValue();
For numeric cells we throw an exception.
From source file:com.nfa.drs.data.StudentWindTunnelFormatXls.java
private List<String> readXlsLines(Path file) { List<String> lines = new ArrayList<>(); try {/* w w w . j a va 2 s .com*/ FileInputStream stream = new FileInputStream(file.toFile()); HSSFWorkbook book = new HSSFWorkbook(stream); HSSFSheet sheet = book.getSheetAt(0); for (Row row : sheet) { int rowIndex = row.getRowNum(); while (rowIndex > lines.size() - 1) { lines.add(""); } StringBuilder line = new StringBuilder(); for (Cell cell : row) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { line.append(cell.getStringCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { line.append(cell.getNumericCellValue()); } line.append(","); } lines.add(line.toString()); } } catch (IOException ex) { } return lines; }
From source file:com.nikoo28.excel.parser.ExcelParser.java
License:Apache License
public String parseExcelData(InputStream is) { try {/*from w ww . j a v a2s .c o m*/ 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 www . j a va 2 s. c om*/ * * @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.ocs.dynamo.importer.impl.BaseXlsImporter.java
License:Apache License
/** * Retrieves the numeric value of a cell * //from www .j a v a2 s .co m * @param cell * @return */ protected Double getNumericValue(Cell cell) { if (cell != null && (Cell.CELL_TYPE_NUMERIC == cell.getCellType() || Cell.CELL_TYPE_BLANK == cell.getCellType())) { try { return cell.getNumericCellValue(); } catch (NullPointerException nex) { // cannot return null from getNumericCellValue - so if the cell // is empty we // have to handle it in this ugly way return null; } catch (Exception ex) { throw new OCSImportException("Found an invalid numeric value: " + cell.getStringCellValue(), ex); } } else if (cell != null && Cell.CELL_TYPE_STRING == cell.getCellType()) { // in case the value is not numeric, simply output a warning. If the // field is required, this will trigger // an error at a later stage if (!StringUtils.isEmpty(cell.getStringCellValue().trim())) { throw new OCSImportException("Found an invalid numeric value: " + cell.getStringCellValue()); } } return null; }
From source file:com.ocs.dynamo.importer.impl.BaseXlsImporter.java
License:Apache License
/** * Retrieves the value of a cell as a string. Returns <code>null</code> if the cell does not * contain a string// w w w . ja v a2 s.c o m * * @param cell * @return */ protected String getStringValue(Cell cell) { if (cell != null && (Cell.CELL_TYPE_STRING == cell.getCellType() || cell.getCellType() == Cell.CELL_TYPE_BLANK)) { String value = cell.getStringCellValue(); return value == null ? null : value.trim(); } else if (cell != null && Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { // if a number is entered in a field that is supposed to contain a // string, Excel goes insane. We have to compensate for this Double d = cell.getNumericCellValue(); return d == null ? null : Long.toString(d.longValue()); } return null; }
From source file:com.ocs.dynamo.importer.impl.BaseXlsImporter.java
License:Apache License
/** * Check if the specified row is completely empty * //from w w w. j av a 2 s. c om * @param row * @return */ public boolean isRowEmpty(Row row) { if (row == null || row.getFirstCellNum() < 0) { return true; } Iterator<Cell> iterator = row.iterator(); while (iterator.hasNext()) { Cell next = iterator.next(); String value = next.getStringCellValue(); if (!StringUtils.isEmpty(value)) { return false; } } return true; }
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 w w w . j ava2 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 ""; }// ww 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 ww w . j a va2 s . c om*/ 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.openitech.db.model.ExcelDataSource.java
License:Apache License
private void populateHeaders(Row row) { columnCount = 0;//w ww.ja va 2s .co m int lastCellNum = row.getLastCellNum(); for (int i = 0; i <= lastCellNum; i++) { Cell cell = row.getCell(i); if (cell == null) { continue; } System.out.println("String value: " + cell.getStringCellValue()); String header = cell.getStringCellValue(); columnMapping.put(header, cell.getColumnIndex()); columnMappingIndex.put(cell.getColumnIndex(), header); columnCount++; } }