pom
官网:https://www.yuque.com/easyexcel/doc
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
写:
public class TestDemo {
public static void main(String[] args) {
List<CondUser> list = new ArrayList<CondUser>();
for (int i = 0; i < 10; i++) {
CondUser condUser = new CondUser();
condUser.setId(Long.valueOf(i+1));
condUser.setName(String.valueOf(i+1));
condUser.setAge(i+1);
list.add(condUser);
}
EasyExcel.write("F:\\2\\qyc.xlsx",CondUser.class).sheet("客户").doWrite(list);
}
}
读:
listener
public class ExcelListener extends AnalysisEventListener<CondUser> {
private List<CondUser> list = new ArrayList<>();
@Override
public void invoke(CondUser condUser, AnalysisContext analysisContext) {
list.add(condUser);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
public List<CondUser> read(){
return list;
}
}
main
public static void main(String[] args) {
String file="F:\\\\2\\\\qyc.xlsx";
ExcelListener excelListener = new ExcelListener();
EasyExcel.read(file,CondUser.class,excelListener).sheet().doRead();
for (CondUser u:excelListener.read()
) {
System.out.println(u);
}
}