Example usage for org.apache.poi.ss.usermodel RichTextString getString

List of usage examples for org.apache.poi.ss.usermodel RichTextString getString

Introduction

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

Prototype

String getString();

Source Link

Document

Returns the plain string representation.

Usage

From source file:b01.officeLink.excel.ExcelRefiller.java

License:Apache License

public void fillGroupDefinition(FocExcelDocument excel) {
    setFocExcelDocument(excel);//from   ww  w  .  ja va2  s .c o m
    Sheet sourceSheet = excel.getSheetAt(1);

    for (int i = 0; i < 100; i++) {
        Row sRow = sourceSheet.getRow(i);
        Cell sCell = sRow != null ? sRow.getCell(0) : null;

        RichTextString groupColValue = sCell != null ? sCell.getRichStringCellValue() : null;
        String groupColValueStr = groupColValue != null ? groupColValue.getString() : null;

        if (groupColValueStr != null) {
            ExcelGroupDefinition groupDef = groupMap.get(groupColValueStr);
            if (groupDef == null) {
                groupDef = new ExcelGroupDefinition();
                groupMap.put(groupColValueStr, groupDef);
            }
            groupDef.addRow(i);
        }
    }
}

From source file:b01.officeLink.excel.ExcelRefiller.java

License:Apache License

