//=======================================Position==================================//
//通过position形式
//实体内的注解,基于position
@CsvBindByPosition(position = 0)
@Excel(isWrap = false,name = "样例",orderNum = "1",width = 30)
private String demo;
public static <T> List<T> readDataFromLocalPathWithPosition(Class<T> clazz, String filePath) {
List<T> parseResult = new ArrayList<>();
if (FileUtils.checkFileExist(filePath)) {
ColumnPositionMappingStrategy<T> mapper = new ColumnPositionMappingStrategy<>();
mapper.setType(clazz);
try {
CsvToBean<T> csvToBean = new CsvToBeanBuilder<T>(new FileReader(filePath))
.withMappingStrategy(mapper)
.withSeparator(DEFAULT_BIZ_SEPERATOR)
.build();
parseResult = csvToBean.parse();
} catch (FileNotFoundException e) {
log.error("[CsvUtils][readDataFromLocalPathWithPosition] meet error when convert csv to java bean ,file path :", filePath);
log.error("[CsvUtils][readDataFromLocalPathWithPosition]",e);
}
}
if(CollectionUtils.isNotEmpty(parseResult)){
return parseResult;
}
return Collections.emptyList();
}
//=======================================Header==================================//
@CsvCustomBindByName(column = "num",converter = ConvertCsvStringToLong.class)
@Excel(isWrap = false,name = "人数",orderNum = "4",width = 20)
private Long num;
//ConvertCsvStringToLong是自定义的类型转换,仅需要继承AbstractBeanField即可,自行实现类型的转换
public class ConvertCsvStringToLong<T, I> extends AbstractBeanField<T, I> {
@Override
protected Object convert(String s) throws CsvDataTypeMismatchException, CsvConstraintViolationException {
return StringUtils.isNotBlank(s)?Long.valueOf(s):0L;
}
}
//以下读取时指定编码UTF-8,建议读取和写入的时候统一指定编码,避免出现中文乱码
public static <T> List<T> readDataFromLocalPathWithHeader(Class<T> clazz, String filePath) {
List<T> parseResult = new ArrayList<>();
if (FileUtils.checkFileExist(filePath)) {
HeaderColumnNameMappingStrategy<T> headerColumnNameMappingStrategy = new HeaderColumnNameMappingStrategy<>();
headerColumnNameMappingStrategy.setType(clazz);
FileInputStream fin = null;
InputStreamReader reader = null;
try {
fin= new FileInputStream(filePath);
reader = new InputStreamReader(fin, StandardCharsets.UTF_8);
CsvToBean<T> csvToBean = new CsvToBeanBuilder<T>(reader)
.withMappingStrategy(headerColumnNameMappingStrategy)
.withSeparator(DEFAULT_BIZ_SEPERATOR)
.build();
parseResult = csvToBean.parse();
} catch (FileNotFoundException e) {
log.error("[CsvUtils][readDataFromLocalPathWithHeader] meet error when convert csv to java bean ,file path :", filePath);
log.error("[CsvUtils][readDataFromLocalPathWithHeader]",e);
}finally {
try {
if (reader != null) {
reader.close();
}
if (fin != null) {
fin.close();
}
}catch (Exception e){
log.error("[CsvUtils][readDataFromLocalPathWithHeader]fail to close stream for csv",e);
}
}
}
if(CollectionUtils.isNotEmpty(parseResult)){
return parseResult;
}
return Collections.emptyList();
}