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:org.pentaho.reporting.ui.datasources.table.ImportFromFileTask.java

License:Open Source License

private void importFromFile(final File file, final boolean firstRowIsHeader) {
    final ByteArrayOutputStream bout = new ByteArrayOutputStream(Math.max(8192, (int) file.length()));
    try {/*from w w  w.java 2s  . co  m*/
        final InputStream fin = new FileInputStream(file);
        try {
            IOUtils.getInstance().copyStreams(new BufferedInputStream(fin), bout);
        } finally {
            fin.close();
        }

        if (Thread.currentThread().isInterrupted()) {
            return;
        }

        final Workbook workbook = WorkbookFactory.create(new ByteArrayInputStream(bout.toByteArray()));
        int sheetIndex = 0;
        if (workbook.getNumberOfSheets() > 1) {
            final SheetSelectorDialog selectorDialog = new SheetSelectorDialog(workbook, parent);
            if (selectorDialog.performSelection()) {
                sheetIndex = selectorDialog.getSelectedIndex();
            } else {
                return;
            }
        }

        final TypedTableModel tableModel = new TypedTableModel();
        final Sheet sheet = workbook.getSheetAt(sheetIndex);
        final Iterator rowIterator = sheet.rowIterator();

        if (firstRowIsHeader) {
            if (rowIterator.hasNext()) {
                final Row headerRow = (Row) rowIterator.next();
                final short cellCount = headerRow.getLastCellNum();
                for (short colIdx = 0; colIdx < cellCount; colIdx++) {
                    final Cell cell = headerRow.getCell(colIdx);
                    if (cell != null) {
                        while (colIdx > tableModel.getColumnCount()) {
                            tableModel.addColumn(Messages.getString("TableDataSourceEditor.Column",
                                    String.valueOf(tableModel.getColumnCount())), Object.class);
                        }

                        final RichTextString string = cell.getRichStringCellValue();
                        if (string != null) {
                            tableModel.addColumn(string.getString(), Object.class);
                        } else {
                            tableModel.addColumn(
                                    Messages.getString("TableDataSourceEditor.Column", String.valueOf(colIdx)),
                                    Object.class);
                        }
                    }
                }
            }
        }

        Object[] rowData = null;
        while (rowIterator.hasNext()) {
            final Row row = (Row) rowIterator.next();
            final short cellCount = row.getLastCellNum();
            if (cellCount == -1) {
                continue;
            }
            if (rowData == null || rowData.length != cellCount) {
                rowData = new Object[cellCount];
            }

            for (short colIdx = 0; colIdx < cellCount; colIdx++) {
                final Cell cell = row.getCell(colIdx);

                final Object value;
                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        final RichTextString string = cell.getRichStringCellValue();
                        if (string != null) {
                            value = string.getString();
                        } else {
                            value = null;
                        }
                    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        final CellStyle hssfCellStyle = cell.getCellStyle();
                        final short dataFormat = hssfCellStyle.getDataFormat();
                        final String dataFormatString = hssfCellStyle.getDataFormatString();
                        if (isDateFormat(dataFormat, dataFormatString)) {
                            value = cell.getDateCellValue();
                        } else {
                            value = cell.getNumericCellValue();
                        }
                    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        value = cell.getBooleanCellValue();
                    } else {
                        value = cell.getStringCellValue();
                    }
                } else {
                    value = null;
                }

                if (value != null && "".equals(value) == false) {
                    while (colIdx >= tableModel.getColumnCount()) {
                        tableModel.addColumn(Messages.getString("TableDataSourceEditor.Column",
                                String.valueOf(tableModel.getColumnCount())), Object.class);
                    }
                }

                rowData[colIdx] = value;
            }

            if (Thread.currentThread().isInterrupted()) {
                return;
            }

            tableModel.addRow(rowData);
        }

        final int colCount = tableModel.getColumnCount();
        final int rowCount = tableModel.getRowCount();
        for (int col = 0; col < colCount; col++) {
            Class type = null;
            for (int row = 0; row < rowCount; row += 1) {
                final Object value = tableModel.getValueAt(row, col);
                if (value == null) {
                    continue;
                }
                if (type == null) {
                    type = value.getClass();
                } else if (type != Object.class) {
                    if (type.isInstance(value) == false) {
                        type = Object.class;
                    }
                }
            }

            if (Thread.currentThread().isInterrupted()) {
                return;
            }

            if (type != null) {
                tableModel.setColumnType(col, type);
            }
        }

        parent.importComplete(tableModel);
    } catch (Exception e) {
        parent.importFailed(e);
        logger.error("Failed to import spreadsheet", e); // NON-NLS
    }
}

