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

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

Introduction

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

Prototype

public String getStringCellValue() 

Source Link

Document

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

Usage

From source file:org.lamsfoundation.lams.admin.service.ImportService.java

License:Open Source License

private boolean parseBooleanCell(HSSFCell cell) {
    if (cell != null) {
        String value;//from w w  w .  j a  va2 s  . c  o  m
        try {
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            if (cell.getStringCellValue() != null) {
                if (cell.getStringCellValue().trim().length() != 0) {
                    emptyRow = false;
                }
            } else {
                return false;
            }
            value = cell.getStringCellValue().trim();
        } catch (Exception e) {
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            double d = cell.getNumericCellValue();
            emptyRow = false;
            value = new Long(new Double(d).longValue()).toString();
        }
        if (StringUtils.equals(value, "1") || StringUtils.equalsIgnoreCase(value, "true")) {
            return true;
        }
    }
    return false;
}

From source file:org.lamsfoundation.lams.admin.service.ImportService.java

License:Open Source License

private String parseStringCell(HSSFCell cell) {
    if (cell != null) {
        try {//from ww w  . j  a  v  a2s . c  om
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            if (cell.getStringCellValue() != null) {
                if (cell.getStringCellValue().trim().length() != 0) {
                    emptyRow = false;
                }
            } else {
                return null;
            }
            // log.debug("string cell value: '"+cell.getStringCellValue().trim()+"'");
            return cell.getStringCellValue().trim();
        } catch (Exception e) {
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            double d = cell.getNumericCellValue();
            emptyRow = false;
            // log.debug("numeric cell value: '"+d+"'");
            return (new Long(new Double(d).longValue()).toString());
        }
    }
    return null;
}

From source file:org.lamsfoundation.lams.admin.service.ImportService.java

License:Open Source License

private List<String> parseRolesCell(HSSFCell cell) {
    String roleDescription = "";
    if (cell != null) {
        try {/* w w w.  j av a2  s. c o m*/
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            if (cell.getStringCellValue() != null || cell.getStringCellValue().trim().length() != 0) {
                emptyRow = false;
            } else {
                log.debug("Couldn't find any roles in spreadsheet column index " + ROLES);
                return null;
            }
            roleDescription = cell.getStringCellValue().trim();
        } catch (Exception e) {
            log.error("Caught exception when reading roles in spreadsheet: " + e.getMessage());
            return null;
        }
        List<String> roles = new ArrayList<String>();
        int fromIndex = 0;
        int index = roleDescription.indexOf(SEPARATOR, fromIndex);
        while (index != -1) {
            String role = addRoleId(roleDescription, fromIndex, index);
            log.debug("Found role: " + role);
            if (role == null) {
                return null;
            } else {
                roles.add(role);
            }
            fromIndex = index + 1;
            index = roleDescription.indexOf(SEPARATOR, fromIndex);
        }
        String role = addRoleId(roleDescription, fromIndex, roleDescription.length());
        log.debug("Found last role: " + role);
        if (role == null) {
            return null;
        } else {
            roles.add(role);
        }
        return roles;
    }
    return null;
}

From source file:org.logic2j.contrib.excel.ExcelReader.java

License:Open Source License

/**
 * @param sheet/*from  w w w . j a  v a 2  s  .c om*/
 * @param rowNumber Row index
 * @param theTargetClass
 * @return Null if row is empty or only containing nulls.
 */
private <T> List<T> readRow(Sheet sheet, final int rowNumber, Class<T> theTargetClass) {
    final HSSFRow row = ((HSSFSheet) sheet).getRow(rowNumber);
    if (row == null) {
        return null;
    }
    final int nbCols = row.getPhysicalNumberOfCells();
    final ArrayList<T> values = new ArrayList<T>();
    boolean hasSomeData = false;
    for (int c = 0; c < nbCols; c++) {
        final HSSFCell cell = row.getCell(c);
        Object value = null;
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_FORMULA:
                value = cell.getCellFormula();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                value = cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            default:
                throw new PrologNonSpecificError("Excel cell at row=" + rowNumber + ", column=" + c
                        + " of type " + cell.getCellType() + " not handled, value is " + value);
            }
        }
        value = mapCellValue(value);
        if (value != null) {
            hasSomeData = true;
        }
        final T cast = TypeUtils.safeCastOrNull("casting Excel cell", value, theTargetClass);
        values.add(cast);
    }
    if (!hasSomeData) {
        return null;
    }
    return values;
}

From source file:org.metaeffekt.core.inventory.processor.reader.InventoryReader.java

License:Apache License

@Override
protected void readHeader(HSSFRow row) {
    for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
        HSSFCell cell = row.getCell(i);
        String id = cell != null ? cell.getStringCellValue() : "Column " + (i + 1);
        artifactColumnMap.put(i, id);/*from   w w  w  .  j a  va 2 s.c o m*/
    }
}

From source file:org.metaeffekt.core.inventory.processor.reader.InventoryReader.java

License:Apache License

@Override
protected void readLicenseMetaDataHeader(HSSFRow row) {
    for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
        HSSFCell cell = row.getCell(i);
        if (cell != null) {
            columnMap.put(i, cell.getStringCellValue());
        }/*  ww w. ja v  a  2  s.com*/
    }
}

From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.XL2TextConverter.java

License:Apache License

