Example usage for org.apache.poi.ss.usermodel Row getLastCellNum

List of usage examples for org.apache.poi.ss.usermodel Row getLastCellNum

Introduction

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

Prototype

short getLastCellNum();

Source Link

Document

Gets the index of the last cell contained in this row PLUS ONE.

Usage

From source file:org.hellojavaer.poi.excel.utils.ExcelUtils.java

License:Apache License

private static InnerRow getTemplateRow(Map<Integer, InnerRow> cache, Sheet sheet,
        ExcelWriteSheetProcessor<?> sheetProcessor, int rowIndex) {
    InnerRow cachedRow = cache.get(rowIndex);
    if (cachedRow != null || cache.containsKey(rowIndex)) {
        return cachedRow;
    }//from  ww  w . j  a v a2 s  .co m
    InnerRow templateRow = null;
    if (sheetProcessor.getTemplateStartRowIndex() != null && sheetProcessor.getTemplateEndRowIndex() != null) {
        if (rowIndex <= sheetProcessor.getTemplateEndRowIndex()) {
            return null;
        }
        int tempRowIndex = (rowIndex - sheetProcessor.getTemplateEndRowIndex() - 1)
                % (sheetProcessor.getTemplateEndRowIndex() - sheetProcessor.getTemplateStartRowIndex() + 1)
                + sheetProcessor.getTemplateStartRowIndex();
        Row tempRow = sheet.getRow(tempRowIndex);
        if (tempRow != null) {
            templateRow = new InnerRow();
            templateRow.setHeight(tempRow.getHeight());
            templateRow.setHeightInPoints(tempRow.getHeightInPoints());
            templateRow.setRowStyle(tempRow.getRowStyle());
            templateRow.setZeroHeight(tempRow.getZeroHeight());
            for (int i = tempRow.getFirstCellNum(); i <= tempRow.getLastCellNum(); i++) {
                Cell cell = tempRow.getCell(i);
                if (cell != null) {
                    InnerCell innerCell = new InnerCell();
                    innerCell.setCellStyle(cell.getCellStyle());
                    innerCell.setCellType(cell.getCellType());
                    templateRow.setCell(i, innerCell);
                }
            }
        }
    }
    cache.put(rowIndex, templateRow);
    return templateRow;
}

From source file:org.is.jxlpoi.JXLPOISheet.java

License:Apache License

public JXLPOICell[] getColumn(int colIndex) {

    int lastRowNum = sheet.getLastRowNum();

    Vector<JXLPOICell> collected = new Vector<JXLPOICell>();
    for (int rowIndex = 0; rowIndex <= lastRowNum; rowIndex++) {
        Row row = sheet.getRow(rowIndex);
        JXLPOICell cs;// ww w  .  ja  v  a2  s . c  o  m
        if (row != null) {
            int numCells = row.getLastCellNum();
            if (numCells < colIndex + 1) { //no cell
                cs = new JXLPOICell(null, null, colIndex, rowIndex);
            } else {
                Cell cell = row.getCell(colIndex);
                cs = new JXLPOICell(workbook, cell, colIndex, rowIndex);
            }
        } else {
            cs = new JXLPOICell(null, null, colIndex, rowIndex);
        }

        collected.add(cs);
    }

    JXLPOICell[] cs = new JXLPOICell[collected.size()];
    collected.copyInto(cs);
    return cs;
}

From source file:org.is.jxlpoi.JXLPOISheet.java

License:Apache License

public JXLPOICell[] getRow(int rowIndex) {
    Row row = sheet.getRow(rowIndex);
    if (row == null) {
        return new JXLPOICell[0];
    }// www . j  a v  a 2 s.com
    int numCells = row.getLastCellNum();
    JXLPOICell[] cs = new JXLPOICell[numCells];
    for (int colIndex = 0; colIndex < cs.length; colIndex++) {
        Cell cell = row.getCell(colIndex);
        cs[colIndex] = new JXLPOICell(workbook, cell, colIndex, rowIndex);
    }
    return cs;
}

From source file:org.jberet.support.io.ExcelItemReaderWriterBase.java

License:Open Source License

/**
 * Saves string values to a string array for all non-blank cells in the row passed in. Useful when trying to get
 * header values./*from   w  w  w .j a va2  s  . co  m*/
 *
 * @param row the source row to get values from
 * @return a String array containing values from all non-blank cells in the row
 */
