Example usage for org.apache.poi.ss.usermodel Cell getStringCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getStringCellValue

Introduction

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

Prototype

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:com.dituiba.excel.ImportTableService.java

License:Apache License

/**
 * ExcelTableBean//from  ww w  . j  av a  2  s. c  o m
 */
public void doImport() {
    int rowNum = sheet.getLastRowNum() + 1;
    int columnNum = 0;
    for (int i = 0; i < rowNum; i++) {
        if (sheet.getRow(i) != null) {
            int last = sheet.getRow(i).getLastCellNum();
            columnNum = last > columnNum ? last : columnNum;
        }
    }
    tableBean = new TableBean(rowNum, columnNum);
    Collection<CellBean> cellBeans = new ArrayList<CellBean>();
    for (int r = startRow; r < rowNum; r++) {
        Row row = sheet.getRow(r);
        if (row != null) {
            for (int c = 0; c < row.getLastCellNum(); c++) {
                Cell cell = row.getCell(c);
                if (cell != null) {
                    String cellValue = null;
                    if (cellHandlerMap.containsKey(c)) {
                        cellValue = cellHandlerMap.get(c).readCell(cell) + "";
                    } else {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        Integer type = forceCellType.get(c);
                        if (type != null) {
                            cell.setCellType(type);
                        }
                        if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
                            cellValue = cell.getBooleanCellValue() + "";
                        } else if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) {
                            try {
                                cellValue = String.valueOf(cell.getNumericCellValue());
                            } catch (IllegalStateException e) {
                                cellValue = String.valueOf(cell.getRichStringCellValue()).trim();
                            }
                        } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                            if (DateUtil.isCellDateFormatted(cell)) {
                                Date date2 = cell.getDateCellValue();
                                SimpleDateFormat dff = new SimpleDateFormat(dateFormat);
                                cellValue = dff.format(date2); //
                            } else {
                                cellValue = String.valueOf(cell.getNumericCellValue());
                            }
                        } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
                            cellValue = cell.getStringCellValue();
                        }
                        if (cellValue != null && cellValue instanceof String) {
                            cellValue = cellValue.toString().trim();
                        }
                    }
                    CellBean cellBean = new CellBean(cellValue, r, c);
                    cellBean.setCell(cell);
                    cellBeans.add(cellBean);
                }
            }
        }
    }
    tableBean.setCellBeans(cellBeans);
}

From source file:com.dotosoft.dotoquiz.tools.OldApp.java

License:Apache License

private boolean showColumnHeader(Object data, String sheetName) {
    if (APPLICATION_TYPE.SHOW_COLUMN_HEADER.toString().equals(settings.getApplicationType())) {
        log.info("Show column header for \"" + sheetName + "\"");
        if (DATA_TYPE.EXCEL.toString().equals(settings.getDataType())) {
            Row rowData = (Row) data;/*from w w w .j a  v a 2s .  c  om*/
            Iterator<Cell> cellIterator = rowData.iterator();
            int columnCount = rowData.getRowNum();
            for (int i = 0; i < columnCount; i++) {
                Cell cell = rowData.getCell(i);
                log.info("\tColumn(" + i + "): " + cell.getStringCellValue());
            }
        } else if (DATA_TYPE.GOOGLESHEET.toString().equals(settings.getDataType())) {
            ListEntry listEntry = (ListEntry) data;
            int index = 0;
            for (String tag : listEntry.getCustomElements().getTags()) {
                log.info("\tColumn" + (index++) + ": " + tag);
            }
        }
        return true;
    }

    return false;
}

From source file:com.dotosoft.dotoquiz.tools.util.DotoQuizStructure.java

License:Apache License

private static String readCellAsString(Cell cell, String defaultValue) {
    String result = defaultValue;
    if (cell == null)
        return result;

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        result = String.valueOf(cell.getNumericCellValue()).replace(".0", "");
        break;/*from www .j a v  a2 s .  co m*/
    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;
    }

    return result;
}

From source file:com.ebay.xcelite.reader.BeanSheetReader.java

License:Apache License

private void buildHeader() {
    header = Sets.newLinkedHashSet();/*from   w  ww  . j  a va 2  s. c  o m*/
    rowIterator = sheet.getNativeSheet().rowIterator();
    Row row = rowIterator.next();
    if (row == null) {
        throw new XceliteException("First row in sheet is empty. First row must contain header");
    }
    Iterator<Cell> itr = row.cellIterator();
    while (itr.hasNext()) {
        Cell cell = itr.next();
        header.add(cell.getStringCellValue());
    }
}

From source file:com.ebay.xcelite.reader.SheetReaderAbs.java

License:Apache License

