List of usage examples for org.apache.poi.ss.usermodel Cell getDateCellValue
Date getDateCellValue();
From source file:org.eclipse.emfforms.internal.spreadsheet.core.EMFFormsSpreadsheetExporterImpl_ITest.java
License:Open Source License
@Test public void testRender() throws DatatypeConfigurationException { final EMFFormsSpreadsheetExporter viewRenderer = new EMFFormsSpreadsheetExporterImpl(new ViewProvider() { @Override/*from www .j av a2 s . c om*/ public VView getViewModel(EObject viewEobject, VViewModelProperties properties) { return getView(); } }); final User user = getDomainModel(); final Workbook wb = viewRenderer.render(Collections.singleton(user), null, null); final Sheet sheet = wb.getSheetAt(0); final Row row = sheet.getRow(3); assertEquals(11, row.getLastCellNum()); for (int i = 0; i < 10; i++) { final Cell cell = row.getCell(i + 1); switch (i) { case 0: assertEquals(user.getFirstName(), cell.getStringCellValue()); break; case 1: assertEquals(user.getLastName(), cell.getStringCellValue()); break; case 2: assertEquals(user.getGender().toString(), cell.getStringCellValue()); break; case 3: assertEquals(user.isActive(), cell.getBooleanCellValue()); break; case 4: assertEquals(user.getTimeOfRegistration(), cell.getDateCellValue()); break; case 5: assertEquals(user.getWeight(), cell.getNumericCellValue(), 0); break; case 6: assertEquals(user.getHeigth(), Double.valueOf(cell.getNumericCellValue()).intValue()); break; case 7: assertEquals(user.getNationality().toString(), cell.getStringCellValue()); break; case 8: assertEquals(user.getDateOfBirth().toGregorianCalendar().getTime(), DateUtil.getJavaCalendarUTC(cell.getNumericCellValue(), false).getTime()); break; case 9: assertEquals(user.getEmail(), cell.getStringCellValue()); break; default: fail(); } } }
From source file:org.eclipse.lyo.samples.excel.adapter.dao.internal.ExcelDaoImpl.java
License:Open Source License
private String getCellValue(Cell cell) { if (cell != null) { String value = null;/*www . j a v a2s . c o m*/ int type = cell.getCellType(); if (type == Cell.CELL_TYPE_STRING) { value = cell.getStringCellValue(); } else if (type == Cell.CELL_TYPE_NUMERIC) { if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); value = FastDateFormat.getInstance(DEFAULT_OUTPUT_DATE_FORMAT).format(date); } else { double d = cell.getNumericCellValue(); if (d == Math.floor(d)) { // need to consider when d is negative value = "" + (int) d; } else { value = "" + cell.getNumericCellValue(); } } } else if (type == Cell.CELL_TYPE_FORMULA) { // get calculated value if the cell type is formula Workbook wb = cell.getSheet().getWorkbook(); CreationHelper crateHelper = wb.getCreationHelper(); FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator(); // get recursively if the value is still formula value = getCellValue(evaluator.evaluateInCell(cell)); } return value; } return null; }
From source file:org.eclipse.titanium.markers.export.ExportedProblemMerger.java
License:Open Source License
/** * Collect the dates contained in a sheet. * //w w w .j a v a2 s. c om * @param file * The file that the date belongs to * @param sheet * The sheet being processed */ private void collectDates(final File file, final HSSFSheet sheet) { final int cols = sheet.getRow(1).getLastCellNum(); for (int col = 1; col < cols; ++col) { final Cell cell = sheet.getRow(1).getCell(col); // if not a deleted column if (cell.getCellType() != HSSFCell.CELL_TYPE_BLANK && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { final Date date = cell.getDateCellValue(); if (!dates.contains(date)) { dates.add(date); datefile.put(date, file); datecol.put(date, col); } } } } }
From source file:org.generationcp.middleware.util.PoiUtil.java
License:Open Source License
private static Object getNumericValue(final Cell cell) { if (DateUtil.isCellDateFormatted(cell)) { final Date date = cell.getDateCellValue(); return PoiUtil.EXCEL_DATE_FORMATTER.format(date); }// w w w .ja v a 2 s . com final double doubleVal = cell.getNumericCellValue(); if (doubleVal % 1 == 0) { return (int) doubleVal; } else { return doubleVal; } }
From source file:org.geoserver.wfs.response.ExcelOutputFormatTest.java
License:Open Source License
private void testExcelOutputFormat(Workbook wb) throws IOException { Sheet sheet = wb.getSheet("PrimitiveGeoFeature"); assertNotNull(sheet);/*from w w w.j ava2 s . c o m*/ FeatureSource fs = getFeatureSource(MockData.PRIMITIVEGEOFEATURE); // check the number of rows in the output final int feautureRows = fs.getCount(Query.ALL); assertEquals(feautureRows + 1, sheet.getPhysicalNumberOfRows()); // check the header is what we expect final SimpleFeatureType schema = (SimpleFeatureType) fs.getSchema(); final Row header = sheet.getRow(0); assertEquals("FID", header.getCell(0).getRichStringCellValue().toString()); for (int i = 0; i < schema.getAttributeCount(); i++) { assertEquals(schema.getDescriptor(i).getLocalName(), header.getCell(i + 1).getRichStringCellValue().toString()); } // check some selected values to see if the content and data type is the one // we expect FeatureIterator fi = fs.getFeatures().features(); SimpleFeature sf = (SimpleFeature) fi.next(); fi.close(); // ... a string cell Cell cell = sheet.getRow(1).getCell(1); assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(sf.getAttribute(0), cell.getRichStringCellValue().toString()); // ... a geom cell cell = sheet.getRow(1).getCell(4); assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); assertEquals(sf.getAttribute(3).toString(), cell.getRichStringCellValue().toString()); // ... a number cell cell = sheet.getRow(1).getCell(6); assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(((Number) sf.getAttribute(5)).doubleValue(), cell.getNumericCellValue()); // ... a date cell (they are mapped as numeric in xms?) cell = sheet.getRow(1).getCell(10); assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); assertEquals(sf.getAttribute(9), cell.getDateCellValue()); // ... a boolean cell (they are mapped as numeric in xms?) cell = sheet.getRow(1).getCell(12); assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); assertEquals(sf.getAttribute(11), cell.getBooleanCellValue()); // ... an empty cell (original value is null -> no cell) cell = sheet.getRow(1).getCell(3); assertNull(cell); }
From source file:org.interpss.util.reader.Excel.java
License:Open Source License
/** * convert the date cell to a string, format "8/18/2011" * // w w w . j a v a2 s .c o m * @param cell * @return */ public static String date2Str(Cell cell) { return DateTimeUtil.formatDate(cell.getDateCellValue()); }
From source file:org.is.jxlpoi.JXLPOIWorkbook.java
License:Apache License
public String getCellContentAsString(Cell c) { if (c == null) return null; switch (c.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: if (c.getBooleanCellValue()) return "Y"; else/* w w w.j ava 2 s .c o m*/ return "N"; case Cell.CELL_TYPE_NUMERIC: String result = ""; int datatype = c.getCellStyle().getDataFormat(); String formatString = c.getCellStyle().getDataFormatString(); if (datatype == 174 && "yyyy/mm/dd".equals(formatString)) { java.util.Date date = c.getDateCellValue(); return fmter.format(date); } else if (datatype == 49 || datatype == 0) { int d = (int) c.getNumericCellValue(); result = Integer.toString(d); } else { result = Double.toString(c.getNumericCellValue()); } //return Double.toString(c.getNumericCellValue()); //System.out.println(" number = "+c.getNumericCellValue()+" *** value ="+twoPlaces.format(c.getNumericCellValue())+""); //return twoPlaces.format(c.getNumericCellValue())+""; return result; case Cell.CELL_TYPE_STRING: return c.getStringCellValue(); case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_ERROR: return "#ERROR" + c.getErrorCellValue(); case Cell.CELL_TYPE_FORMULA: String formulaCellValue; if (formulaEvaluator == null) { formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator(); //formulaEvaluator.setIgnoreFormulaException(); //System.out.println(formulaEvaluator); } //formulaEvaluator.evaluateFormulaCell(c); //formulaEvaluator.evaluateInCell(c); CellValue cv = formulaEvaluator.evaluate(c); switch (cv.getCellType()) { //switch (formulaEvaluator.evaluateInCell(c).getCellType()) { case Cell.CELL_TYPE_BOOLEAN: if (cv.getBooleanValue()) formulaCellValue = "Y"; else formulaCellValue = "F"; break; case Cell.CELL_TYPE_NUMERIC: formulaCellValue = Double.toString(cv.getNumberValue()); break; case Cell.CELL_TYPE_STRING: formulaCellValue = cv.getStringValue(); break; case Cell.CELL_TYPE_BLANK: formulaCellValue = ""; break; case Cell.CELL_TYPE_ERROR: formulaCellValue = Byte.toString(cv.getErrorValue()); break; default: formulaCellValue = ""; break; }//switch return formulaCellValue; default: return ""; }//switch }
From source file:org.isisaddons.module.excel.dom.CellMarshaller.java
License:Apache License
@SuppressWarnings("unchecked") private <T> T getCellValue(final Cell cell, final Class<T> requiredType) { final int cellType = cell.getCellType(); if (requiredType == boolean.class || requiredType == Boolean.class) { if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) { boolean booleanCellValue = cell.getBooleanCellValue(); return (T) Boolean.valueOf(booleanCellValue); } else {/* ww w . j ava 2 s. c o m*/ return null; } } // enum if (Enum.class.isAssignableFrom(requiredType)) { String stringCellValue = cell.getStringCellValue(); @SuppressWarnings("rawtypes") Class rawType = requiredType; return (T) Enum.valueOf(rawType, stringCellValue); } // date if (requiredType == java.util.Date.class) { java.util.Date dateCellValue = cell.getDateCellValue(); return (T) dateCellValue; } if (requiredType == org.apache.isis.applib.value.Date.class) { java.util.Date dateCellValue = cell.getDateCellValue(); return (T) new org.apache.isis.applib.value.Date(dateCellValue); } if (requiredType == org.apache.isis.applib.value.DateTime.class) { java.util.Date dateCellValue = cell.getDateCellValue(); return (T) new org.apache.isis.applib.value.DateTime(dateCellValue); } if (requiredType == LocalDate.class) { java.util.Date dateCellValue = cell.getDateCellValue(); return (T) new LocalDate(dateCellValue.getTime()); } if (requiredType == LocalDateTime.class) { java.util.Date dateCellValue = cell.getDateCellValue(); return (T) new LocalDateTime(dateCellValue.getTime()); } if (requiredType == DateTime.class) { java.util.Date dateCellValue = cell.getDateCellValue(); return (T) new DateTime(dateCellValue.getTime()); } // number if (requiredType == double.class || requiredType == Double.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { double doubleValue = cell.getNumericCellValue(); return (T) Double.valueOf(doubleValue); } else { return null; } } if (requiredType == float.class || requiredType == Float.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { float floatValue = (float) cell.getNumericCellValue(); return (T) Float.valueOf(floatValue); } else { return null; } } if (requiredType == BigDecimal.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { double doubleValue = cell.getNumericCellValue(); return (T) BigDecimal.valueOf(doubleValue); } else { return null; } } if (requiredType == BigInteger.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { long longValue = (long) cell.getNumericCellValue(); return (T) BigInteger.valueOf(longValue); } else { return null; } } if (requiredType == long.class || requiredType == Long.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { long longValue = (long) cell.getNumericCellValue(); return (T) Long.valueOf(longValue); } else { return null; } } if (requiredType == int.class || requiredType == Integer.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { int intValue = (int) cell.getNumericCellValue(); return (T) Integer.valueOf(intValue); } else { return null; } } if (requiredType == short.class || requiredType == Short.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { short shortValue = (short) cell.getNumericCellValue(); return (T) Short.valueOf(shortValue); } else { return null; } } if (requiredType == byte.class || requiredType == Byte.class) { if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { byte byteValue = (byte) cell.getNumericCellValue(); return (T) Byte.valueOf(byteValue); } else { return null; } } if (requiredType == String.class) { if (cellType == HSSFCell.CELL_TYPE_STRING) { return (T) cell.getStringCellValue(); } else { return null; } } return null; }
From source file:org.isource.util.CSVUtils.java
private static List<List> readWorkbook(HSSFWorkbook workbook) { List<List> lines = new ArrayList<List>(); workbook = evaluateFormulas(workbook); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next();/*www. j a va2 s. c om*/ List<String> line = new ArrayList<String>(); //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: line.add(new Boolean(cell.getBooleanCellValue()).toString()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); line.add(dateFormat.format(cell.getDateCellValue())); } else { line.add(new Double(cell.getNumericCellValue()).toString()); } break; case Cell.CELL_TYPE_STRING: line.add(cell.getStringCellValue()); break; case Cell.CELL_TYPE_FORMULA: switch (cell.getCachedFormulaResultType()) { case Cell.CELL_TYPE_NUMERIC: line.add(new Double(cell.getNumericCellValue()).toString()); break; case Cell.CELL_TYPE_STRING: line.add(cell.getRichStringCellValue().toString()); break; } break; } } lines.add(line); } return lines; }
From source file:org.jberet.support.io.ExcelUserModelItemReader.java
License:Open Source License
protected Object getCellValue(final Cell c, final int cellType) { final Object cellValue; switch (cellType) { case Cell.CELL_TYPE_STRING: cellValue = c.getStringCellValue(); break;/*w w w . ja v a 2s . co m*/ case Cell.CELL_TYPE_BOOLEAN: cellValue = c.getBooleanCellValue(); break; case Cell.CELL_TYPE_NUMERIC: cellValue = DateUtil.isCellDateFormatted(c) ? c.getDateCellValue() : c.getNumericCellValue(); break; case Cell.CELL_TYPE_BLANK: cellValue = null; break; case Cell.CELL_TYPE_FORMULA: if (formulaEvaluator == null) { formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator(); } formulaEvaluator.evaluateFormulaCell(c); cellValue = getCellValue(c, c.getCachedFormulaResultType()); break; default: cellValue = c.getStringCellValue(); break; } return cellValue; }