protected void appendTextFromCell(HSSFCell cell, StringBuffer sb) {
    String cellValue = null;// w ww . j a  v  a2s.co m
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        cellValue = Double.toString(cell.getNumericCellValue()).trim();
        break;
    case HSSFCell.CELL_TYPE_STRING:
        cellValue = cell.getStringCellValue().trim().replaceAll("\n", " ");
        break;
    }

    if (cellValue != null && cellValue.length() > 0) {
        sb.append(cellValue);
    }
}

From source file:org.ofbiz.tools.rest.FixOfcTools.java

/**
 * ?? //from  w  w w .  j  a va 2 s .com
 * @param cell
 * @return
 */
public static String convertCell(HSSFCell cell) {
    String cellValue = "";
    if (cell == null) {
        return cellValue;
    }
    NumberFormat formater = NumberFormat.getInstance();
    formater.setGroupingUsed(false);
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        cellValue = formater.format(cell.getNumericCellValue());
        break;
    case HSSFCell.CELL_TYPE_STRING:
        cellValue = cell.getStringCellValue();
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        cellValue = cell.getStringCellValue();
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        cellValue = Boolean.valueOf(cell.getBooleanCellValue()).toString();
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        cellValue = String.valueOf(cell.getErrorCellValue());
        break;
    default:
        cellValue = "";
    }
    return cellValue.replaceAll("\\s", "").trim();
}

From source file:org.olat.search.service.document.file.ExcelDocument.java

License:Apache License

@Override
protected String readContent(final VFSLeaf leaf) throws IOException, DocumentException {
    BufferedInputStream bis = null;
    int cellNullCounter = 0;
    int rowNullCounter = 0;
    int sheetNullCounter = 0;

    try {/*  w  w  w. j a v a2s .com*/
        bis = new BufferedInputStream(leaf.getInputStream());
        final StringBuilder content = new StringBuilder(bis.available());
        final POIFSFileSystem fs = new POIFSFileSystem(bis);
        final HSSFWorkbook workbook = new HSSFWorkbook(fs);

        for (int sheetNumber = 0; sheetNumber < workbook.getNumberOfSheets(); sheetNumber++) {
            final HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
            if (sheet != null) {
                for (int rowNumber = sheet.getFirstRowNum(); rowNumber <= sheet.getLastRowNum(); rowNumber++) {
                    final HSSFRow row = sheet.getRow(rowNumber);
                    if (row != null) {
                        for (int cellNumber = row.getFirstCellNum(); cellNumber <= row
                                .getLastCellNum(); cellNumber++) {
                            final HSSFCell cell = row.getCell(cellNumber);
                            if (cell != null) {
                                // if (cell.getCellStyle().equals(HSSFCell.CELL_TYPE_NUMERIC))
                                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                                    content.append(cell.getStringCellValue()).append(' ');
                                }
                            } else {
                                // throw new DocumentException();
                                cellNullCounter++;
                            }
                        }
                    } else {
                        rowNullCounter++;
                    }
                }
            } else {
                sheetNullCounter++;
            }
        }
        if (log.isDebug()) {
            if ((cellNullCounter > 0) || (rowNullCounter > 0) || (sheetNullCounter > 0)) {
                log.debug("Read Excel content cell=null #:" + cellNullCounter + ", row=null #:" + rowNullCounter
                        + ", sheet=null #:" + sheetNullCounter);
            }
        }
        return content.toString();
    } catch (final Exception ex) {
        throw new DocumentException("Can not read XLS Content. File=" + leaf.getName());
    } finally {
        if (bis != null) {
            bis.close();
        }

    }
}

From source file:org.opencms.search.extractors.CmsExtractorMsExcel.java

License:Open Source License

/**
 * Extracts the text from the Excel table content.<p>
 * /*from  w ww  .ja  v a  2  s . c  o  m*/
 * @param in the document input stream
 * @return the extracted text
 * @throws IOException if something goes wring
 */
protected String extractTableContent(InputStream in) throws IOException {

    HSSFWorkbook excelWb = new HSSFWorkbook(in);
    StringBuffer result = new StringBuffer(4096);

    int numberOfSheets = excelWb.getNumberOfSheets();

    for (int i = 0; i < numberOfSheets; i++) {
        HSSFSheet sheet = excelWb.getSheetAt(i);
        int numberOfRows = sheet.getPhysicalNumberOfRows();
        if (numberOfRows > 0) {

            if (CmsStringUtil.isNotEmpty(excelWb.getSheetName(i))) {
                // append sheet name to content
                if (i > 0) {
                    result.append("\n\n");
                }
                result.append(excelWb.getSheetName(i).trim());
                result.append(":\n\n");
            }

            Iterator rowIt = sheet.rowIterator();
            while (rowIt.hasNext()) {
                HSSFRow row = (HSSFRow) rowIt.next();
                if (row != null) {
                    boolean hasContent = false;
                    Iterator it = row.cellIterator();
                    while (it.hasNext()) {
                        HSSFCell cell = (HSSFCell) it.next();
                        String text = null;
                        try {
                            switch (cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_BLANK:
                            case HSSFCell.CELL_TYPE_ERROR:
                                // ignore all blank or error cells
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                text = Double.toString(cell.getNumericCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                text = Boolean.toString(cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_STRING:
                            default:
                                text = cell.getStringCellValue();
                                break;
                            }
                        } catch (Exception e) {
                            // ignore this cell
                        }
                        if ((text != null) && (text.length() != 0)) {
                            result.append(text.trim());
                            result.append(' ');
                            hasContent = true;
                        }
                    }
                    if (hasContent) {
                        // append a newline at the end of each row that has content                            
                        result.append('\n');
                    }
                }
            }
        }
    }

    return result.toString();
}