Example usage for org.apache.poi.ss.usermodel Sheet getLastRowNum

List of usage examples for org.apache.poi.ss.usermodel Sheet getLastRowNum

Introduction

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

Prototype

int getLastRowNum();

Source Link

Document

Gets the last row on the sheet Note: rows which had content before and were set to empty later might still be counted as rows by Excel and Apache POI, so the result of this method will include such rows and thus the returned value might be higher than expected!

Usage

From source file:org.apache.any23.plugin.officescraper.ExcelExtractor.java

License:Apache License

private void writeSheetMetadata(URI sheetURI, Sheet sheet, ExtractionResult er) {
    final String sheetName = sheet.getSheetName();
    final int firstRowNum = sheet.getFirstRowNum();
    final int lastRowNum = sheet.getLastRowNum();
    er.writeTriple(sheetURI, excel.sheetName, RDFUtils.literal(sheetName));
    er.writeTriple(sheetURI, excel.firstRow, RDFUtils.literal(firstRowNum));
    er.writeTriple(sheetURI, excel.lastRow, RDFUtils.literal(lastRowNum));
}

From source file:org.apache.metamodel.excel.ExcelUpdateCallback.java

License:Apache License

private int getLastRowNum(Sheet sheet) {
    final int lastRowNum = sheet.getLastRowNum();
    if (lastRowNum == 0 && sheet instanceof SXSSFSheet) {
        // streaming sheets have bad behaviour in this scenario - since no
        // rows are in cache, it will return 0!
        DataSet ds = _dataContext.query().from(sheet.getSheetName()).selectCount().execute();
        ds.next();/*from  w w  w .  j  a v  a  2s.  c  o  m*/
        final Number count = (Number) ds.getRow().getValue(0);
        final int columnNameLineNumber = _configuration.getColumnNameLineNumber();
        int oneBasedResult = count.intValue()
                + (columnNameLineNumber == ExcelConfiguration.NO_COLUMN_NAME_LINE ? 0 : columnNameLineNumber);
        return oneBasedResult - 1;
    }
    return lastRowNum;
}

From source file:org.apache.metamodel.excel.ZeroBasedRowIteratorTest.java

License:Apache License

public void testHasNext() throws Exception {
    Workbook workbook = WorkbookFactory//from   w w  w  .  j a  v a 2s .  c om
            .create(new FileInputStream("src/test/resources/xls_single_cell_sheet.xls"));
    Sheet sheet = workbook.getSheetAt(0);

    // POI's row numbers are 0-based also - the last cell in the sheet is
    // actually A6.
    assertEquals(5, sheet.getLastRowNum());

    ZeroBasedRowIterator it = new ZeroBasedRowIterator(sheet);

    assertTrue(it.hasNext());
    assertNull(it.next());

    assertTrue(it.hasNext());
    assertNull(it.next());

    assertTrue(it.hasNext());
    assertNull(it.next());

    assertTrue(it.hasNext());
    assertNull(it.next());

    assertTrue(it.hasNext());
    assertNull(it.next());

    assertTrue(it.hasNext());
    assertNotNull(it.next());

    assertFalse(it.hasNext());
}

From source file:org.bbreak.excella.core.SheetParser.java

License:Open Source License

/**
 * ??// w  w w .jav  a2s.co m
 * 
 * @param sheet ?
 * @param data BookController?parseBook(), parseSheet()?
 *              SheetParser?parseSheet?????
 *              TagParser??????
 * @return ??
 * @throws ParseException ??????Throw?
 */
public SheetData parseSheet(Sheet sheet, Object data) throws ParseException {
    // ??
    String sheetName = PoiUtil.getSheetName(sheet);
    SheetData sheetData = new SheetData(sheetName);

    int firstRowNum = sheet.getFirstRowNum();

    // ?
    for (int rowCnt = firstRowNum; rowCnt <= sheet.getLastRowNum(); rowCnt++) {
        // ??
        Row row = sheet.getRow(rowCnt);
        if (row == null) {
            continue;
        }
        if (row != null) {
            for (int columnIdx = 0; columnIdx < row.getLastCellNum(); columnIdx++) {
                // ??
                Cell cell = row.getCell(columnIdx);
                if (cell == null) {
                    continue;
                }
                if (parseCell(sheet, data, sheetData, cell, row, columnIdx)) {
                    // ?
                    return sheetData;
                }
            }
        }
    }

    return sheetData;
}