protected Object readValueFromCell(Cell cell) {
    if (cell == null)
        return null;
    Object cellValue = null;/*  w  ww . j a v  a2s.co  m*/
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        cellValue = cell.getNumericCellValue();
        break;
    default:
        cellValue = cell.getStringCellValue();
    }
    return cellValue;
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private void importDataToWorkbook(HSSFWorkbook xlsBook, Workbook workbook) {
    if (workbook.getObservations() != null) {
        HSSFSheet observationSheet = xlsBook.getSheetAt(1);
        int xlsRowIndex = 1; //row 0 is the header row
        for (MeasurementRow wRow : workbook.getObservations()) {
            HSSFRow xlsRow = observationSheet.getRow(xlsRowIndex);
            for (MeasurementData wData : wRow.getDataList()) {
                String label = wData.getLabel();
                int xlsColIndex = findColumn(observationSheet, label);
                Cell cell = xlsRow.getCell(xlsColIndex);
                String xlsValue = "";

                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        Double doubleVal = Double.valueOf(cell.getNumericCellValue());
                        Integer intVal = Integer.valueOf(doubleVal.intValue());
                        if (Double.parseDouble(intVal.toString()) == doubleVal.doubleValue()) {
                            xlsValue = intVal.toString();
                        } else {
                            xlsValue = doubleVal.toString();
                        }//from ww w  . ja v a 2  s.  co  m

                    } else
                        xlsValue = cell.getStringCellValue();
                }
                wData.setValue(xlsValue);
            }
            xlsRowIndex++;
        }
    }
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private Integer getExcelValueInt(HSSFRow row, int columnIndex) {
    Cell cell = row.getCell(columnIndex);
    String xlsStr = "";
    if (cell.getCellType() == Cell.CELL_TYPE_STRING)
        xlsStr = cell.getStringCellValue();
    else//from  www.ja va2s  .  com
        xlsStr = String.valueOf((int) cell.getNumericCellValue());
    if (NumberUtils.isNumber(xlsStr)) {
        return Integer.valueOf(xlsStr);
    }
    return null;
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ImportGermplasmFileServiceImpl.java

License:Open Source License

/**
 * Gets the cell string value.//from  ww w  . j  a v  a 2  s. c  o m
 *
 * @param sheetNumber the sheet number
 * @param rowNumber the row number
 * @param columnNumber the column number
 * @param followThisPosition the follow this position
 * @return the cell string value
 */
private String getCellStringValue(Integer sheetNumber, Integer rowNumber, Integer columnNumber,
        Boolean followThisPosition) {
    if (followThisPosition) {
        currentSheet = sheetNumber;
        currentRow = rowNumber;
        currentColumn = columnNumber;
    }

    try {
        Sheet sheet = wb.getSheetAt(sheetNumber);
        Row row = sheet.getRow(rowNumber);
        Cell cell = row.getCell(columnNumber);
        return cell.getStringCellValue();
    } catch (IllegalStateException e) {
        Sheet sheet = wb.getSheetAt(sheetNumber);
        Row row = sheet.getRow(rowNumber);
        Cell cell = row.getCell(columnNumber);
        return String.valueOf(Integer.valueOf((int) cell.getNumericCellValue()));
    } catch (NullPointerException e) {
        return "";
    }
}

From source file:com.eleven0eight.xls2json.App.java

License:Open Source License

public String convertXlsToJson(FileInputStream fis) throws Exception {

    Workbook workbook = WorkbookFactory.create(fis);
    Sheet sheet = workbook.getSheetAt(0);
    JSONObject json = new JSONObject();
    JSONArray items = new JSONArray();
    ArrayList cols = new ArrayList();

    for (int i = 0; i <= sheet.getLastRowNum(); i++) {
        Row row = sheet.getRow(i);/*from   w  ww  .  j av a 2s.  c  o m*/
        JSONObject item = new JSONObject();

        for (short colIndex = row.getFirstCellNum(); colIndex <= row.getLastCellNum(); colIndex++) {
            Cell cell = row.getCell(colIndex);
            if (cell == null) {
                continue;
            }
            if (i == 0) { // header
                cols.add(colIndex, cell.getStringCellValue());
            } else {
                item.put((String) cols.get(colIndex), cell.getStringCellValue());
            }
        }
        if (item.length() > 0) {
            items.put(item);
        }
    }
    json.put("items", items);
    return json.toString();

}

From source file:com.envisioncn.it.super_sonic.showcase.evaluation.utils.ExcelUtils.java

License:Open Source License

/**
 * Excel/*from w  ww . j a  v  a  2s  .  c  om*/
 * 
 * @param hssfCell
 *            Excel??
 * @return Excel??
 */
@SuppressWarnings("static-access")
public static String getValue(Cell Cell) {

    // 
    if (Cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {

        return String.valueOf(Cell.getBooleanCellValue());
        // 
    } else if (Cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

        return String.valueOf(new BigDecimal(Cell.getNumericCellValue()));
        // 
    } else {

        return String.valueOf(Cell.getStringCellValue());
    }
}