实际项目的场景并非只有这两种情况,
所以要借助工具类来解决项目中遇到的问题:
我遇到的问题是:从excel读取数据的时候,如果是空数据的话,一直报异常,空数据的情况很多,并不只是单单的“非空”,“非空串”还有其他的.....(这是重点!)
我的代码如下:
package com.cmf.otc.chinamoney.data.common;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
/** 将excel转化为List<T>列表 */
public class Excel2ObjectUtil {
public static <T> List<T> translate(Map<String, String> map,Class<T> clazz,Sheet sheet,int dataRowBeginIndex) throws InstantiationException, IllegalAccessException{
Map<String, Integer> fieldName2ExcelColumnIndexMap = new HashMap<String, Integer>();
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
String mapKey = key.replaceAll("_", "").toLowerCase();
fieldName2ExcelColumnIndexMap.put(mapKey, Integer.valueOf((String)map.get(key)));
}
if(sheet.getLastRowNum() <dataRowBeginIndex){
return new ArrayList<>();
}
//反射获取目标实体类的属性列表
Field[] fields = clazz.getDeclaredFields();
List<T> returnList = new ArrayList<>();
//遍历数据行
for(int i = dataRowBeginIndex; i < sheet.getLastRowNum() + dataRowBeginIndex; i++){
Row row = sheet.getRow(i);
if(row==null || row.getCell(0) == null){//因为 sheet.getLastRowNum() 获取不到真实的行数,所以通过这种方式判断
break;
}
T obj = clazz.newInstance();
for(int j = 0; j < fields.length; j++){
try {
//通过反射方式设置值
String fieldName = fields[j].getName();
if(fieldName2ExcelColumnIndexMap.containsKey(fieldName.toLowerCase())){
int cellColumnIndex = fieldName2ExcelColumnIndexMap.get(fieldName.toLowerCase());
Cell cell = row.getCell(cellColumnIndex);
if(!fields[j].isAccessible()){
fields[j].setAccessible(true);
}
if(cell!=null){
if(cell.getCellTypeEnum() == CellType.NUMERIC){
fields[j].set(obj, new BigDecimal(cell.getNumericCellValue()));
}else{
if(fields[j].getType() == String.class){
fields[j].set(obj, cell.getStringCellValue());
}else if(fields[j].getType() == Integer.class){
fields[j].set(obj, Integer.valueOf(cell.getStringCellValue()));
}else if(fields[j].getType() == BigDecimal.class){
fields[j].set(obj, StrToBigDecimal.change(cell.getStringCellValue()));//注意:这里就是无数据,所抛出的异常。
}
}
}
}
} catch (Exception e) {
System.out.println("读取excel字段时错误,实体类字段名:"+fields[j].getName()+",错误消息:"+e.getMessage());
}
}
returnList.add(obj);
}
return returnList;
}
}
//将没有数据的内容,如何转换成BigDecimal类型,这里面用到了:org.springframework.util.StringUtils里面的hasText(String str)方法.
package com.cmf.otc.chinamoney.data.common;
import java.math.BigDecimal;
import org.springframework.util.StringUtils;
/**
* @author 作者 E-mail:
* @version 创建时间:2017年8月29日 下午4:26:22
* 类说明
*/
public class StrToBigDecimal {
public static BigDecimal change(String strBigDecimal){
if(StringUtils.hasText(strBigDecimal)){
return new BigDecimal(strBigDecimal.replaceAll(",",""));
}else{
return null;
}
}
}
附::大神对org.springframework.util.StringUtils里面的hasText(String str)方法解读:http://blog.csdn.net/chunqiuwei/article/details/8131298