Example usage for org.apache.poi.xssf.usermodel XSSFRichTextString XSSFRichTextString

List of usage examples for org.apache.poi.xssf.usermodel XSSFRichTextString XSSFRichTextString

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRichTextString XSSFRichTextString.

Prototype

@Internal
public XSSFRichTextString(CTRst st) 

Source Link

Document

Create a rich text string from the supplied XML bean

Usage

From source file:net.geoprism.data.etl.excel.XSSFSheetXMLHandler.java

License:Open Source License

public void endElement(String uri, String localName, String name) throws SAXException {
    String thisStr = null;//from ww w  .j ava 2s.co  m
    String thisData = null;

    // v => contents of a cell
    if (isTextTag(name)) {
        vIsOpen = false;

        // Process the value contents as required, now we have it all
        switch (nextDataType) {
        case BOOLEAN:
            char first = value.charAt(0);
            thisStr = first == '0' ? "FALSE" : "TRUE";
            thisData = new String(thisStr);
            break;

        case ERROR:
            thisStr = "ERROR:" + value.toString();
            thisData = new String(thisStr);
            break;

        case FORMULA:
            if (formulasNotResults) {
                thisStr = formula.toString();
                thisData = new String(thisStr);
            } else {
                String fv = value.toString();

                if (this.formatString != null) {
                    try {
                        // Try to use the value as a formattable number
                        double d = Double.parseDouble(fv);
                        thisStr = this.cellFormatter.formatRawCellContents(d, this.formatIndex,
                                this.formatString);
                        thisData = this.contentFormatter.formatRawCellContents(d, this.formatIndex,
                                this.formatString);
                    } catch (NumberFormatException e) {
                        // Formula is a String result not a Numeric one
                        thisStr = fv;
                        thisData = new String(thisStr);
                    }
                } else {
                    // No formating applied, just do raw value in all cases
                    thisStr = fv;
                    thisData = new String(thisStr);
                }
            }
            break;

        case INLINE_STRING:
            // TODO: Can these ever have formatting on them?
            XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
            thisStr = rtsi.toString();
            thisData = new String(thisStr);
            break;

        case TEXT:
            String sstIndex = value.toString();
            try {
                int idx = Integer.parseInt(sstIndex);
                XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx));
                thisStr = rtss.toString();
                thisData = new String(thisStr);
            } catch (NumberFormatException ex) {
                System.err.println("Failed to parse SST index '" + sstIndex + "': " + ex.toString());
            }
            break;

        case NUMBER:
            String n = value.toString();
            if (this.formatString != null) {
                thisStr = cellFormatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex,
                        this.formatString);
                thisData = this.contentFormatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex,
                        this.formatString);
            } else {
                thisStr = n;
                thisData = new String(thisStr);
            }
            break;

        default:
            thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
            thisData = new String(thisStr);
            break;
        }

        if (DateUtil.isADateFormat(this.formatIndex, this.formatString)) {
            output.cell(cellRef, thisData, thisStr, ColumnType.DATE);
        } else {
            output.cell(cellRef, thisData, thisStr, nextDataType);
        }
    } else if ("f".equals(name)) {
        fIsOpen = false;
    } else if ("is".equals(name)) {
        isIsOpen = false;
    } else if ("row".equals(name)) {
        output.endRow();
    } else if ("oddHeader".equals(name) || "evenHeader".equals(name) || "firstHeader".equals(name)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), true, name);
    } else if ("oddFooter".equals(name) || "evenFooter".equals(name) || "firstFooter".equals(name)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), false, name);
    }
}

From source file:net.mcnewfamily.rmcnew.writer.AbstractXlsxWriter.java

License:Open Source License

protected void createTextBox(XSSFSheet xssfSheet, XSSFClientAnchor xssfClientAnchor, String text) {
    if (xssfSheet != null) {
        XSSFDrawing xssfDrawing = xssfSheet.createDrawingPatriarch();
        XSSFTextBox xssfTextBox = xssfDrawing.createTextbox(xssfClientAnchor);
        xssfTextBox.setText(new XSSFRichTextString(text));

    } else {/*w  w w. j a va  2s .c  o  m*/
        throw new IllegalArgumentException("Cannot create drawing on  null or empty sheet!");
    }
}

