最近有个项目用到 easyexcel 读取excel文件,使用的版本
<easyexcel.version>2.2.11</easyexcel.version>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
写了一个demo,读取数据,发现读取了3列,部分列丢失了。
封装的excel的实体类如下
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EnvironmentCityDO {
@ExcelProperty(value = "Location_ID", index = 0)
private String locationId;
@ExcelProperty(value = "Location_Name_EN", index = 1)
private String locationNameEn;
@ExcelProperty(value = "Location_Name_ZH", index = 2)
private String locationNameZh;
@ExcelProperty(value = "Country_Code", index = 3)
private String CountryCode;
@ExcelProperty(value = "Country_Name_EN", index = 4)
private String CountryNameEN;
@ExcelProperty(value = "Country_Name_ZH", index = 5)
private String Country_Name_ZH;
@ExcelProperty(value = "Adm1_Name_EN", index = 6)
private String Adm1_Name_EN;
@ExcelProperty(value = "Adm1_Name_ZH", index = 7)
private String Adm1_Name_ZH;
@ExcelProperty(value = "Adm2_Name_EN", index = 8)
private String Adm2_Name_EN;
@ExcelProperty(value = "Adm2_Name_ZH", index = 9)
private String Adm2_Name_ZH;
@ExcelProperty(value = "Timezone", index = 10)
private String Timezone;
@ExcelProperty(value = "Latitude", index = 11)
private String Latitude;
@ExcelProperty(value = "Longitude", index = 12)
private String Longitude;
@ExcelProperty(value = "Adcode", index = 13)
private String Adcode;
}
读取excel的代码如下
public static void main(String[] args) throws FileNotFoundException {
String path = "d:/test/city.xls";
EasyExcel.read(path, EnvironmentCityDO.class, new ReadListener<EnvironmentCityDO>() {
private List<EnvironmentCityDO> cachedDataList = new ArrayList<>(500000);
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
}
@Override
public void invokeHead(Map headMap, AnalysisContext context) {
}
@Override
public void invoke(EnvironmentCityDO data, AnalysisContext context) {
log.info("___________________________________city={}", JSON.toJSONString(data));
cachedDataList.add(data);
}
@Override
public void extra(CellExtra extra, AnalysisContext context) {
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
@Override
public boolean hasNext(AnalysisContext context) {
return true;
}
}).sheet().doRead();
}
最后发现问题出在封装的实体类上面,有数据的3列,实体类首字母是小写的,所有读取没问题,后面的实体类首字母大写的,所以读取不到。
修改实体类后,发现读取没问题了
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EnvironmentCityDO {
@ExcelProperty(value = "Location_ID", index = 0)
private String locationId;
@ExcelProperty(value = "Location_Name_EN", index = 1)
private String locationNameEn;
@ExcelProperty(value = "Location_Name_ZH", index = 2)
private String locationNameZh;
@ExcelProperty(value = "Country_Code", index = 3)
private String countryCode;
@ExcelProperty(value = "Country_Name_EN", index = 4)
private String countryNameEN;
@ExcelProperty(value = "Country_Name_ZH", index = 5)
private String country_Name_ZH;
@ExcelProperty(value = "Adm1_Name_EN", index = 6)
private String adm1_Name_EN;
@ExcelProperty(value = "Adm1_Name_ZH", index = 7)
private String adm1_Name_ZH;
@ExcelProperty(value = "Adm2_Name_EN", index = 8)
private String adm2_Name_EN;
@ExcelProperty(value = "Adm2_Name_ZH", index = 9)
private String adm2_Name_ZH;
@ExcelProperty(value = "Timezone", index = 10)
private String timezone;
@ExcelProperty(value = "Latitude", index = 11)
private String latitude;
@ExcelProperty(value = "Longitude", index = 12)
private String longitude;
@ExcelProperty(value = "Adcode", index = 13)
private String adcode;
}