From source file:org.bbreak.excella.core.tag.excel2java.ArraysParser.java

License:Open Source License

/**
 * ?//  w  w  w  . ja  va  2 s  .c o m
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 *              SheetParser?parseSheet?????<BR>
 *              TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<Object[]> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    List<Object[]> resultList = new ArrayList<Object[]>();

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int tagColIdx = tagCell.getColumnIndex();
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();
    // 
    int valueColumnFromIdx;
    // 
    int valueColumnToIdx = 0;

    // 
    boolean valueColumnToFlag = false;

    try {

        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

        // ?
        valueColumnFromIdx = TagUtil.adjustValue(tagColIdx, paramDef, PARAM_DATA_CLOMUN_FROM,
                DEFAULT_VALUE_COLUMN_FROM_ADJUST);
        if (valueColumnFromIdx < 0 || valueColumnFromIdx > PoiUtil.getLastColNum(sheet)) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_CLOMUN_FROM);
        }

        // 
        valueColumnToFlag = paramDef.containsKey(PARAM_DATA_CLOMUN_TO);
        if (valueColumnToFlag) {
            // ?
            valueColumnToIdx = tagColIdx + Integer.valueOf(paramDef.get(PARAM_DATA_CLOMUN_TO));
            if (valueColumnToIdx < 0 || valueColumnToIdx > PoiUtil.getLastColNum(sheet)) {
                throw new ParseException(tagCell, "?" + PARAM_DATA_CLOMUN_TO);
            }

            // ???
            if (valueColumnFromIdx > valueColumnToIdx) {
                throw new ParseException(tagCell,
                        "?" + PARAM_DATA_CLOMUN_FROM + "," + PARAM_DATA_CLOMUN_TO);
            }
        }
    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ??
    for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {

        List<Object> objList = new ArrayList<Object>();

        Row row = sheet.getRow(rowCnt);
        if (row == null) {
            // ?null??
            continue;
        }
        if (!valueColumnToFlag) {
            // ?????
            // ????
            valueColumnToIdx = row.getLastCellNum() - 1;
        }
        for (int cellCnt = valueColumnFromIdx; cellCnt <= valueColumnToIdx; cellCnt++) {

            Cell cell = row.getCell(cellCnt);
            Object cellValue = PoiUtil.getCellValue(cell);
            objList.add(cellValue);
        }

        resultList.add(objList.toArray());
    }
    return resultList;
}

From source file:org.bbreak.excella.core.tag.excel2java.ListParser.java

License:Open Source License

/**
 * ?/*from   www .  java2  s  . c o m*/
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 *              SheetParser?parseSheet?????<BR>
 *              TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<?> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    // ??
    List<Object> results = new ArrayList<Object>();

    try {
        // 
        int tagRowIdx = tagCell.getRowIndex();
        int valueRowFromIdx;
        int valueRowToIdx = sheet.getLastRowNum();
        int valueColIdx = tagCell.getColumnIndex();

        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

        // Value??
        valueColIdx = TagUtil.adjustValue(valueColIdx, paramDef, PARAM_VALUE_COLUMN,
                DEFAULT_VALUE_COLUMN_ADJUST);
        if (valueColIdx > PoiUtil.getLastColNum(sheet) || valueColIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_VALUE_COLUMN);
        }

        // ?
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row row = sheet.getRow(rowCnt);
            if (row != null) {
                Object value = PoiUtil.getCellValue(row.getCell(valueColIdx));
                results.add(value);
            }
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    return results;
}

From source file:org.bbreak.excella.core.tag.excel2java.MapParser.java

