Example usage for org.apache.poi.hssf.usermodel HSSFRow getCell

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow getCell

Introduction

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

Prototype

@Override
public HSSFCell getCell(int cellnum, MissingCellPolicy policy) 

Source Link

Document

Get the hssfcell representing a given column (logical cell) 0-based.

Usage

From source file:com.asakusafw.testtools.excel.ExcelUtils.java

License:Apache License

/**
 * ??????/*from w  ww .  j av a 2  s .  c o  m*/
 * @param sheet ?????
 * @return ?
 */
private List<Writable> createDatalList(HSSFSheet sheet) {
    List<Writable> list = new ArrayList<Writable>();

    Class<?> modelClass = getModelClass();

    int rownum = 0; // 0??????
                    // 1???
    for (;;) {
        rownum++;
        HSSFRow row = sheet.getRow(rownum);
        if (isEmpty(row)) {
            break;
        }
        Writable model;
        try {
            model = (Writable) modelClass.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        for (int col = 0; col < columnInfos.size(); col++) {
            HSSFCell cell = row.getCell(col, Row.CREATE_NULL_AS_BLANK);
            MySqlDataType type = columnInfos.get(col).getDataType();
            ValueOption<?> vo;
            switch (type) {
            case CHAR:
            case VARCHAR:
                vo = getStringOption(cell);
                break;
            case DATE:
                vo = getDateOption(cell);
                break;
            case DATETIME:
            case TIMESTAMP:
                vo = getDateTimeOption(cell);
                break;
            case DECIMAL:
                vo = getDecimalOption(cell);
                break;
            case TINY_INT:
                vo = getByteOption(cell);
                break;
            case SMALL_INT:
                vo = getShortOption(cell);
                break;
            case INT:
                vo = getIntOption(cell);
                break;
            case LONG:
                vo = getLongOption(cell);
                break;
            default:
                throw new RuntimeException("Unsupported data type: " + type);
            }
            try {
                String setterName = columnInfos.get(col).getSetterName();
                Method setter = model.getClass().getMethod(setterName, vo.getClass());
                setter.invoke(model, vo);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        list.add(model);
    }
    return list;
}

From source file:com.codequicker.quick.templates.source.adapters.ExcelSourceAdapter.java

License:Apache License

private void readOleBasedData(BufferedInputStream bufferedStream, Map<String, List<Map<String, String>>> data) {

    try {/*from w  ww .  j  a va  2  s  .  co  m*/
        HSSFWorkbook workbook = new HSSFWorkbook(bufferedStream);

        int sheetCount = workbook.getNumberOfSheets();

        for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++) {
            HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
            Cell cell = null;

            List<Map<String, String>> sheetData = new ArrayList<Map<String, String>>();

            int lastRowNumber = sheet.getLastRowNum();

            for (int rowIndex = 0; rowIndex <= lastRowNumber; rowIndex++) {
                HSSFRow row = sheet.getRow(rowIndex);
                if (row == null) {
                    continue;
                }

                Map<String, String> columnData = new HashMap<String, String>();

                for (int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) {
                    cell = row.getCell(cellIndex, Row.CREATE_NULL_AS_BLANK);
                    columnData.put("column" + (cellIndex + 1), cell.toString());
                }

                sheetData.add(columnData);
            }

            data.put("sheet" + (sheetIndex + 1), sheetData);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:fr.univrouen.poste.services.ExcelParser.java

License:Apache License

public List<List<String>> getCells(InputStream xslFileInput) {

    List<List<String>> cellVectorHolder = new Vector<List<String>>();

    try {//from w  ww. j av a2 s  . c  o  m

        POIFSFileSystem fileSystem = new POIFSFileSystem(xslFileInput);
        HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator<Row> rowIter = sheet.rowIterator();

        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            List<String> cellStoreVector = new Vector<String>();
            // take care of blank cell !
            // @see http://stackoverflow.com/questions/4929646/how-to-get-an-excel-blank-cell-value-in-apache-poi
            int max = myRow.getLastCellNum();
            for (int i = 0; i < max; i++) {
                HSSFCell myCell = (HSSFCell) myRow.getCell(i, Row.CREATE_NULL_AS_BLANK);
                if (Cell.CELL_TYPE_STRING == myCell.getCellType())
                    cellStoreVector.add(myCell.getStringCellValue());
                else if ((Cell.CELL_TYPE_NUMERIC == myCell.getCellType()))
                    cellStoreVector.add(Long.toString(new Double(myCell.getNumericCellValue()).longValue()));
                else if ((Cell.CELL_TYPE_BLANK == myCell.getCellType()))
                    cellStoreVector.add("");
                else {
                    logger.debug("This cell is not numeric or string ... : " + myCell + " \n ... cellType : "
                            + myCell.getCellType());
                    cellStoreVector.add("");
                }
            }
            cellVectorHolder.add(cellStoreVector);
        }
    } catch (Exception e) {
        logger.error("Error during parsing the XSL File", e);
        throw new RuntimeException("Error during parsing the XSL File", e);
    }

    return cellVectorHolder;
}