Example usage for org.apache.poi.hssf.usermodel HSSFCell getRichStringCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getRichStringCellValue

Introduction

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

Prototype

public HSSFRichTextString getRichStringCellValue() 

Source Link

Document

get the value of the cell as a string - for numeric cells we throw an exception.

Usage

From source file:org.beangle.commons.transfer.excel.ExcelItemReader.java

License:Open Source License

/**
 * ????/*w ww  .  j  a va  2  s.co  m*/
 * 
 * @param sheet a {@link org.apache.poi.hssf.usermodel.HSSFSheet} object.
 * @param rowIndex a int.
 * @return an array of {@link java.lang.String} objects.
 */
protected String[] readLine(HSSFSheet sheet, int rowIndex) {
    HSSFRow row = sheet.getRow(rowIndex);
    logger.debug("values count:{}", row.getLastCellNum());
    List<String> attrList = CollectUtils.newArrayList();
    for (int i = 0; i < row.getLastCellNum(); i++) {
        HSSFCell cell = row.getCell(i);
        if (null != cell) {
            String attr = cell.getRichStringCellValue().getString();
            if (Strings.isEmpty(attr)) {
                break;
            } else {
                attrList.add(attr.trim());
            }
        } else {
            break;
        }
    }
    String[] attrs = new String[attrList.size()];
    attrList.toArray(attrs);
    logger.debug("has attrs {}", attrs);
    return attrs;
}

From source file:org.beangle.commons.transfer.excel.ExcelItemReader.java

License:Open Source License

/**
 * <p>//from w  w w  . j  a va  2s. c  om
 * getCellValue.
 * </p>
 * 
 * @see ?cell??
 * @param cell a {@link org.apache.poi.hssf.usermodel.HSSFCell} object.
 * @return a {@link java.lang.Object} object.
 */
public static Object getCellValue(HSSFCell cell) {
    if ((cell == null))
        return null;
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_BLANK:
        return null;
    case HSSFCell.CELL_TYPE_STRING:
        return Strings.trim(cell.getRichStringCellValue().getString());
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {
            return numberFormat.format(cell.getNumericCellValue());
        }
    case HSSFCell.CELL_TYPE_BOOLEAN:
        return (cell.getBooleanCellValue()) ? Boolean.TRUE : Boolean.FALSE;
    default:
        // cannot handle HSSFCell.CELL_TYPE_ERROR,HSSFCell.CELL_TYPE_FORMULA
        return null;
    }
}

From source file:org.beangle.model.transfer.excel.ExcelItemReader.java

License:Open Source License

/**
 * ????/*ww  w .  ja  v  a 2s .c  o  m*/
 * 
 * @param sheet
 * @param rowIndex
 * @return
 */
protected String[] readLine(HSSFSheet sheet, int rowIndex) {
    HSSFRow row = sheet.getRow(rowIndex);
    logger.debug("values count:{}", row.getLastCellNum());
    List<String> attrList = CollectUtils.newArrayList();
    for (int i = 0; i < row.getLastCellNum(); i++) {
        HSSFCell cell = row.getCell(i);
        if (null != cell) {
            String attr = cell.getRichStringCellValue().getString();
            if (StringUtils.isEmpty(attr)) {
                break;
            } else {
                attrList.add(attr.trim());
            }
        } else {
            break;
        }
    }
    String[] attrs = new String[attrList.size()];
    attrList.toArray(attrs);
    logger.debug("has attrs {}", attrs);
    return attrs;
}

From source file:org.beangle.model.transfer.excel.ExcelItemReader.java

License:Open Source License

/**
 * @see ?cell??/*from  w w w .  j a v  a  2s. co m*/
 * @param cell
 * @param objClass
 * @return
 */
public static Object getCellValue(HSSFCell cell) {
    if ((cell == null))
        return null;
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_BLANK:
        return null;
    case HSSFCell.CELL_TYPE_STRING:
        return StringUtils.trim(cell.getRichStringCellValue().getString());
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {
            return numberFormat.format(cell.getNumericCellValue());
        }
    case HSSFCell.CELL_TYPE_BOOLEAN:
        return (cell.getBooleanCellValue()) ? Boolean.TRUE : Boolean.FALSE;
    default:
        // cannot handle HSSFCell.CELL_TYPE_ERROR,HSSFCell.CELL_TYPE_FORMULA
        return null;
    }
}

From source file:org.dbunit.dataset.excel.XlsTable.java

License:Open Source License

static ITableMetaData createMetaData(String tableName, HSSFRow sampleRow) {
    logger.debug("createMetaData(tableName={}, sampleRow={}) - start", tableName, sampleRow);

    List columnList = new ArrayList();
    for (int i = 0;; i++) {
        HSSFCell cell = sampleRow.getCell(i);
        if (cell == null) {
            break;
        }// ww w.j av a  2s.  c  o  m

        String columnName = cell.getRichStringCellValue().getString();
        if (columnName != null) {
            columnName = columnName.trim();
        }

        // Bugfix for issue ID 2818981 - if a cell has a formatting but no name also ignore it
        if (columnName.length() <= 0) {
            logger.debug(
                    "The column name of column # {} is empty - will skip here assuming the last column was reached",
                    String.valueOf(i));
            break;
        }

        Column column = new Column(columnName, DataType.UNKNOWN);
        columnList.add(column);
    }
    Column[] columns = (Column[]) columnList.toArray(new Column[0]);
    return new DefaultTableMetaData(tableName, columns);
}