License:Open Source License

/**
 * ?/*w w w . j  av  a 2s  .  co  m*/
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 *              SheetParser?parseSheet?????<BR>
 *              TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public Map<?, ?> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int tagColIdx = tagCell.getColumnIndex();
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();

    // 
    int keyColIdx = 0;
    // 
    int valueColIdx = 0;

    // 
    boolean keyTagFlag;
    // 
    boolean valueTagFlag;

    // 
    String defKey = null;
    // 
    String defValue = null;

    // 
    boolean keyCellTagFlag;
    // 
    boolean valueCellTagFlag;

    // 
    int keyRowIdx = 0;
    // 
    int valueRowIdx = 0;

    try {
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        checkParam(paramDef, tagCell);

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

        keyTagFlag = paramDef.containsKey(PARAM_KEY);
        keyCellTagFlag = paramDef.containsKey(PARAM_KEY_CELL);

        if (keyTagFlag) {
            // ???
            defKey = paramDef.get(PARAM_KEY);

        } else if (keyCellTagFlag) {
            // ???
            String value = paramDef.get(PARAM_KEY_CELL);

            // ??
            keyRowIdx = tagRowIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_FIRST_INDEX]);
            if (keyRowIdx < 0 || keyRowIdx > sheet.getLastRowNum()) {
                throw new ParseException(tagCell, "?" + PARAM_KEY_CELL);
            }
            // ??
            keyColIdx = tagColIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_LAST_INDEX]);
            if (keyColIdx > PoiUtil.getLastColNum(sheet) || keyColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_KEY_CELL);
            }

        } else {
            // ????
            // ??
            keyColIdx = TagUtil.adjustValue(tagColIdx, paramDef, PARAM_KEY_COLUMN, DEFAULT_KEY_COLUMN_ADJUST);
            if (keyColIdx > PoiUtil.getLastColNum(sheet) || keyColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_KEY_COLUMN);
            }
        }

        valueTagFlag = paramDef.containsKey(PARAM_VALUE);
        valueCellTagFlag = paramDef.containsKey(PARAM_VALUE_CELL);

        if (valueTagFlag) {
            // ???
            defValue = paramDef.get(PARAM_VALUE);

        } else if (valueCellTagFlag) {
            // ???
            String value = paramDef.get(PARAM_VALUE_CELL);

            // ??
            valueRowIdx = tagRowIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_FIRST_INDEX]);
            if (valueRowIdx < 0 || valueRowIdx > sheet.getLastRowNum()) {
                throw new ParseException(tagCell, "?" + PARAM_VALUE_CELL);
            }

            // ??
            valueColIdx = tagColIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_LAST_INDEX]);
            if (valueColIdx > PoiUtil.getLastColNum(sheet) || valueColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_VALUE_CELL);
            }

        } else {
            // ????
            // ??
            valueColIdx = TagUtil.adjustValue(tagColIdx, paramDef, PARAM_VALUE_COLUMN,
                    DEFAULT_VALUE_COLUMN_ADJUST);
            if (valueColIdx > PoiUtil.getLastColNum(sheet) || valueColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_VALUE_COLUMN);
            }
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ?
    Map<Object, Object> results = new LinkedHashMap<Object, Object>();
    Row keyRow = null;
    Row valueRow = null;
    for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {

        Object key;
        Object value;

        if (keyTagFlag) {
            // ???
            key = defKey;

        } else {
            // ????

            // ??
            if (keyCellTagFlag) {
                // ???
                keyRow = sheet.getRow(keyRowIdx);
            } else {
                // ????
                keyRow = sheet.getRow(rowCnt);
            }

            if (keyRow == null) {
                continue;
            }
            key = PoiUtil.getCellValue(keyRow.getCell(keyColIdx));
        }

        if (valueTagFlag) {
            // ???
            value = defValue;

        } else {
            // ????

            // ??
            if (valueCellTagFlag) {
                // ???
                valueRow = sheet.getRow(valueRowIdx);
            } else {
                // ????
                valueRow = sheet.getRow(rowCnt);
            }
            if (valueRow != null) {
                value = PoiUtil.getCellValue(valueRow.getCell(valueColIdx));
            } else {
                value = null;
            }
        }
        results.put(key, value);
    }
    return results;
}