public void fillGroupContent(String groupStr, FocObject object) {
    ExcelGroupDefinition grpDef = getGroupDefinition(groupStr);
    Sheet srcSheet = getSourceSheet();//from   w  ww .  ja v  a 2  s .c  om
    Sheet tarSheet = getTargetSheet();
    if (grpDef != null) {
        for (int i = 0; i < grpDef.getRowCount(); i++) {
            int rowIdx = grpDef.getRowAt(i);
            Row sRow = srcSheet.getRow(rowIdx);
            if (sRow != null) {
                Row tRow = tarSheet.getRow(currentRow);
                if (tRow == null) {
                    tRow = tarSheet.createRow(currentRow);
                }
                if (tRow != null) {
                    tRow.setHeight(sRow.getHeight());
                    for (int c = 0; c < 20; c++) {
                        Cell sCell = sRow.getCell(c + 1);
                        if (sCell != null) {
                            Cell tCell = tRow.getCell(c);
                            if (tCell == null) {
                                tCell = tRow.createCell(c);
                            }
                            if (tCell != null) {
                                tCell.setCellStyle(sCell.getCellStyle());

                                String str = "";
                                if (sCell.getCellType() == Cell.CELL_TYPE_STRING) {
                                    RichTextString rts = sCell.getRichStringCellValue();
                                    str = rts.getString();
                                    str = analyseContent(str, object);
                                } else if (sCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                                    str = String.valueOf(sCell.getNumericCellValue());
                                }

                                if (str != null && !str.isEmpty()) {
                                    int iVal = convertString2Integer(str);
                                    double dVal = convertString2Double(str);
                                    if (iVal != Integer.MAX_VALUE) {
                                        tCell.setCellValue(iVal);
                                    } else if (!Double.isNaN(dVal)) {
                                        tCell.setCellValue(dVal);
                                    } else {
                                        if (getFocExcelDocument() != null
                                                && getFocExcelDocument().getWorkbook() != null) {
                                            tCell.setCellValue(getFocExcelDocument().getWorkbook()
                                                    .getCreationHelper().createRichTextString(str));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                currentRow++;
            }
        }
    }
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the string value of a cell//w  w  w.  j a  v  a2 s  .c  om
 * 
 * @param cell
 * @return
 */
static String getStringCellValue(Cell cell) {
    try {
        int cellType = cell.getCellType();
        switch (cellType) {
        case Cell.CELL_TYPE_BLANK:
            return "";
        case Cell.CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue());
        case Cell.CELL_TYPE_ERROR:
            return String.valueOf(cell.getErrorCellValue());
        case Cell.CELL_TYPE_FORMULA:
            return cell.getCellFormula();
        case Cell.CELL_TYPE_NUMERIC:
            try {
                double doubleValue = cell.getNumericCellValue();
                int intValue = (int) doubleValue;
                double fracPart = doubleValue - intValue;
                if (Math.abs(fracPart) <= Double.MIN_VALUE) {
                    return String.valueOf(intValue);
                } else {
                    return String.valueOf(doubleValue);
                }
            } catch (Exception e) {
            }
        case Cell.CELL_TYPE_STRING:
            RichTextString richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                return richTextString.getString();
            }
        }
    } catch (Exception e) {
        LOGGER.debug("Getting the string value failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    }
    return "";
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the Integer value of a cell/*w w  w.  j  a  v  a2 s .c  o m*/
 * 
 * @param cell
 * @return
 */
private static Integer getIntegerCellValue(Cell cell) throws ExcelImportInvalidCellValueException {
    Integer integerValue = null;
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_NUMERIC) {
        Double doubleValue = null;
        try {
            double numericValue = cell.getNumericCellValue();
            doubleValue = new Double(numericValue);
            integerValue = new Integer(doubleValue.intValue());
        } catch (Exception e) {
            if (doubleValue == null) {
                doubleValue = new Double(Double.NaN);
            }
            throw new ExcelImportInvalidCellValueException(doubleValue.toString());
        }
    } else {
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString richTextString = null;
            richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                String stringValue = richTextString.getString();
                if (stringValue != null && !"".equals(stringValue)) {
                    stringValue = stringValue.trim();
                    if (stringValue != null) {
                        try {
                            integerValue = Integer.valueOf(stringValue);
                        } catch (Exception e) {
                            throw new ExcelImportInvalidCellValueException(stringValue);
                        }
                    }
                }
            }
        } else {
            throw new ExcelImportInvalidCellValueException(getStringCellValue(cell));
        }
    }
    return integerValue;
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the Double value of a cell//from w w w .ja va2 s  .  c  o m
 * 
 * @param cell
 * @return
 */
private static Double getDoubleCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException {
    Double doubleValue = null;
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_NUMERIC) {
        double numericValue = cell.getNumericCellValue();
        try {
            doubleValue = new Double(numericValue);
        } catch (Exception e) {
            throw new ExcelImportInvalidCellValueException(String.valueOf(numericValue));
        }
    } else {
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                String stringValue = richTextString.getString();
                if (stringValue != null) {
                    stringValue = stringValue.trim();
                    if (!"".equals(stringValue)) {
                        doubleValue = DoubleNumberFormatUtil.getInstance().parseGUI(stringValue, locale);
                        if (doubleValue == null) {
                            doubleValue = DoubleNumberFormatUtil.parseISO(stringValue);
                            if (doubleValue == null) {
                                throw new ExcelImportInvalidCellValueException(stringValue);
                            }
                        }
                    }
                }
            }
        } else {
            throw new ExcelImportInvalidCellValueException(getStringCellValue(cell));
        }
    }
    return doubleValue;
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the Double value of a cell//from   w ww  .  j  a va2  s  .c o m
 * 
 * @param cell
 * @return
 */
private static Date getDateCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException {
    Date dateValue = null;
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_NUMERIC) {
        try {
            dateValue = cell.getDateCellValue();
        } catch (Exception e) {
            throw new ExcelImportInvalidCellValueException(String.valueOf(cell.getNumericCellValue()));
        }
    } else {
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                String stringValue = richTextString.getString();
                if (stringValue != null) {
                    stringValue = stringValue.trim();
                    if (!"".equals(stringValue)) {
                        dateValue = DateTimeUtils.getInstance().parseGUIDate(stringValue, locale);
                        if (dateValue == null) {
                            dateValue = DateTimeUtils.getInstance().parseShortDate(stringValue, locale);
                            if (dateValue == null) {
                                dateValue = DateTimeUtils.getInstance().parseISODate(stringValue);
                                if (dateValue == null) {
                                    throw new ExcelImportInvalidCellValueException(stringValue);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            throw new ExcelImportInvalidCellValueException(getStringCellValue(cell));
        }
    }
    return dateValue;
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the Double value of a cell/*  w ww  . jav  a 2s .  c om*/
 * 
 * @param cell
 * @return
 */
private static Date getDateTimeCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException {
    Date dateValue = null;
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_NUMERIC) {
        try {
            dateValue = cell.getDateCellValue();
        } catch (Exception e) {
            throw new ExcelImportInvalidCellValueException(String.valueOf(cell.getNumericCellValue()));
        }
    } else {
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                String stringValue = richTextString.getString();
                if (stringValue != null) {
                    stringValue = stringValue.trim();
                    if (!"".equals(stringValue)) {
                        dateValue = DateTimeUtils.getInstance().parseGUIDateTime(stringValue, locale);
                        if (dateValue == null) {
                            dateValue = DateTimeUtils.getInstance().parseShortDateTime(stringValue, locale);
                            if (dateValue == null) {
                                dateValue = DateTimeUtils.getInstance().parseISODateTime(stringValue);
                            }
                        }
                    }
                }
            }
        } else {
            throw new ExcelImportInvalidCellValueException(getStringCellValue(cell));
        }
    }
    return dateValue;
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the Double value of a cell/*from   w  ww  .j av  a2 s .c  o  m*/
 * 
 * @param cell
 * @return
 */
private static Boolean getBooleanCellValue(Cell cell) throws ExcelImportInvalidCellValueException {
    Boolean booleanValue = null;
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_BOOLEAN) {
        boolean boolCellValue = cell.getBooleanCellValue();
        booleanValue = new Boolean(boolCellValue);
    } else {
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                String stringValue = richTextString.getString();
                if (stringValue != null) {
                    stringValue = stringValue.trim();
                    if (!"".equals(stringValue)) {
                        if ("true".equalsIgnoreCase(stringValue)
                                || BooleanFields.TRUE_VALUE.equalsIgnoreCase(stringValue)) {
                            booleanValue = new Boolean(true);
                        } else {
                            if ("false".equalsIgnoreCase(stringValue)
                                    || BooleanFields.FALSE_VALUE.equalsIgnoreCase(stringValue)) {
                                booleanValue = new Boolean(false);
                            } else {
                                if (stringValue != null && !"".equals(stringValue.trim())) {
                                    throw new ExcelImportInvalidCellValueException(stringValue);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            throw new ExcelImportInvalidCellValueException(getStringCellValue(cell));
        }
    }
    return booleanValue;
}

From source file:com.dua3.meja.model.poi.PoiCell.java

License:Apache License

public RichText toRichText(RichTextString rts) {
    String text = rts.getString();
    //TODO: properly process tabs
    text = text.replace('\t', ' '); // tab
    text = text.replace((char) 160, ' '); // non-breaking space

    RichTextBuilder rtb = new RichTextBuilder();
    int start = 0;
    for (int i = 0; i < rts.numFormattingRuns(); i++) {
        start = rts.getIndexOfFormattingRun(i);
        int end = i + 1 < rts.numFormattingRuns() ? rts.getIndexOfFormattingRun(i + 1) : rts.length();

        if (start == end) {
            // skip empty
            continue;
        }//  ww  w . j  a v a2  s  .c  o m

        // apply font attributes for formatting run
        PoiFont runFont = getFontForFormattingRun(rts, i);
        rtb.push(Style.FONT_FAMILY, runFont.getFamily());
        rtb.push(Style.FONT_SIZE, runFont.getSizeInPoints() + "pt");
        rtb.push(Style.COLOR, runFont.getColor().toString());
        if (runFont.isBold()) {
            rtb.push(Style.FONT_WEIGHT, "bold");
        }
        if (runFont.isItalic()) {
            rtb.push(Style.FONT_STYLE, "italic");
        }
        if (runFont.isUnderlined()) {
            rtb.push(Style.TEXT_DECORATION, "underline");
        }
        if (runFont.isStrikeThrough()) {
            rtb.push(Style.TEXT_DECORATION, "line-through");
        }

        rtb.append(text, start, end);
        start = end;
    }
    rtb.append(text, start, text.length());

    return rtb.toRichText();
}

From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetStringAdapter.java

License:Apache License

@Override
public String fromSpreadsheet(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        final RichTextString text = cell.getRichStringCellValue();
        final String value = text.getString().trim();
        return !value.isEmpty() ? value : null;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return String.valueOf(SpreadsheetDateAdapter.DATE_PATTERNS[0].format(cell.getDateCellValue()));
        } else {/*ww w  .j a v  a 2s.  com*/
            return decimalFormat.format(cell.getNumericCellValue());
        }
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_ERROR:
    case Cell.CELL_TYPE_FORMULA:
    default:
        throw new InvalidCellFormatException(
                "The cell type: " + cell.getCellType() + " is either not supported or not possible");
    }
}