From source file:org.encuestame.business.search.IndexerFile.java

License:Apache License

/**
 * Extract spreadsheets content./* w w w .  j a  v  a2 s.co  m*/
 * @param workBook
 * @return
 * @throws Exception
 */
public static String extractContentSpreadsheetsDocument(final HSSFWorkbook workBook) throws Exception {
    StringBuilder contents = new StringBuilder();
    for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
        HSSFSheet sheet = workBook.getSheetAt(i);
        Iterator<Row> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            // Display the row number
            log.debug(row.getRowNum());
            Iterator<Cell> cells = row.cellIterator();
            while (cells.hasNext()) {
                HSSFCell cell = (HSSFCell) cells.next();
                // Display the cell number of the current Row
                switch (cell.getCellType()) {

                case HSSFCell.CELL_TYPE_NUMERIC: {
                    log.debug(String.valueOf(cell.getNumericCellValue()));
                    contents.append(String.valueOf(cell.getNumericCellValue())).append(" ");
                    break;
                }

                case HSSFCell.CELL_TYPE_STRING: {
                    HSSFRichTextString richTextString = cell.getRichStringCellValue();
                    log.debug(richTextString.toString());
                    contents.append(richTextString.toString()).append(" ");
                    break;
                }

                case HSSFCell.CELL_TYPE_BOOLEAN: {
                    contents.append(String.valueOf(cell.getBooleanCellValue())).append(" ");
                    break;
                }
                }
            }
        }
    }
    return contents.toString();
}

From source file:org.encuestame.business.search.SearchUtils.java

License:Apache License

/**
* Create Spreadsheets Document./* ww  w .  j  a  v  a 2 s. co m*/
* @param file Spreadsheet {@link File}.
* @param Long attachmentId.
* @return {@link Document}
* @throws FileNotFoundException
*/
public static Document createSpreadsheetsDocument(final File file) throws Exception {
    InputStream is = new FileInputStream(file);
    StringBuilder contents = new StringBuilder();
    POIFSFileSystem fileSystem = new POIFSFileSystem(is);
    HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
    for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
        HSSFSheet sheet = workBook.getSheetAt(i);
        Iterator<Row> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            // Display the row number
            log.debug(row.getRowNum());
            Iterator<Cell> cells = row.cellIterator();
            while (cells.hasNext()) {
                HSSFCell cell = (HSSFCell) cells.next();
                // Display the cell number of the current Row
                switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC: {
                    log.debug(String.valueOf(cell.getNumericCellValue()));
                    contents.append(String.valueOf(cell.getNumericCellValue())).append(" ");
                    break;
                }

                case HSSFCell.CELL_TYPE_STRING: {
                    HSSFRichTextString richTextString = cell.getRichStringCellValue();
                    log.debug(richTextString.toString());
                    contents.append(richTextString.toString()).append(" ");
                    break;
                }

                case HSSFCell.CELL_TYPE_BOOLEAN: {
                    contents.append(String.valueOf(cell.getBooleanCellValue())).append(" ");
                    break;
                }
                }
            }
        }
    }
    Document doc = SearchUtils.addFields(file, contents.toString());
    return doc;
}

From source file:org.hil.children.service.impl.ChildrenManagerImpl.java

License:Open Source License

private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum,
        int destinationRowNum) {
    // Get the source / new row
    HSSFRow newRow = worksheet.getRow(destinationRowNum);
    HSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row
    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {//from  ww  w  .j av  a2s.c  o  m
        newRow = worksheet.createRow(destinationRowNum);
    }

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        HSSFCell oldCell = sourceRow.getCell(i);
        HSSFCell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;
            continue;
        }

        // Copy style from old cell and apply to new cell
        HSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());

        newCell.setCellStyle(newCellStyle);

        // If there is a cell comment, copy
        if (newCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        // If there is a cell hyperlink, copy
        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

        // Set the cell data value
        switch (oldCell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }

    // If there are are any merged regions in the source row, copy to new row
    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
        if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                    (newRow.getRowNum() + (cellRangeAddress.getFirstRow() - cellRangeAddress.getLastRow())),
                    cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }
}

From source file:org.mcisb.excel.ExcelReader.java

License:Open Source License

/**
 * /*from www.  j ava 2  s  .c o m*/
 * @param cell
 * @return Object
 */
private static Object getValue(HSSFCell cell) {
    if (cell == null) {
        return null;
    }

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN: {
        return Boolean.valueOf(cell.getBooleanCellValue());
    }
    case Cell.CELL_TYPE_NUMERIC: {
        return Double.valueOf(cell.getNumericCellValue());
    }
    case Cell.CELL_TYPE_STRING: {
        return cell.getRichStringCellValue().getString().trim();
    }
    default: {
        return null;
    }
    }
}

From source file:org.mifos.application.importexport.xls.XlsClientsImporter.java

License:Open Source License

private String getCellStringValue(final HSSFRow row, final XlsImportConstants xlsImportConstant) {
    final HSSFCell cell = row.getCell(xlsImportConstant.value(), HSSFRow.RETURN_BLANK_AS_NULL);
    if (cell != null) {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            HSSFRichTextString richText = cell.getRichStringCellValue();
            return (richText == null) ? "" : richText.getString();
        case HSSFCell.CELL_TYPE_NUMERIC:
            int intVal = (int) cell.getNumericCellValue();
            return String.valueOf(intVal);
        default://from   ww w. ja  va 2  s.  co m
            return "";
        }
    } else {
        return "";
    }
}