import com.example.springbatch.xxkfz.annotation.ExcelField;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @author xxkfz
* Excel工具类
*/
@Slf4j
public class ExcelUtils {
private HSSFWorkbook workbook;
public ExcelUtils(String fileDir) {
File file = new File(fileDir);
try {
workbook = new HSSFWorkbook( new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取Excel数据
*
* @param sheetName
* @param object
* @return
*/
public List readFromExcelData(String sheetName, Object object) {
List result = new ArrayList();
// 获取该对象的class对象
Class class_ = object.getClass();
// 获得该类的所有属性
Field[] fields = class_.getDeclaredFields();
// 读取excel数据 获得指定的excel表
HSSFSheet sheet = workbook.getSheet(sheetName);
// 获取表格的总行数
int rowCount = sheet.getLastRowNum() + 1 ; // 需要加一
if (rowCount < 1 ) {
return result;
}
// 获取表头的列数
int columnCount = sheet.getRow( 0 ).getLastCellNum();
// 读取表头信息,确定需要用的方法名---set方法
// 用于存储方法名
String[] methodNames = new String[columnCount]; // 表头列数即为需要的set方法个数
// 用于存储属性类型
String[] fieldTypes = new String[columnCount];
// 获得表头行对象
HSSFRow titleRow = sheet.getRow( 0 );
// 遍历表头列
for ( int columnIndex = 0 ; columnIndex < columnCount; columnIndex++) {
// 取出某一列的列名
String colName = titleRow.getCell(columnIndex).toString();
/*
// 将列名的首字母字母转化为大写
String UColName = Character.toUpperCase(colName.charAt(0)) + colName.substring(1, colName.length());
// set方法名存到methodNames
methodNames[columnIndex] = "set" + UColName;
*/
//
String fieldName = fields[columnIndex].getName();
String UpperFieldName = Character.toUpperCase(fieldName.charAt( 0 )) + fieldName.substring( 1 , fieldName.length());
methodNames[columnIndex] = "set" + UpperFieldName;
// 遍历属性数组
for ( int i = 0 ; i < fields.length; i++) {
// 获取属性上的注解name值
String name = fields[i].getAnnotation(ExcelField. class ).name();
// 属性与表头相等
if (Objects.nonNull(name) && colName.equals(name)) {
// 将属性类型放到数组中
fieldTypes[columnIndex] = fields[i].getType().getName();
}
}
}
// 逐行读取数据 从1开始 忽略表头
for ( int rowIndex = 1 ; rowIndex < rowCount; rowIndex++) {
// 获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null ) {
Object obj = null ;
// 实例化该泛型类的对象一个对象
try {
obj = class_.newInstance();
} catch (Exception e1) {
e1.printStackTrace();
}
// 获得本行中各单元格中的数据
for ( int columnIndex = 0 ; columnIndex < columnCount; columnIndex++) {
String data = row.getCell(columnIndex).toString();
// 获取要调用方法的方法名
String methodName = methodNames[columnIndex];
obj = this .valueConvert(fieldTypes[columnIndex], methodName, class_, obj, data);
}
result.add(obj);
}
}
return result;
}
/**
* @param fieldType 字段类型
* @param methodName 方法名
* @param class_
* @param data
* @return
*/
private Object valueConvert(String fieldType, String methodName, Class class_, Object obj, String data) {
Method method = null ;
if (Objects.isNull(fieldType) || Objects.isNull(methodName) || Objects.isNull(class_) || Objects.isNull(obj)) {
return obj;
}
try {
switch (fieldType) {
case "java.lang.String" :
method = class_.getDeclaredMethod(methodName, String. class );
method.invoke(obj, data); // 执行该方法
break ;
case "java.lang.Integer" :
method = class_.getDeclaredMethod(methodName, Integer. class );
Integer value = Integer.valueOf(data);
method.invoke(obj, value); // 执行该方法
break ;
case "java.lang.Boolean" :
method = class_.getDeclaredMethod(methodName, Boolean. class );
Boolean booleanValue = Boolean.getBoolean(data);
method.invoke(obj, booleanValue); // 执行该方法
break ;
case "java.lang.Double" :
method = class_.getDeclaredMethod(methodName, Double. class );
double doubleValue = Double.parseDouble(data);
method.invoke(obj, doubleValue); // 执行该方法
break ;
case "java.math.BigDecimal" :
method = class_.getDeclaredMethod(methodName, BigDecimal. class );
BigDecimal bigDecimal = new BigDecimal(data);
method.invoke(obj, bigDecimal); // 执行该方法
break ;
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
|