Example usage for org.apache.poi.ss.usermodel Cell getRichStringCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getRichStringCellValue

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getRichStringCellValue.

Prototype

RichTextString getRichStringCellValue();

Source Link

Document

Get the value of the cell as a XSSFRichTextString

For numeric cells we throw an exception.

Usage

From source file:magicware.scm.redmine.tools.util.ExcelUtils.java

License:Apache License

public static String getCellContent(Cell cell, FormulaEvaluator evaluator) {

    String result = null;/*w  w  w .j  a v a 2 s  . co m*/

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        result = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            result = Constants.DATE_FORMAT.format(cell.getDateCellValue());
        } else {
            result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        switch (evaluator.evaluateFormulaCell(cell)) {
        case Cell.CELL_TYPE_BOOLEAN:
            result = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                result = Constants.DATE_FORMAT.format(cell.getDateCellValue());
            } else {
                result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
            }
            break;
        case Cell.CELL_TYPE_STRING:
            result = String.valueOf(cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BLANK:
            break;
        case Cell.CELL_TYPE_ERROR:
            result = String.valueOf(cell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            break;
        }
        break;
    default:
        break;
    }
    return result;
}

From source file:main.KeywordList.java

public void writeToExcel(String fileName) {
    final int GROUP = 0;
    final int PACKAGENAME = 1;
    final int SUBPACKAGENAME = 2;
    final int POSITIONTYPE = 3;
    final int KEYWORD = 4;
    final int DATE = 5;
    final int PRIORITY = 6;

    try {//from   w w  w .j a  va2  s.c om
        System.out.println("Writing XML: " + fileName);
        ForcastUi.consoleLog("Writing XML: " + fileName);

        FileInputStream fileIn = new FileInputStream(fileName);

        Workbook wb = WorkbookFactory.create(fileIn);
        Sheet sheet = wb.getSheetAt(0);

        keywordList.stream().filter(key -> key.isUsed()).forEach(keyword -> {
            //?  ? or Don't
            int rowInt = 1;
            while (true) {
                Row row = sheet.getRow(rowInt);

                if (row == null)
                    break;

                Cell cellKeyword = row.getCell(KEYWORD);
                String shortName = cellKeyword.getRichStringCellValue().getString();
                if (shortName.equalsIgnoreCase(keyword.getKeyword())) {//If Match, Update Date
                    Cell cellDate = row.getCell(DATE);
                    cellDate.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cellDate.setCellValue(new Date());
                    //cellDate.setCellValue(ForcastUi.dateToString(new Date()));
                    //cellDate.setCellType(Cell.CELL_TYPE_STRING);
                    //cellDate.setCellValue(ForcastUi.dateToString(new Date()));
                    //break;
                }
                rowInt++;
            }
        });

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream(fileName);
        wb.write(fileOut);
        fileOut.close();
        fileIn.close();

    } catch (FileNotFoundException e) {
        ErrorMessages.printErrorMsg(ErrorMessages.FILENOTFOUND, fileName);
        ForcastUi.consoleLog(e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        ErrorMessages.printErrorMsg(ErrorMessages.FILECOR, fileName);
        ForcastUi.consoleLog(e.getMessage());
        e.printStackTrace();
    } catch (Exception ex) {
        ErrorMessages.printErrorMsg(ErrorMessages.FILECOR, fileName);
        ForcastUi.consoleLog(ex.getMessage());
        Logger.getLogger(TopStockDescriptionList.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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 ava 2s .c  o  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:midas.sheeco.type.adapter.SpreadsheetBooleanAdapterTest.java

License:Apache License

@Test
public void testStringTrue() {
    SpreadsheetBooleanAdapter adapter = new SpreadsheetBooleanAdapter();

    Cell cell = mock(Cell.class);
    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING);
    when(cell.getRichStringCellValue()).thenReturn(new HSSFRichTextString("TRUe"));

    Boolean value = adapter.fromSpreadsheet(cell);
    Assert.assertTrue(value);//from  ww w. j  a  v  a2s .  c  om
}

From source file:midas.sheeco.type.adapter.SpreadsheetBooleanAdapterTest.java

License:Apache License

@Test
public void testStringYes() {
    SpreadsheetBooleanAdapter adapter = new SpreadsheetBooleanAdapter();

    Cell cell = mock(Cell.class);
    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING);
    when(cell.getRichStringCellValue()).thenReturn(new HSSFRichTextString("YeS"));

    Boolean value = adapter.fromSpreadsheet(cell);
    Assert.assertTrue(value);//from  w  ww  .j  av a2  s. c  om
}

From source file:midas.sheeco.type.adapter.SpreadsheetBooleanAdapterTest.java

License:Apache License

@Test
public void testString1() {
    SpreadsheetBooleanAdapter adapter = new SpreadsheetBooleanAdapter();

    Cell cell = mock(Cell.class);
    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING);
    when(cell.getRichStringCellValue()).thenReturn(new HSSFRichTextString("1"));

    Boolean value = adapter.fromSpreadsheet(cell);
    Assert.assertTrue(value);/*from  w w w  .  j  a va  2 s  .c o m*/
}

From source file:midas.sheeco.type.adapter.SpreadsheetBooleanAdapterTest.java

License:Apache License

@Test
public void testStringFalse() {
    SpreadsheetBooleanAdapter adapter = new SpreadsheetBooleanAdapter();

    Cell cell = mock(Cell.class);
    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_STRING);
    when(cell.getRichStringCellValue()).thenReturn(new HSSFRichTextString("FaLsE"));

    Boolean value = adapter.fromSpreadsheet(cell);
    Assert.assertFalse(value);/*from   w  w w . j  a v a  2s  .c o m*/
}