public static <T> List<T> getList(MultipartFile excelFile,
int headerRowIndex, int startRowIndex,Class<T> tClass){
try (ExcelReader excelReader =
cn.hutool.poi.excel.ExcelUtil.getReader(excelFile.getInputStream())){
initHeaderMap(tClass).entrySet().stream()
.forEach(e -> excelReader.addHeaderAlias(e.getKey(),e.getValue()));
return excelReader.read(headerRowIndex,startRowIndex,tClass);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static <T> Map<String,String> initHeaderMap(Class<T> tClass){
return Arrays.stream(tClass.getDeclaredFields())
.map(field -> {
ExcelHeader header = field.getDeclaredAnnotation(ExcelHeader.class);
return (header == null || !StringUtils.hasLength(header.value())) ? null :
new AbstractMap.SimpleEntry<>(header.value(),field.getName());
}).filter(ObjectUtil::isNotNull)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
}
import java.lang.annotation.*;
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelHeader {
String value() default "";
}