List of usage examples for org.apache.poi.ss.usermodel RichTextString getString
String getString();
From source file:com.siberhus.tdfl.excel.DefaultExcelRowReader.java
License:Apache License
private Object getCellValue(Cell cell) { if (cell == null) return null; switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: RichTextString rts = cell.getRichStringCellValue(); if (rts != null) { return rts.getString(); }//from w w w . j a v a 2 s . com return null; case Cell.CELL_TYPE_NUMERIC: String value = cell.toString(); /* * In POI we cannot know which cell is date or number because both * cells have numeric type To fix this problem we need to call * toString if it's number cell we can parse it but if it's date * cell we cannot parse the value with number parser */ try { return new BigDecimal(value); } catch (Exception e) { return cell.getDateCellValue(); } case Cell.CELL_TYPE_BLANK: return null; case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); } return null; }
From source file:de.jlo.talendcomp.excel.SpreadsheetInput.java
License:Apache License
public String getCommentCellValue(int columnIndex, boolean nullable, boolean trim, boolean useLast) throws Exception { String value = null;/*www . j a v a2s.c o m*/ Cell cell = getCell(columnIndex); if (cell == null) { if (nullable == false) { throw new Exception("Cell in column " + columnIndex + " has no value!"); } } else { Comment comment = cell.getCellComment(); if (comment == null) { if (nullable == false) { throw new Exception("Cell in column " + columnIndex + " has no value!"); } } else { RichTextString rt = comment.getString(); if (rt == null) { if (nullable == false) { throw new Exception("Cell in column " + columnIndex + " has no value!"); } } else { value = rt.getString(); if (value != null) { value = value.trim(); } } } } if (useLast && (value == null || value.isEmpty())) { value = (String) lastValueMap.get(columnIndex); } else { lastValueMap.put(columnIndex, value); } return value; }
From source file:de.jlo.talendcomp.excel.SpreadsheetInput.java
License:Apache License
public boolean isCellCommentEmpty(int columnIndex) { Cell cell = getCell(columnIndex);//from ww w. j a v a2s. c o m if (cell == null) { return true; } Comment comment = cell.getCellComment(); if (comment == null) { return true; } else { RichTextString rt = comment.getString(); if (rt == null) { return true; } else { return rt.getString() != null ? rt.getString().trim().isEmpty() : true; } } }
From source file:midas.sheeco.processor.PayloadFillerTest.java
License:Apache License
@Test public void fillAllAttributes() throws ParseException { final Sheet sheet = Mockito.mock(Sheet.class); final Row row = Mockito.mock(Row.class); final Cell cell = Mockito.mock(Cell.class); final RichTextString richString = Mockito.mock(RichTextString.class); Mockito.when(richString.getString()).thenReturn("Floofly", "0", "2014-12-12 01:01:01.000"); Mockito.when(cell.getRichStringCellValue()).thenReturn(richString); Mockito.when(row.getCell(Mockito.anyInt(), Mockito.any(MissingCellPolicy.class))).thenReturn(cell); Mockito.when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING); final FormulaEvaluator evaluator = Mockito.mock(FormulaEvaluator.class); final Payload<Cat> payload = new Payload<>(Cat.class); final Cat instance = payload.newInstance(); final PayloadContext<Cat> ctx = new PayloadContext<>(sheet, evaluator, payload); PayloadFiller.fillAttributes(instance, row, ctx); Assert.assertEquals("Floofly", instance.getName()); Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2014-12-12 01:01:01.000"), instance.getBirthDate());// w w w. j a v a2 s . co m Assert.assertEquals(Boolean.FALSE, instance.isMale()); }
From source file:midas.sheeco.processor.PayloadFillerTest.java
License:Apache License
@Test public void fillAttributesNull() throws ParseException { final Sheet sheet = Mockito.mock(Sheet.class); final Row row = Mockito.mock(Row.class); final Cell cell = Mockito.mock(Cell.class); final RichTextString richString = Mockito.mock(RichTextString.class); Mockito.when(richString.getString()).thenReturn("", "", ""); Mockito.when(cell.getRichStringCellValue()).thenReturn(richString); Mockito.when(row.getCell(Mockito.anyInt(), Mockito.any(MissingCellPolicy.class))).thenReturn(cell); Mockito.when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING); final FormulaEvaluator evaluator = Mockito.mock(FormulaEvaluator.class); final Payload<Cat> payload = new Payload<>(Cat.class); final Cat instance = payload.newInstance(); final PayloadContext<Cat> ctx = new PayloadContext<>(sheet, evaluator, payload); PayloadFiller.fillAttributes(instance, row, ctx); Assert.assertNull(instance.getName()); Assert.assertNull(instance.getBirthDate()); Assert.assertNull(instance.isMale()); }
From source file:midas.sheeco.processor.PayloadFillerTest.java
License:Apache License
@Test public void testFillElements() { final Sheet sheet = Mockito.mock(Sheet.class); final Row row = Mockito.mock(Row.class); final Cell cell = Mockito.mock(Cell.class); final RichTextString richString = Mockito.mock(RichTextString.class); Mockito.when(richString.getString()).thenReturn("1", "White", "2", "Black"); Mockito.when(cell.getRichStringCellValue()).thenReturn(richString); Mockito.when(row.getCell(Mockito.anyInt(), Mockito.any(MissingCellPolicy.class))).thenReturn(cell); Mockito.when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING); final Payload<Cat> payload = new Payload<>(Cat.class); final Cat instance = payload.newInstance(); final FormulaEvaluator evaluator = Mockito.mock(FormulaEvaluator.class); final PayloadContext<Cat> ctx = new PayloadContext<>(sheet, evaluator, payload); PayloadFiller.fillElements(instance, row, ctx); Assert.assertNotNull(instance.getBody()); Assert.assertNotNull(instance.getTail()); Assert.assertEquals(Integer.valueOf(1), instance.getBody().getHairLength()); Assert.assertEquals(Integer.valueOf(2), instance.getTail().getHairLength()); Assert.assertEquals("White", instance.getBody().getHairColor()); Assert.assertEquals("Black", instance.getTail().getHairColor()); }
From source file:midas.sheeco.processor.PayloadFillerTest.java
License:Apache License
@Test public void testFillElementsNullFields() { final Sheet sheet = Mockito.mock(Sheet.class); final Row row = Mockito.mock(Row.class); final Cell cell = Mockito.mock(Cell.class); final RichTextString richString = Mockito.mock(RichTextString.class); // The adapter excepts that the cell return BLANK on NULLs Mockito.when(richString.getString()).thenReturn("", "", "", ""); Mockito.when(cell.getRichStringCellValue()).thenReturn(richString); Mockito.when(row.getCell(Mockito.anyInt(), Mockito.any(MissingCellPolicy.class))).thenReturn(cell); Mockito.when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING); final Payload<Cat> payload = new Payload<>(Cat.class); final Cat instance = payload.newInstance(); final FormulaEvaluator evaluator = Mockito.mock(FormulaEvaluator.class); final PayloadContext<Cat> ctx = new PayloadContext<>(sheet, evaluator, payload); PayloadFiller.fillElements(instance, row, ctx); Assert.assertNotNull(instance.getBody()); Assert.assertNotNull(instance.getTail()); Assert.assertNull(instance.getBody().getHairLength()); Assert.assertNull(instance.getTail().getHairLength()); Assert.assertNull(instance.getBody().getHairColor()); Assert.assertNull(instance.getTail().getHairColor()); }
From source file:org.dbflute.helper.io.xls.DfTableXlsReader.java
License:Apache License
protected void setupColumns(DfDataTable table, Row nameRow, Row valueRow) { for (int i = 0;; ++i) { final Cell nameCell = nameRow.getCell(i); if (nameCell == null) { break; }/*from w ww .jav a 2 s .co m*/ final RichTextString richStringCellValue = nameCell.getRichStringCellValue(); if (richStringCellValue == null) { break; } final String columnName = richStringCellValue.getString().trim(); if (columnName.length() == 0) { break; } Cell valueCell = null; if (valueRow != null) { valueCell = valueRow.getCell(i); } if (valueCell != null) { table.addColumn(columnName, getColumnType(valueCell)); } else { table.addColumn(columnName); } } }
From source file:org.dbflute.logic.manage.freegen.table.xls.DfXlsTableLoader.java
License:Apache License
protected boolean processColumnValue(final String requestName, final Map<String, String> columnMap, final Row row, final Map<String, Object> beanMap, final String key, final String value, List<DfFreeGenLazyReflector> reflectorList, Map<String, Map<String, String>> mappingMap) { if (convertByMethod(requestName, beanMap, key, value, reflectorList)) { return false; }//from w w w . ja v a 2 s .c o m // normal setting (cell number) boolean exists = false; final Integer cellNumber; try { cellNumber = Integer.valueOf(value) - 1; } catch (NumberFormatException e) { String msg = "The property value should be Integer in FreeGen " + requestName + ":"; msg = msg + " key=" + key + " value=" + value; throw new DfIllegalPropertySettingException(msg); } final Cell cell = row.getCell(cellNumber); if (cell == null) { return false; } final RichTextString cellValue = cell.getRichStringCellValue(); if (cellValue == null) { return false; } exists = true; String resultValue = cellValue.getString(); final Map<String, String> mapping = mappingMap.get(key); if (mapping != null) { final String mappingValue = mapping.get(resultValue); if (mappingValue != null) { resultValue = mappingValue; } } beanMap.put(key, resultValue); return exists; }
From source file:org.isisaddons.module.excel.dom.CellMarshaller.java
License:Apache License
private Object getCellComment(final Cell cell, final Class<?> requiredType) { final Comment comment = cell.getCellComment(); if (comment == null) { return null; }// w w w. jav a 2 s .c o m final RichTextString commentRts = comment.getString(); if (commentRts == null) { return null; } final String bookmarkStr = commentRts.getString(); final Bookmark bookmark = new Bookmark(bookmarkStr); return bookmarkService.lookup(bookmark, requiredType); }