From source file:net.rrm.ehour.ui.common.report.excel.CellFactory.java

License:Open Source License

public static Cell createCell(Row row, int column, Object value, ExcelWorkbook workbook,
        ExcelStyle excelStyle) {/*from ww w .  ja v a 2s .  c  o m*/
    Cell cell = row.createCell(column);

    if (value instanceof Float) {
        cell.setCellValue((Float) value);
    } else if (value instanceof Number) {
        cell.setCellValue(((Number) value).doubleValue());
    } else if (value instanceof Date) {
        cell.setCellValue((Date) value);
    } else if (value instanceof LockableDate) {
        cell.setCellValue(((LockableDate) value).getDate());
    } else {
        cell.setCellValue(new XSSFRichTextString(value.toString()));
    }

    cell.setCellStyle(workbook.getCellStyle(excelStyle));

    return cell;
}

From source file:org.apache.metamodel.excel.XlsxSheetToRowsHandler.java

License:Apache License

private String createValue() {
    if (_value.length() == 0) {
        return null;
    }/*from  w w  w .  jav a2s. c o m*/

    switch (_dataType) {

    case BOOL:
        char first = _value.charAt(0);
        return first == '0' ? "false" : "true";
    case ERROR:
        logger.warn("Error-cell occurred: {}", _value);
        return _value.toString();
    case FORMULA:
        return _value.toString();
    case INLINESTR:
        XSSFRichTextString rtsi = new XSSFRichTextString(_value.toString());
        return rtsi.toString();
    case SSTINDEX:
        String sstIndex = _value.toString();
        int idx = Integer.parseInt(sstIndex);
        XSSFRichTextString rtss = new XSSFRichTextString(_sharedStringTable.getEntryAt(idx));
        return rtss.toString();
    case NUMBER:
        final String numberString = _value.toString();
        if (_formatString != null) {
            DataFormatter formatter = getDataFormatter();
            if (HSSFDateUtil.isADateFormat(_formatIndex, _formatString)) {
                Date date = DateUtil.getJavaDate(Double.parseDouble(numberString));
                return DateUtils.createDateFormat().format(date);
            }
            return formatter.formatRawCellContents(Double.parseDouble(numberString), _formatIndex,
                    _formatString);
        } else {
            if (numberString.endsWith(".0")) {
                // xlsx only stores doubles, so integers get ".0" appended
                // to them
                return numberString.substring(0, numberString.length() - 2);
            }
            return numberString;
        }
    default:
        logger.error("Unsupported data type: {}", _dataType);
        return "";
    }
}

From source file:org.dhatim.fastexcel.reader.RowSpliterator.java

License:Apache License