protected static String[] getCellStringValues(final Row row) {
    final short firstCellNum = row.getFirstCellNum();
    final short lastCellNum = row.getLastCellNum();
    final String[] values = new String[lastCellNum - firstCellNum];
    for (int i = 0; i < values.length; ++i) {
        values[i] = row.getCell(i).getStringCellValue();
    }
    return values;
}

From source file:org.jberet.support.io.ExcelUserModelItemReader.java

License:Open Source License

@Override
public Object readItem() throws Exception {
    if (currentRowNum == this.end) {
        return null;
    }// w  w  w . j ava 2 s . co m
    Row row;
    while (rowIterator.hasNext()) {
        row = rowIterator.next();
        currentRowNum = row.getRowNum();
        final short lastCellNum = row.getLastCellNum();
        if (lastCellNum == -1) { // no cell in the current row
            continue;
        }
        final int lastColumn = Math.max(lastCellNum, minColumnCount);
        if (java.util.List.class.isAssignableFrom(beanType)) {
            final List<Object> resultList = new ArrayList<Object>();
            for (int cn = 0; cn < lastColumn; cn++) {
                final Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
                if (c == null) { // The spreadsheet is empty in this cell
                    resultList.add(null);
                } else {
                    resultList.add(getCellValue(c, c.getCellType()));
                }
            }
            return resultList;
        } else {
            final Map<String, Object> resultMap = new HashMap<String, Object>();
            for (int cn = 0; cn < header.length; cn++) {
                final Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
                if (c != null) {
                    resultMap.put(header[cn], getCellValue(c, c.getCellType()));
                }
            }
            if (java.util.Map.class.isAssignableFrom(beanType)) {
                return resultMap;
            } else {
                if (objectMapper == null) {
                    initJsonFactoryAndObjectMapper();
                }
                final Object readValue = objectMapper.convertValue(resultMap, beanType);
                if (!skipBeanValidation) {
                    ItemReaderWriterBase.validate(readValue);
                }
                return readValue;
            }
        }
    }

    return null;
}

From source file:org.jeecgframework.poi.excel.imports.ExcelImportServer.java

License:Apache License

/***
 * ?List?/*from  w w  w .j  a v  a 2 s  .  co m*/
 * 
 * @param exclusions
 * @param object
 * @param param
 * @param row
 * @param titlemap
 * @param targetId
 * @param pictures
 * @param params
 */
private void addListContinue(Object object, ExcelCollectionParams param, Row row, Map<Integer, String> titlemap,
        String targetId, Map<String, PictureData> pictures, ImportParams params) throws Exception {
    Collection collection = (Collection) PoiPublicUtil.getMethod(param.getName(), object.getClass())
            .invoke(object, new Object[] {});
    Object entity = PoiPublicUtil.createObject(param.getType(), targetId);
    String picId;
    boolean isUsed = false;// ??
    for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
        Cell cell = row.getCell(i);
        String titleString = (String) titlemap.get(i);
        if (param.getExcelParams().containsKey(titleString)) {
            if (param.getExcelParams().get(titleString).getType() == 2) {
                picId = row.getRowNum() + "_" + i;
                saveImage(object, picId, param.getExcelParams(), titleString, pictures, params);
            } else {
                saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row);
            }
            isUsed = true;
        }
    }
    if (isUsed) {
        collection.add(entity);
    }
}

From source file:org.jeecgframework.poi.excel.imports.ExcelImportServer.java

License:Apache License

