您的位置:首页 > 其它

在Excel导入时判断效验

2018-01-08 11:06 204 查看
1:excel有数据效验时 空值判断

/**
* 检查表格数据是否为空  excel处理数据有效性  poi把空值当作shu
*
* @param obj
* @return
*/
private boolean checkObjFieldIsNotNull(Object obj) {
try {
for (Field f : obj.getClass().getDeclaredFields()) {

4000
f.setAccessible(true);
if (f.get(obj) != null) {
return true;
}
}
} catch (IllegalAccessException e) {

}
return false;
}

/**
* 调用这个方法,可以将空白行去掉:
*/
Iterator<excelus> it = excelList.iterator();//excelList是List<excelLine>类型的Excel数据,excelLine是行的数据类型
while(it.hasNext()){
if (isBlankObject(it.next())) {
it.remove();
}
}
/**
* 用反射的方法可以排除这种数据
* @param model
* @return
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static boolean isBlankObject(Object model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Field[] field = model.getClass().getDeclaredFields();        //获取实体类的所有属性,返回Field数组
for(int j=0 ; j<field.length ; j++){     //遍历所有属性
String name = field[j].getName();    //获取属性的名字

//System.out.println("attribute name:"+name);
name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写,方便构造get,set方法
//String type = field[j].getGenericType().toString();    //获取属性的类型
Method m = model.getClass().getMethod("get"+name);
if(!StringUtils.isBlank((String)m.invoke(model))){
return false;
}
}

return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: