private Class<? extends Object> getPropertyType(Object object, ExcelCell cellDefinition) throws Exception { Class<?> clazz = DataConvertorConfigurator.getInstance().getSupportedClass(cellDefinition.getType()); if (clazz != null) { return clazz; } return getPropertyType(object, cellDefinition.getDataName()); }
private Object checkValue( int sheetNo, String cellIndex, Object value, ExcelCell cellDefinition, Class<? extends Object> clazz) throws ExcelManipulateException { DataConvertor<?> dc = DataConvertorConfigurator.getInstance().getConvertor(clazz); // primitive type should be mandatory if (clazz.isPrimitive()) cellDefinition.setMandatory(true); if (dc == null) { throw new ExcelManipulateException( ErrorCode.UNSUPPORTING_DATA_TYPE, new Object[] { sheetNo + 1, cellIndex, null, cellDefinition.getPattern(), cellDefinition.getChoiceString() }); } return dc.convert(value, sheetNo, cellIndex, cellDefinition); }
private void readSimpleBlock( Workbook wb, int sheetNo, ExcelBlock blockDefinition, OgnlStack stack, ReadStatus readStatus) { // Simple Block will only care about cells in these Block Sheet sheet = wb.getSheetAt(sheetNo); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); for (ExcelCell cellDefinition : blockDefinition.getCells()) { Row row = sheet.getRow(cellDefinition.getRow()); Cell cell = row == null ? null : row.getCell(cellDefinition.getCol()); try { Object value = getCellValue(cell, evaluator); value = checkValue( sheetNo, ExcelUtil.getCellIndex(cellDefinition.getRow(), cellDefinition.getCol()), value, cellDefinition, getPropertyType(stack.peek(), cellDefinition)); logger.debug( "{}[Checked]:{}", ExcelUtil.getCellIndex(cellDefinition.getRow(), cellDefinition.getCol()), value); stack.setValue(cellDefinition.getDataName(), value); } catch (ExcelManipulateException e) { if (readStatus.getStatus() == ReadStatus.STATUS_SUCCESS) readStatus.setStatus(ReadStatus.STATUS_DATA_COLLECTION_ERROR); readStatus.addException(e); } catch (Exception e) { e.printStackTrace(); readStatus.setStatus(ReadStatus.STATUS_SYSTEM_ERROR); readStatus.setMessage(e.getMessage()); } } }
/** * Read Block in loop condition * * @param <T> * @param wb * @param sheetNo * @param blockDefinition * @param startRow * @param needCreate * @param readStatus * @return * @throws Exception */ private Object readBlock( Workbook wb, int sheetNo, ExcelBlock blockDefinition, int startRow, ReadStatus readStatus) throws Exception { Sheet sheet = wb.getSheetAt(sheetNo); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); if (blockDefinition.getLoopClass() == null) { Map<String, Object> result = new HashMap<String, Object>(); for (ExcelCell cellDefinition : blockDefinition.getCells()) { int rowOffSet = cellDefinition.getRow() - blockDefinition.getStartRow(); Row row = sheet.getRow(startRow + rowOffSet); Cell cell = row == null ? null : row.getCell(cellDefinition.getCol()); try { Object value = getCellValue(cell, evaluator); value = checkValue( sheetNo, ExcelUtil.getCellIndex(startRow + rowOffSet, cellDefinition.getCol()), value, cellDefinition, getPropertyType(result, cellDefinition)); logger.debug( "{}[Checked]:{}", ExcelUtil.getCellIndex(startRow + rowOffSet, cellDefinition.getCol()), value); result.put(cellDefinition.getDataName(), value); } catch (ExcelManipulateException e) { if (readStatus.getStatus() == ReadStatus.STATUS_SUCCESS) readStatus.setStatus(ReadStatus.STATUS_DATA_COLLECTION_ERROR); readStatus.addException(e); } } return result; } else { Object result = blockDefinition.getLoopClass().newInstance(); OgnlStack ognlStack = new OgnlStack(result); for (ExcelCell cellDefinition : blockDefinition.getCells()) { int rowOffSet = cellDefinition.getRow() - blockDefinition.getStartRow(); Row row = sheet.getRow(startRow + rowOffSet); Cell cell = row == null ? null : row.getCell(cellDefinition.getCol()); try { Object value = getCellValue(cell, evaluator); value = checkValue( sheetNo, ExcelUtil.getCellIndex(startRow + rowOffSet, cellDefinition.getCol()), value, cellDefinition, getPropertyType(result, cellDefinition)); logger.debug( "{}[Checked]:{}", ExcelUtil.getCellIndex(startRow + rowOffSet, cellDefinition.getCol()), value); ognlStack.setValue(cellDefinition.getDataName(), value); } catch (ExcelManipulateException e) { if (readStatus.getStatus() == ReadStatus.STATUS_SUCCESS) readStatus.setStatus(ReadStatus.STATUS_DATA_COLLECTION_ERROR); readStatus.addException(e); } } return result; } }