private Row next() throws XMLStreamException {
    if (!"row".equals(r.getLocalName())) {
        throw new NoSuchElementException();
    }//  www.ja  v  a  2s .c  o m
    int rowIndex = r.getIntAttribute("r");
    ArrayList<Cell> cells = new ArrayList<>(rowCapacity);
    int physicalCellCount = 0;

    while (r.goTo(() -> r.isStartElement("c") || r.isEndElement("row"))) {
        if ("row".equals(r.getLocalName())) {
            break;
        }
        String cellRef = r.getAttribute("r");
        CellAddress addr = new CellAddress(cellRef);
        String type = r.getOptionalAttribute("t").orElse("n");

        Object value;
        CellType cellType;
        String formula;
        String rawValue;
        if ("inlineStr".equals(type)) {
            Object v = null;
            String f = null;
            String rv = null;
            while (r.goTo(() -> r.isStartElement("is") || r.isEndElement("c") || r.isStartElement("f"))) {
                if ("is".equals(r.getLocalName())) {
                    rv = r.getValueUntilEndElement("is");
                    XSSFRichTextString rtss = new XSSFRichTextString(rv);
                    v = rtss.toString();
                } else if ("f".equals(r.getLocalName())) {
                    f = r.getValueUntilEndElement("f");
                } else {
                    break;
                }
            }
            value = v;
            formula = f;
            cellType = f == null ? CellType.STRING : CellType.FORMULA;
            rawValue = rv;
        } else if ("s".equals(type)) {
            r.goTo("v");
            int index = Integer.parseInt(r.getValueUntilEndElement("v"));
            CTRst ctrst = workbook.getSharedStringsTable().getEntryAt(index);
            XSSFRichTextString rtss = new XSSFRichTextString(ctrst);
            value = rtss.toString();
            cellType = CellType.STRING;
            formula = null;
            rawValue = ctrst.xmlText();
        } else {
            Function<String, ?> parser;
            CellType definedType;
            switch (type) {
            case "b":
                definedType = CellType.BOOLEAN;
                parser = RowSpliterator::parseBoolean;
                break;
            case "e":
                definedType = CellType.ERROR;
                parser = Function.identity();
                break;
            case "n":
                definedType = CellType.NUMBER;
                parser = RowSpliterator::parseNumber;
                break;
            case "str":
                definedType = CellType.FORMULA;
                parser = Function.identity();
                break;
            default:
                throw new IllegalStateException("Unknown cell type : " + type);
            }

            Object v = null;
            String f = null;
            String rv = null;
            while (r.goTo(() -> r.isStartElement("v") || r.isEndElement("c") || r.isStartElement("f"))) {
                if ("v".equals(r.getLocalName())) {
                    rv = r.getValueUntilEndElement("v");
                    v = "".equals(rv) ? null : parser.apply(rv);
                } else if ("f".equals(r.getLocalName())) {
                    String ref = r.getAttribute("ref");
                    String t = r.getAttribute("t");
                    f = r.getValueUntilEndElement("f");
                    if ("array".equals(t) && ref != null) {
                        CellRangeAddress range = CellRangeAddress.valueOf(ref);
                        sharedFormula.put(range, f);
                    }
                } else {
                    break;
                }
            }

            if (f == null) {
                f = getSharedFormula(addr).orElse(null);
            }

            if (f == null && definedType == CellType.NUMBER && v == null) {
                cellType = CellType.EMPTY;
                formula = null;
                value = null;
                rawValue = rv;
            } else {
                cellType = f == null ? definedType : CellType.FORMULA;
                formula = f;
                value = v;
                rawValue = rv;
            }

        }

        if (addr.getColumn() >= cells.size()) {
            setSize(cells, addr.getColumn() + 1);
        }

        Cell cell = new Cell(workbook, cellType, value, addr, formula, rawValue);
        cells.set(addr.getColumn(), cell);
        physicalCellCount++;
    }
    rowCapacity = Math.max(rowCapacity, cells.size());
    return new Row(rowIndex, physicalCellCount, cells.toArray(new Cell[cells.size()]));
}

From source file:org.displaytag.render.XssfTableWriter.java

License:Artistic License

/**
 * @see org.displaytag.render.TableWriterTemplate#writeCaption(org.displaytag.model.TableModel)
 *//*from www.  j  a  v  a  2s  . c  o  m*/
protected void writeCaption(TableModel model) throws Exception {
    XSSFFont captionFont = wb.createFont();
    captionFont.setFontHeightInPoints((short) 14);
    captionFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
    captionFont.setBold(true);
    captionFont.setItalic(false);

    CellStyle captionstyle = this.wb.createCellStyle();
    captionstyle.setAlignment(CellStyle.ALIGN_CENTER);
    captionstyle.setFont(captionFont);

    this.colNum = 0;
    this.currentRow = this.sheet.createRow(this.rowNum++);
    this.currentCell = this.currentRow.createCell(this.colNum++);
    this.currentCell.setCellStyle(captionstyle);
    String caption = model.getCaption();
    this.currentCell.setCellValue(new XSSFRichTextString(caption));
    this.rowSpanTable(model);
}

From source file:org.displaytag.render.XssfTableWriter.java

License:Artistic License

/**
 * @see org.displaytag.render.TableWriterTemplate#writeColumnValue(Object,org.displaytag.model.Column)
 *//* ww w. ja  v a2 s.co  m*/
