基础处理类
package com.xwcloud.superins.common.util.excel.handler;
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.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author yu
* @Description Excel处理基类
* @create 2019/1/8
* @since 1.0.0
*/
public class PoiHandler {
private static Logger logger = LoggerFactory.getLogger(PoiHandler.class);
/**
* 将excel每一行的数据转换为java对象
*
* @param obj
* @param row
* @return
* @throws Exception
*/
Map<String, Object> dataObj(Object obj, Row row, FormulaEvaluator evaluator) throws Exception {
Class<?> rowClazz = obj.getClass();
Field[] fields = FieldUtils.getAllFields(rowClazz);
if (fields == null || fields.length < 1) {
return null;
}
// 容器
Map<String, Object> map = new HashMap<>(50);
// 注意excel表格字段顺序要和obj字段顺序对齐
for (int j = 0; j < fields.length; j++) {
map.put(fields[j].getName(), getVal(row.getCell(j), evaluator));
}
return map;
}
/**
* 无公式处理val
*
* @par