/**
* 根据Excel文件中的内容读取数据,将数据封装成实体类对象,然后将这些对象存储到集合中
* @Author: 何智文
* @param inputStream
* @param sheetIndex
* @param clazz
* @return
* @param <T>
* @throws Exception
*/
public static <T> List<T> mapExcelToEntityList(InputStream inputStream, int sheetIndex, Class<T> clazz) throws Exception {
// 定义一个用于存储实体类对象的集合
List<T> entityList = new ArrayList<>();
// 创建一个工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
// 获取指定索引的工作表对象
HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
// 获取实体类中所有的属性对象
Field[] fields = clazz.getDeclaredFields();
// 创建一个映射表,用于存储属性名和对应的ApiModelProperty注解值之间的映射关系
Map<String, String> mapping = new HashMap<>();
for (Field field : fields) {
ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);
if (apiModelProperty != null) {
String propertyName = field.getName();
String columnLabel = apiModelProperty.value();
mapping.put(columnLabel, propertyName);
}
}
// 遍历工作表中的所有行,从第2行开始读取数据(第1行通常是表头)
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
// 获取当前行对象
HSSFRow row = sheet.getRow(i);
// 创建一个实体类对象,用于存储当前行的数据
T entity = clazz.newInstance();
// 获取当前行中的所有单元格,并使用反射机制将单元格的值映射为实体类对象的属性值
for (int j = 0; j < row.getLastCellNum(); j++) {
HSSFCell cell = row.getCell(j);
HSSFRow topRow = sheet.getRow(0);
//根据表头获取对应的属性名
String topValue = topRow.getCell(j).getStringCellValue();
String propertyName = mapping.get(topValue);
if (propertyName != null) {
Field field = clazz.getDeclaredField(propertyName);
field.setAccessible(true);
Class<?> fieldType = field.getType();
Object value = null;
if (String.class.equals(fieldType)) {
value = cell.getStringCellValue();
} else if (Integer.class.equals(fieldType) || int.class.equals(fieldType)) {
value = (int) cell.getNumericCellValue();
} else if (Double.class.equals(fieldType) || double.class.equals(fieldType)) {
value = cell.getNumericCellValue();
} else if (Float.class.equals(fieldType) || float.class.equals(fieldType)) {
value = (float) cell.getNumericCellValue();
} else if (Long.class.equals(fieldType) || long.class.equals(fieldType)) {
value = (long) cell.getNumericCellValue();
} else if (Boolean.class.equals(fieldType) || boolean.class.equals(fieldType)) {
value = cell.getBooleanCellValue();
}
field.set(entity, value);
}
}
// 将实体类对象添加到集合中
entityList.add(entity);
}
// 关闭文件输入流
inputStream.close();
return entityList;
}
实体类我也贴下
package com.hx.zhdx.ep.partner.model.supplier;
import com.jweb.comm.abs.APage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* 合作伙伴年度评分(SupplierYearGrade)实体类
*
* @author makejava
* @since 2023-03-06 09:29:42
*/
@Data
public class SupplierYearGrade implements Serializable {
private static final long serialVersionUID = -41924040468431128L;
/**
* 供应商ID
*/
@ApiModelProperty(value = "合作伙伴id(请勿修改)")
private Long supplierId;
/**
* 供应商名称
*/
private String supplierName ;
/**
* 合作伙伴统一社会信用代码(税号)
*/
private String supplierMainCode;
/**
* 合作类型
*/
private String cooperateType;
/**
* 是否合作
*/
@ApiModelProperty(value = "是否合作")
private String ifSupplier;
/**
* 服务评分(15)
*/
@ApiModelProperty(value = "服务评分(15)")
private Integer serviceGrade;
/**
* 法务评分(15)
*/
@ApiModelProperty(value = "法务评分(15)")
private Integer lawGrade;
/**
* 财务评分(15)
*/
@ApiModelProperty(value = "财务评分(15)")
private Integer faGrade;
/**
* 商务评分(15)
*/
@ApiModelProperty(value = "商务评分(15)")
private Integer commercialGrade;
/**
* 网信安评分(15)
*/
@ApiModelProperty(value = "网信安评分(15)")
private Integer networkGrade;
/**
* 最终评价分
*/
@ApiModelProperty(value = "最终评价分")
private Integer finalGrade;
/**
* 状态1000 有效,1100 无效
*/
private String statusCd;
/**
* 创建人
*/
private Long createStaff;
/**
* 创建时间
*/
private String createDate;
/**
* 修改人
*/
private Long updateStaff;
/**
* 修改时间
*/
private String updateDate;
/**
* 年度
*/
private Integer year;
/**
* 业务评分(25)
*/
@ApiModelProperty(value = "业务评分(25)")
private Integer busGrade;
}