protected void writeColumnValue(Object value, Column column) throws Exception {
    if (value instanceof Number) {
        Number num = (Number) value;
        // Percentage
        if (value.toString().indexOf("%") > -1) {
            this.currentCell.setCellValue(num.doubleValue() / 100);
            XSSFCellStyle cellStyle = this.wb.createCellStyle();
            if (this.pctFormat == -1) {
                this.pctFormat = this.wb.createDataFormat().getFormat("0.00%");
            }
            cellStyle.setDataFormat(this.pctFormat);
            this.currentCell.setCellStyle(cellStyle);
        } else {
            this.currentCell.setCellValue(num.doubleValue());
        }
    } else if (value instanceof Date) {
        this.currentCell.setCellValue((Date) value);
    } else if (value instanceof Calendar) {
        this.currentCell.setCellValue((Calendar) value);
    } else {
        this.currentCell.setCellValue(new XSSFRichTextString(this.escapeColumnValue(value)));
    }
}

From source file:org.displaytag.render.XssfTableWriter.java

License:Artistic License

/**
 * Writes a table header or a footer./*from  w  w w .  java 2s.  co m*/
 * @param value Header or footer value to be rendered.
 * @param row The row in which to write the header or footer.
 * @param style Style used to render the header or footer.
 */
private void writeHeaderFooter(String value, XSSFRow row, XSSFCellStyle style) {
    this.currentCell = row.createCell(this.colNum++);
    this.currentCell.setCellValue(new XSSFRichTextString(value));
    this.currentCell.setCellStyle(style);
}

From source file:org.jberet.support.io.ExcelStreamingItemReader.java

License:Open Source License

private String getCellStringValue() throws Exception {
    String result = null;/*  www .  j  a  v a2 s  .co m*/
    final String cellType = sheetStreamReader.getAttributeValue(null, "t");
    while (sheetStreamReader.hasNext()) {
        final int event = sheetStreamReader.next();
        if (event == XMLStreamConstants.START_ELEMENT && "v".equals(sheetStreamReader.getLocalName())) {
            result = sheetStreamReader.getElementText();
            if ("s".equals(cellType)) {
                final int idx = Integer.parseInt(result);
                result = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx)).toString();
            }
        } else if (event == XMLStreamConstants.START_ELEMENT && "t".equals(sheetStreamReader.getLocalName())) {
            result = sheetStreamReader.getElementText();
        } else if (event == XMLStreamConstants.END_ELEMENT && "c".equals(sheetStreamReader.getLocalName())) {
            break;
        }
    }
    return result;
}

From source file:org.jmesa.view.excel.Excel2007View.java

License:Apache License

@Override
public Object render() {

    XSSFWorkbook workbook = new XSSFWorkbook();
    Table table = this.getTable();
    String caption = table.getCaption();
    if (!StringUtils.hasText(caption)) {
        caption = "JMesa Export";
    }//from  ww  w  .j av a  2 s.  c  om
    XSSFSheet sheet = workbook.createSheet(caption);

    Row row = table.getRow();
    row.getRowRenderer();
    List<Column> columns = table.getRow().getColumns();

    // renderer header
    XSSFRow hssfRow = sheet.createRow(0);
    int columncount = 0;
    for (Column col : columns) {
        XSSFCell cell = hssfRow.createCell(columncount++);
        cell.setCellValue(new XSSFRichTextString(col.getTitle()));
    }

    // renderer body
    Collection<?> items = getCoreContext().getPageItems();
    int rowcount = 1;
    for (Object item : items) {
        XSSFRow r = sheet.createRow(rowcount++);
        columncount = 0;
        for (Column col : columns) {
            XSSFCell cell = r.createCell(columncount++);
            Object value = col.getCellRenderer().render(item, rowcount);
            if (value == null) {
                value = "";
            }

            if (value instanceof Number) {
                Double number = Double.valueOf(value.toString());
                cell.setCellValue(number);
            } else {
                cell.setCellValue(new XSSFRichTextString(value.toString()));
            }
        }
    }
    return workbook;
}