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

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

Introduction

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

Prototype

Cell getCell(int cellnum);

Source Link

Document

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

Usage

From source file:com.jeefuse.system.log.web.imports.excel.GsysOperatelogExcelImport.java

License:GNU General Public License

/**
 * populate model./*from   w ww . j  ava  2 s  .c  om*/
 * 
 * @generated
 */
@Override
protected GsysOperatelog readExcelToModel(Row row, String[] columnNames) {
    if (row == null)
        return null;
    GsysOperatelog model = new GsysOperatelog();
    int cellLenght = columnNames.length;
    Cell cell = null;
    for (int i = 0; i < cellLenght; i++) {
        cell = row.getCell(i);
        String columnName = columnNames[i];
        GsysOperatelogField gsysOperatelogField = GsysOperatelogField.valueOfFieldLabel(columnName);
        if (null == gsysOperatelogField) {
            gsysOperatelogField = GsysOperatelogField.valueOfFieldName(columnName);
        }
        if (null == gsysOperatelogField)
            throw new DataNoExistException("??:" + columnName);
        setModelData(model, cell, gsysOperatelogField);
        InvalidValue[] invalidValues = GsysOperatelogValidate.validateProperty(model, gsysOperatelogField);
        if (invalidValues.length > 0) {
            List<String> errors = new ArrayList<String>();
            for (InvalidValue invalidValue : invalidValues) {
                errors.add(gsysOperatelogField.getFieldLabel() + ": " + invalidValue.getMessage());
            }
            throw new ValidateViolationException("" + (cell.getRowIndex() + 1) + ","
                    + (cell.getColumnIndex() + 1) + "!", errors);
        }
    }
    return model;
}

From source file:com.jeefuse.system.param.web.imports.excel.GsysParameterExcelImport.java

License:GNU General Public License

/**
 * populate model./*from  w  ww  .ja v a2s  .c  om*/
 * 
 * @generated
 */
@Override
protected GsysParameter readExcelToModel(Row row, String[] columnNames) {
    if (row == null)
        return null;
    GsysParameter model = new GsysParameter();
    int cellLenght = columnNames.length;
    Cell cell = null;
    for (int i = 0; i < cellLenght; i++) {
        cell = row.getCell(i);
        String columnName = columnNames[i];
        GsysParameterField gsysParameterField = GsysParameterField.valueOfFieldLabel(columnName);
        if (null == gsysParameterField) {
            gsysParameterField = GsysParameterField.valueOfFieldName(columnName);
        }
        if (null == gsysParameterField)
            throw new DataNoExistException("??:" + columnName);
        setModelData(model, cell, gsysParameterField);
        InvalidValue[] invalidValues = GsysParameterValidate.validateProperty(model, gsysParameterField);
        if (invalidValues.length > 0) {
            List<String> errors = new ArrayList<String>();
            for (InvalidValue invalidValue : invalidValues) {
                errors.add(gsysParameterField.getFieldLabel() + ": " + invalidValue.getMessage());
            }
            throw new ValidateViolationException("" + (cell.getRowIndex() + 1) + ","
                    + (cell.getColumnIndex() + 1) + "!", errors);
        }
    }
    return model;
}

From source file:com.jeefuse.system.security.web.imports.excel.GsysFunctionExcelImport.java

License:GNU General Public License

/**
 * populate model.//  w w w .  j a v  a 2s .co m
 * 
 * @generated
 */
@Override
protected GsysFunction readExcelToModel(Row row, String[] columnNames) {
    if (row == null)
        return null;
    GsysFunction model = new GsysFunction();
    int cellLenght = columnNames.length;
    Cell cell = null;
    for (int i = 0; i < cellLenght; i++) {
        cell = row.getCell(i);
        String columnName = columnNames[i];
        GsysFunctionField gsysFunctionField = GsysFunctionField.valueOfFieldLabel(columnName);
        if (null == gsysFunctionField) {
            gsysFunctionField = GsysFunctionField.valueOfFieldName(columnName);
        }
        if (null == gsysFunctionField)
            throw new DataNoExistException("??:" + columnName);
        setModelData(model, cell, gsysFunctionField);
        InvalidValue[] invalidValues = GsysFunctionValidate.validateProperty(model, gsysFunctionField);
        if (invalidValues.length > 0) {
            List<String> errors = new ArrayList<String>();
            for (InvalidValue invalidValue : invalidValues) {
                errors.add(gsysFunctionField.getFieldLabel() + ": " + invalidValue.getMessage());
            }
            throw new ValidateViolationException("" + (cell.getRowIndex() + 1) + ","
                    + (cell.getColumnIndex() + 1) + "!", errors);
        }
    }
    return model;
}

From source file:com.jeefuse.system.security.web.imports.excel.GsysResourceExcelImport.java

License:GNU General Public License

/**
 * populate model./*w ww .j  a v  a  2  s.co  m*/
 * 
 * @generated
 */
