EasyExcel简单操作
<-----pom依赖----->
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>
<-----实体类----->
@Data //lombok 注解
public class ExcelUser {
@ExcelProperty(value = "法人名称" ,index = 0)
private String frmc;
@ExcelProperty(value = "统一社会信用代码",index = 1)
private String tyshdm;
@ExcelProperty(value = "实体ID",index = 2)
private String id;
}
<-----EasyExce监听器----->
public class EasyExcelListener extends AnalysisEventListener<ExcelUser> {
static List<ExcelUser> list = new ArrayList<ExcelUser>();
@Override
public void invoke(ExcelUser excelUser, AnalysisContext analysisContext) {
//添加读取到的数据
list.add(excelUser);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
<-----Excel调用测试----->
public class ExcelUtils {
public static void main(String[] args) {
// 文件数据读取
File readFile = new File("C:\\Users\\zhuxun-pc\\Desktop\\example.xlsx");
EasyExcel.read(readFile, ExcelUser.class, new EasyExcelListener()).sheet().doRead();
List<ExcelUser> list = EasyExcelListener.list;
System.out.println("--------分割线--------");
// 文件数据写入
File wirtFile = new File("C:\\Users\\zhuxun-pc\\Desktop\\presion.xlsx");
EasyExcel.write(wirtFile).sheet("presion").head(ExcelUser.class).doWrite(list);
System.out.println("-------结束--------");
}
}