private <T> List<T> importExcel(Collection<T> result, Sheet sheet, Class<?> pojoClass, ImportParams params,
        Map<String, PictureData> pictures) throws Exception {
    List collection = new ArrayList();
    Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>();
    List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>();
    String targetId = null;//from w w  w .ja  va2  s.c om
    if (!Map.class.equals(pojoClass)) {
        Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
        ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
        if (etarget != null) {
            targetId = etarget.value();
        }
        getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null);
    }
    Iterator<Row> rows = sheet.rowIterator();
    for (int j = 0; j < params.getTitleRows(); j++) {
        rows.next();
    }
    Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection);
    Row row = null;
    Object object = null;
    String picId;
    // 
    int count = 4;
    while (rows.hasNext()
            && (row == null || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) {
        count++;
        row = rows.next();
        // ???,?,?
        if ((row.getCell(params.getKeyIndex()) == null
                || StringUtils.isEmpty(getKeyValue(row.getCell(params.getKeyIndex())))) && object != null) {
            for (ExcelCollectionParams param : excelCollection) {
                try {
                    addListContinue(object, param, row, titlemap, targetId, pictures, params);
                } catch (Exception e) {
                    // TODO: handle exception
                    Exception f = new Exception("" + count + "" + e.getMessage());
                    throw f;
                }
            }
        } else {
            object = PoiPublicUtil.createObject(pojoClass, targetId);
            try {
                for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) {
                    Cell cell = row.getCell(i);
                    String titleString = (String) titlemap.get(i);
                    if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) {
                        if (excelParams.get(titleString) != null
                                && excelParams.get(titleString).getType() == 2) {
                            picId = row.getRowNum() + "_" + i;
                            saveImage(object, picId, excelParams, titleString, pictures, params);
                        } else {
                            try {
                                saveFieldValue(params, object, cell, excelParams, titleString, row);
                            } catch (Exception e) {
                                // TODO: handle exception
                                Exception f = new Exception("" + count + "" + e.getMessage());
                                throw f;
                            }
                        }
                    }
                }

                for (ExcelCollectionParams param : excelCollection) {
                    try {
                        addListContinue(object, param, row, titlemap, targetId, pictures, params);
                    } catch (Exception e) {
                        // TODO: handle exception
                        Exception f = new Exception("" + count + "" + e.getMessage());
                        throw f;
                    }
                }
                collection.add(object);
            } catch (ExcelImportException e) {
                if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
                    throw new ExcelImportException(e.getType(), e);
                }
            }
        }
    }
    return collection;
}

From source file:org.jeecgframework.poi.excel.imports.ExcelImportServer.java

License:Apache License

/**
 * ?(?,,?)//from  w  w  w. j av  a 2s  . c o  m
 * 
 * @param params
 * @param object
 * @param cell
 * @param excelParams
 * @param titleString
 * @param row
 * @throws Exception
 */
private void saveFieldValue(ImportParams params, Object object, Cell cell,
        Map<String, ExcelImportEntity> excelParams, String titleString, Row row) throws Exception {
    Object value = cellValueServer.getValue(params.getDataHanlder(), object, cell, excelParams, titleString);
    if (object instanceof Map) {
        if (params.getDataHanlder() != null) {
            params.getDataHanlder().setMapValue((Map) object, titleString, value);
        } else {
            ((Map) object).put(titleString, value);
        }
    } else {
        ExcelVerifyHanlderResult verifyResult = verifyHandlerServer.verifyData(object, value, titleString,
                excelParams.get(titleString).getVerify(), params.getVerifyHanlder());
        if (verifyResult.isSuccess()) {
            setValues(excelParams.get(titleString), object, value);
        } else {
            Cell errorCell = row.createCell(row.getLastCellNum());
            errorCell.setCellValue(verifyResult.getMsg());
            errorCell.setCellStyle(errorCellStyle);
            verfiyFail = true;
            throw new ExcelImportException(ExcelImportEnum.VERIFY_ERROR);
        }
    }
}

From source file:org.joeffice.spreadsheet.sheet.SheetTableModel.java

License:Apache License

public int getLastColumnNum() {
    int lastRowNum = sheet.getLastRowNum();
    int lastColumn = 0;
    for (int i = 0; i < lastRowNum; i++) {
        Row row = sheet.getRow(i);
        if (row != null) {
            int lastCell = row.getLastCellNum() - 1;
            if (lastColumn < lastCell) {
                lastColumn = lastCell;/*from w  ww.  j a  va2  s  .c o  m*/
            }
        }
    }
    return lastColumn;
}

From source file:org.joeffice.spreadsheet.sheet.SheetTableModel.java

License:Apache License

public void removeColumns(int... columns) {
    for (int rowIndex = 0; rowIndex < sheet.getLastRowNum(); rowIndex++) {
        Row row = sheet.getRow(rowIndex);
        if (row != null) {
            short lastColumn = row.getLastCellNum();
            for (int i = columns.length; i >= 0; i--) {
                int columnIndex = columns[i];
                if (columnIndex <= lastColumn) {
                    Cell cell = row.getCell(columnIndex);
                    // I'm afraid that this only clear the cell and doesn't shift
                    // Also shiting columns is not supported in POI, so nothing happens for empty cells
                    if (cell != null) {
                        row.removeCell(cell);
                    }//from ww  w .  ja  va2s.  c  om
                }
            }
        }
    }
    fireTableStructureChanged();
}