@Override
protected GsysResource readExcelToModel(Row row, String[] columnNames) {
    if (row == null)
        return null;
    GsysResource model = new GsysResource();
    int cellLenght = columnNames.length;
    Cell cell = null;
    for (int i = 0; i < cellLenght; i++) {
        cell = row.getCell(i);
        setModelData(model, cell, columnNames[i]);
    }
    return model;
}

From source file:com.jeefuse.system.security.web.imports.excel.GsysRoleExcelImport.java

License:GNU General Public License

/**
 * populate model./*from  w w  w  . ja v  a 2 s.  c om*/
 * 
 * @generated
 */
@Override
protected GsysRole readExcelToModel(Row row, String[] columnNames) {
    if (row == null)
        return null;
    GsysRole model = new GsysRole();
    int cellLenght = columnNames.length;
    Cell cell = null;
    for (int i = 0; i < cellLenght; i++) {
        cell = row.getCell(i);
        setModelData(model, cell, columnNames[i]);
    }
    return model;
}

From source file:com.jeefuse.system.security.web.imports.excel.GsysUserExcelImport.java

License:GNU General Public License

/**
 * populate model./* www . j a v a2  s  .  com*/
 * 
 * @generated
 */
@Override
protected GsysUser readExcelToModel(Row row, String[] columnNames) {
    if (row == null)
        return null;
    GsysUser model = new GsysUser();
    int cellLenght = columnNames.length;
    Cell cell = null;
    for (int i = 0; i < cellLenght; i++) {
        cell = row.getCell(i);
        setModelData(model, cell, columnNames[i]);
    }
    return model;
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityExcelRowParser.java

License:Apache License

/**
 * Gets field raw data value resolved by locator.
 *
 * @param locator/*w  ww.  j  a  v  a  2 s .  c  o m*/
 *            activity field locator
 * @param cData
 *            MS Excel document row representing activity object data fields
 * @param formattingNeeded
 *            flag to set if value formatting is not needed
 * @return raw value resolved by locator, or {@code null} if value is not resolved
 *
 * @throws ParseException
 *             if exception occurs while resolving raw data value
 */
@Override
protected Object resolveLocatorValue(ActivityFieldLocator locator, ActivityContext cData,
        AtomicBoolean formattingNeeded) throws ParseException {
    Object val = null;
    String locStr = locator.getLocator();
    Row row = cData.getData();

    if (StringUtils.isNotEmpty(locStr)) {
        int cellIndex = CellReference.convertColStringToIndex(locStr);
        if (cellIndex < 0) {
            throw new ParseException(
                    StreamsResources.getStringFormatted(MsOfficeStreamConstants.RESOURCE_BUNDLE_NAME,
                            "ActivityExcelRowParser.unresolved.cell.reference", locStr),
                    row.getRowNum());
        }
        Cell cell = row.getCell(cellIndex);
        boolean cellFound = false;
        if (cell != null) {
            val = getCellValue(cell);
            cellFound = true;
        }

        logger().log(OpLevel.TRACE,
                StreamsResources.getString(MsOfficeStreamConstants.RESOURCE_BUNDLE_NAME,
                        "ActivityExcelRowParser.resolved.cell.value"),
                locStr, row.getSheet().getWorkbook().getMissingCellPolicy(), toString(val));
    }

    return val;
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityExcelSheetParser.java

License:Apache License

/**
 * Gets field raw data value resolved by locator.
 *
 * @param locator/* w  w w.  j  ava  2  s  .  c om*/
 *            activity field locator
 * @param cData
 *            MS Excel document sheet representing activity object data fields
 * @param formattingNeeded
 *            flag to set if value formatting is not needed
 * @return raw value resolved by locator, or {@code null} if value is not resolved
 *
 * @throws ParseException
 *             if exception occurs while resolving raw data value
 */
@Override
protected Object resolveLocatorValue(ActivityFieldLocator locator, ActivityContext cData,
        AtomicBoolean formattingNeeded) throws ParseException {
    Object val = null;
    String locStr = locator.getLocator();
    Sheet sheet = cData.getData();

    if (StringUtils.isNotEmpty(locStr)) {
        CellReference ref = new CellReference(locStr);
        boolean cellFound = false;
        if (ref.getRow() < 0 || ref.getCol() < 0) {
            throw new ParseException(
                    StreamsResources.getStringFormatted(MsOfficeStreamConstants.RESOURCE_BUNDLE_NAME,
                            "ActivityExcelRowParser.unresolved.cell.reference", locStr),
                    sheet.getWorkbook().getSheetIndex(sheet));
        }
        Row row = sheet.getRow(ref.getRow());
        if (row != null) {
            Cell cell = row.getCell(ref.getCol());
            if (cell != null) {
                val = getCellValue(cell);
                cellFound = true;
            } else {
                val = row;
            }
        }

        logger().log(OpLevel.TRACE,
                StreamsResources.getString(MsOfficeStreamConstants.RESOURCE_BUNDLE_NAME,
                        "ActivityExcelRowParser.resolved.cell.value"),
                locStr, sheet.getWorkbook().getMissingCellPolicy(), toString(val));
    }

    return val;
}

From source file:com.joalgoca.validatorLayout.layoutDefinition.XLSXDocumentLayout.java

@Override
public ResponseValidator validateDocument(InputStream inputStream) {
    ResponseValidator response;//from  w w w .j a v  a2  s.  c  o  m
    StringBuilder stringBuilder = new StringBuilder();
    if (isReadyToValidate() && inputStream != null) {
        HashMap rowsType = new HashMap();
        for (int i = 0; i < documentValidator.getListRowValidator().size(); i++) {
            rowsType.put(documentValidator.getListRowValidator().get(i).getName(), i);
        }
        try {
            int rownum = 0;
            int wrong = 0;
            int right = 0;
            int skip = 0;
            OPCPackage pkg = OPCPackage.open(inputStream);
            XSSFWorkbook workBook = new XSSFWorkbook(pkg);
            FormulaEvaluator evaluator = workBook.getCreationHelper().createFormulaEvaluator();
            XSSFSheet xssfSheet = workBook.getSheetAt(0);
            for (Row row : xssfSheet) {
                String rowType = row.getCell(0).getStringCellValue();
                if (rowsType.containsKey(rowType)) {
                    RowValidator rowValidator = documentValidator.getListRowValidator()
                            .get((int) rowsType.get(rowType));
                    int columnNum = rowValidator.getListItemValidator().size();
                    if (row.getLastCellNum() == columnNum) {
                        String[] values = new String[columnNum];
                        int i = 0;
                        for (Cell cell : row) {
                            switch (evaluator.evaluateInCell(cell).getCellType()) {
                            case Cell.CELL_TYPE_NUMERIC:
                                values[i] = cell.getNumericCellValue() + "";
                                break;
                            case Cell.CELL_TYPE_STRING:
                                values[i] = cell.getStringCellValue();
                                break;
                            case Cell.CELL_TYPE_FORMULA:
                                values[i] = "";
                                break;
                            case Cell.CELL_TYPE_BLANK:
                                values[i] = "";
                                break;
                            }
                            i++;
                        }
                        ResponseValidator responseValidator = rowValidator.validate(values);
                        if (!responseValidator.isSuccess()) {
                            wrong++;
                            stringBuilder.append("{\"row\":").append(rownum).append(",\"message\":")
                                    .append(responseValidator.getMessage()).append(",");
                        } else
                            right++;
                    } else {
                        wrong++;
                        stringBuilder.append("{\"row\":").append(rownum)
                                .append(",\"success\":false,\"message\":\"Line wrong size\"},").toString();
                    }
                } else {
                    skip++;
                    stringBuilder.append("{\"row\":").append(rownum)
                            .append(",\"success\":false,\"message\":\"Unknow row type\"},").toString();
                }
                rownum++;

            }

            response = new ResponseValidator(wrong == 0,
                    "{\"skip\":" + skip + ",\"wrong\":" + wrong + ",\"right\":" + right + ",\"count\":" + rownum
                            + ",\"errorMessages\":["
                            + (stringBuilder.toString().length() > 0
                                    ? stringBuilder.substring(0, stringBuilder.toString().length() - 1)
                                    : "")
                            + "]}");
        } catch (Exception ex) {
            Logger.getLogger(FlatFixedDocumentLayout.class.getName()).log(Level.SEVERE, null, ex);
            response = new ResponseValidator(false, stringBuilder.append("\"success\":false,\"message\":\"")
                    .append(ex.getMessage()).append("\"}").toString());
        } finally {
            try {
                inputStream.close();
            } catch (IOException ex) {
                response = new ResponseValidator(false, stringBuilder.append("\"success\":false,\"message\":\"")
                        .append(ex.getMessage()).append("\"}").toString());
            }
        }
    } else {
        response = new ResponseValidator(false,
                stringBuilder.append("\"success\":false,\"message\":\"No configuration loaded\"}").toString());
    }
    return response;
}

From source file:com.joint.base.util.excel.ImportExcel.java

License:Open Source License

/**
 * ???//from   w w w  .j  a  v a2s  .c o  m
 * @param cmap: eg(Map<String,Integer> === Map("type",2))
 * @param ei
 * @return
 */
public List<Map<String, Object>> getValue(ImportExcel ei, Map<String, Integer> cmap) {
    int firstRowNum = ei.getDataRowNum();
    int lastRowNum = ei.getLastDataRowNum();
    List<Map<String, Object>> objList = Lists.newArrayList();
    Map<String, Object> rmap = null;
    for (int i = firstRowNum; i <= lastRowNum; i++) {
        Row row = sheet.getRow(i);
        if (row != null) {
            rmap = new HashMap<String, Object>();
            for (String name : cmap.keySet()) {
                Cell cell = row.getCell(cmap.get(name));
                if (cell == null) {
                    rmap.put(name, "");
                    continue;
                }

                switch (cell.getCellType()) {
                case 0: //
                    rmap.put(name, (long) row.getCell(cmap.get(name)).getNumericCellValue());
                    break;
                case 1: //
                    rmap.put(name, row.getCell(cmap.get(name)).getStringCellValue().trim().toString());
                    break;
                }

            }
            objList.add(rmap);
        }
    }

    return objList;
}