From source file:org.seasar.fisshplate.util.FPPoiUtil.java

License:Apache License

/**
 *????????/*from   w w w . j  a v  a2 s .c o  m*/
 *????????null???
 * @param hssfCell
 * @return ?
 */
public static String getStringValue(Cell hssfCell) {
    if (!isStringCell(hssfCell)) {
        return null;
    }
    RichTextString richVal = hssfCell.getRichStringCellValue();
    if (richVal == null) {
        return null;
    }
    return richVal.getString();
}

From source file:org.specrunner.source.excel.SourceFactoryExcel.java

License:Open Source License

/**
 * Add information from cell comments.//from  w  w  w .j a va 2 s.c  o  m
 * 
 * @param table
 *            The table element.
 * @param caption
 *            The table caption.
 * @param row
 *            The row element.
 * @param item
 *            The header element.
 * @param cell
 *            The cell to read comments from.
 * @param p
 *            The pair, if exist.
 */
private void addAttributes(Element table, Element caption, Element row, Element item, Cell cell, Dimension p) {
    Comment c = cell.getCellComment();
    if (c != null) {
        RichTextString rts = c.getString();
        if (rts != null) {
            String text = rts.getString();
            StringTokenizer st = new StringTokenizer(text, "\n");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                int pos = token.indexOf('=');
                if (pos > 0) {
                    String name = token.substring(0, pos);
                    String value = token.substring(pos + 1).replace("\"", "");
                    if (name.startsWith(TABLE_ATTRIBUTE)) {
                        table.addAttribute(
                                new Attribute(name.substring(TABLE_ATTRIBUTE.length(), name.length()), value));
                    } else if (name.startsWith(CAPTION_ATTRIBUTE)) {
                        caption.addAttribute(new Attribute(
                                name.substring(CAPTION_ATTRIBUTE.length(), name.length()), value));
                    } else if (name.startsWith(ROW_ATTRIBUTE)) {
                        row.addAttribute(
                                new Attribute(name.substring(ROW_ATTRIBUTE.length(), name.length()), value));
                    } else {
                        item.addAttribute(new Attribute(name, value));
                    }
                }
            }
        }
    }
    if (p != null) {
        if (p.rows > 1) {
            item.addAttribute(new Attribute("rowspan", String.valueOf(p.rows)));
        }
        if (p.cols > 1) {
            item.addAttribute(new Attribute("colspan", String.valueOf(p.cols)));
        }
    }
}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

public Topic getCommentTopic(Cell cell, TopicMap tm) throws TopicMapException {
    Comment comment = cell.getCellComment();
    if (comment != null) {
        RichTextString rts = comment.getString();
        String str = rts.getString();
        String basename = str.replace('\n', ' ');
        basename = basename.replace('\r', ' ');
        basename = basename.replace('\t', ' ');
        Topic topic = getOrCreateTopic(tm, EXCEL_COMMENT_SI_PREFIX + "/" + urlEncode(basename), basename);
        topic.setData(getCommentTypeTopic(tm), tm.getTopic(XTMPSI.getLang(DEFAULT_LANG)), str);
        topic.addType(getCommentTypeTopic(tm));
        return topic;
    }//from www . j  av  a  2 s .  c  o  m
    return null;
}