From source file:org.bbreak.excella.core.tag.excel2java.MapsParser.java

License:Open Source License

/**
 * ?/*from  w  ww  .j a  va  2 s .  com*/
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 * SheetParser?parseSheet?????<BR>
 * TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<Map<?, ?>> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    List<Map<?, ?>> resultList = new ArrayList<Map<?, ?>>();

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int keyRowIdx;
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();

    try {
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        keyRowIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_KEY_ROW, DEFAULT_KEY_ROW_ADJUST);
        if (keyRowIdx < 0 || keyRowIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_KEY_ROW);
        }

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ??
    List<Integer> targetColNums = new ArrayList<Integer>();
    Row keyRow = sheet.getRow(keyRowIdx);
    if (keyRow == null) {
        // ?null??
        return resultList;
    }
    int firstCellNum = keyRow.getFirstCellNum();
    int lastCellNum = keyRow.getLastCellNum();
    for (int cellCnt = firstCellNum; cellCnt < lastCellNum; cellCnt++) {
        Cell cell = keyRow.getCell(cellCnt);
        Object cellValue = PoiUtil.getCellValue(cell);
        if (cellValue instanceof String) {
            String keyName = (String) cellValue;
            if (keyName.startsWith(BookController.COMMENT_PREFIX)) {
                continue;
            }
        }
        if (cellValue != null) {
            targetColNums.add(cellCnt);
        }
    }

    if (targetColNums.size() > 0) {
        // ????

        // ??
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row dataRow = sheet.getRow(rowCnt);
            if (dataRow == null) {
                continue;
            }
            Map<Object, Object> map = new LinkedHashMap<Object, Object>();
            for (Integer colCnt : targetColNums) {
                Cell keyCell = keyRow.getCell(colCnt);
                Cell valueCell = dataRow.getCell(colCnt);

                Object key = PoiUtil.getCellValue(keyCell);
                Object value = PoiUtil.getCellValue(valueCell);

                map.put(key, value);
            }
            resultList.add(map);
        }
    }

    return resultList;
}

From source file:org.bbreak.excella.core.tag.excel2java.ObjectsParser.java

License:Open Source License

