import com.guard.biz.common.util.excel.ExcelIn;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Wei
* @time 2019/10/29
* @Description excel 导入工具类
*/
public class ExcelImportUtil {
private static final Logger log = LoggerFactory.getLogger(ExcelImportUtil.class);
private static BeanUtilsBean beanUtilsBean = new BeanUtilsBean();
static {
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.DateConverter(null), java.util.Date.class);
}
/**
* 表头名字和对应所在第几列的下标,用于根据title取到对应的值
*/
private final Map title_to_index = new HashMap<>();
/**
* 所有带有ExcelIn注解的字段
*/
private final List fields = new ArrayList<>();
/**
* 统计表格的行和列数量用来遍历表格
*/
private int firstCellNum = 0;
private int lastCellNum = 0;
private int firstRowNum = 0;
private int lastRowNum = 0;
private String sheetName;
private Sheet sheet;
public List read(InputStream in, Class clazz) throws Exception {
gatherAnnotationFields(clazz);
configSheet(in);
configHeader();
List rList = null;
try {
rList = readContent(clazz);
} catch (IllegalAccessException e) {
throw new Exception(e);
} catch (InstantiationException e) {
throw new Exception(e);
} catch (InvocationTargetException e) {
throw new Exception(e);
}
return rList;
}
private List readContent(Class clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException {
Object o = null;
Row row = null;
List