/**
 * ?//from   www. j av  a 2  s. co m
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 *              SheetParser?parseSheet?????<BR>
 *              TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<Object> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    List<Object> resultList = new ArrayList<Object>();
    Class<?> clazz = null;

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int propertyRowIdx;
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();

    try {
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        clazz = Class.forName(paramDef.get(PARAM_CLASS));

        // ?
        propertyRowIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_PROPERTY_ROW,
                DEFAULT_PROPERTY_ROW_ADJUST);
        if (propertyRowIdx < 0 || propertyRowIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_PROPERTY_ROW);
        }

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ???
    Map<Integer, Class<?>> propertyClassMap = new HashMap<Integer, Class<?>>();
    // ????
    Map<Integer, String> propertyNameMap = new HashMap<Integer, String>();
    // ??
    Map<String, List<ObjectsPropertyParser>> customPropertyParserMap = new HashMap<String, List<ObjectsPropertyParser>>();

    // ??
    List<Integer> targetColNums = new ArrayList<Integer>();
    Row propertyRow = sheet.getRow(propertyRowIdx);
    if (propertyRow == null) {
        // ?null??
        return resultList;
    }
    int firstCellNum = propertyRow.getFirstCellNum();
    int lastCellNum = propertyRow.getLastCellNum();
    for (int cellCnt = firstCellNum; cellCnt < lastCellNum; cellCnt++) {
        Cell cell = propertyRow.getCell(cellCnt);
        if (cell == null) {
            continue;
        }
        try {
            String propertyName = cell.getStringCellValue();
            if (propertyName.startsWith(BookController.COMMENT_PREFIX)) {
                continue;
            }

            Object obj = clazz.newInstance();
            Class<?> propertyClass = PropertyUtils.getPropertyType(obj, propertyName);
            if (propertyClass != null) {
                propertyClassMap.put(cellCnt, propertyClass);
                propertyNameMap.put(cellCnt, propertyName);
                targetColNums.add(cellCnt);
            } else {
                // ????
                for (ObjectsPropertyParser parser : customPropertyParsers) {
                    if (parser.isParse(sheet, cell)) {
                        List<ObjectsPropertyParser> propertyParsers = customPropertyParserMap.get(propertyName);
                        if (propertyParsers == null) {
                            propertyParsers = new ArrayList<ObjectsPropertyParser>();
                        }
                        // ???????
                        if (!propertyParsers.contains(parser)) {
                            propertyParsers.add(parser);
                        }
                        customPropertyParserMap.put(propertyName, propertyParsers);

                        if (!targetColNums.contains(cellCnt)) {
                            propertyNameMap.put(cellCnt, propertyName);
                            targetColNums.add(cellCnt);
                        }
                    }
                }
            }

        } catch (Exception e) {
            throw new ParseException(cell, e);
        }
    }

    if (targetColNums.size() > 0) {
        // ????

        // ??
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row dataRow = sheet.getRow(rowCnt);
            if (dataRow == null) {
                continue;
            }
            Object obj;
            try {
                obj = clazz.newInstance();
                for (Integer colCnt : targetColNums) {
                    Cell cell = dataRow.getCell(colCnt);

                    try {
                        Class<?> propertyClass = propertyClassMap.get(colCnt);
                        String propertyName = propertyNameMap.get(colCnt);
                        // ?
                        if (customPropertyParserMap.containsKey(propertyName)) {
                            List<ObjectsPropertyParser> propertyParsers = customPropertyParserMap
                                    .get(propertyName);
                            Map<String, String> params = TagUtil.getParams(propertyName);
                            Object cellValue = PoiUtil.getCellValue(cell);

                            // ??
                            for (ObjectsPropertyParser propertyParser : propertyParsers) {
                                propertyParser.parse(obj, cellValue, TagUtil.getTag(propertyName), params);
                            }
                        } else {
                            Object value = null;
                            if (cell != null) {
                                value = PoiUtil.getCellValue(cell, propertyClass);
                            }
                            PropertyUtils.setProperty(obj, propertyName, value);
                        }
                    } catch (Exception e) {
                        throw new ParseException(cell, e);
                    }
                }
            } catch (Exception e) {
                if (e instanceof ParseException) {
                    throw (ParseException) e;
                } else {
                    throw new ParseException(tagCell, e);
                }
            }
            resultList.add(obj);
        }
    }
    return resultList;
}

From source file:org.bbreak.excella.core.util.PoiUtil.java

License:Open Source License

/**
 * ??????/*from  w ww  .ja v  a 2s .co m*/
 * 
 * @param sheet 
 * @param firstColumnIndex 
 * @param lastColmunIndex 
 * @return ?
 */
public static int getLastRowNum(Sheet sheet, int firstColumnIndex, int lastColmunIndex) {
    // ??
    int sheetLastRowNum = sheet.getLastRowNum();

    int rangeLastRowNum = -1;
    // ???
    for (int i = sheetLastRowNum; 0 <= i; i--) {
        Row row = sheet.getRow(i);
        if (row == null) {
            continue;
        }
        Iterator<Cell> rowIterator = row.iterator();
        while (rowIterator.hasNext()) {
            Cell cell = rowIterator.next();
            if (cell != null) {
                if (firstColumnIndex <= cell.getColumnIndex() && cell.getColumnIndex() <= lastColmunIndex) {
                    rangeLastRowNum = i;
                    break;
                }
            }
        }
        if (rangeLastRowNum != -1) {
            break;
        }
    }
    return rangeLastRowNum;
}