赞
踩
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
@Data //也可以自行创建get、set方法
public class UserData {
@ExcelProperty(value = "用户编号",index = 0)
private int uid;
@ExcelProperty(value = "用户名称",index = 1)
private String username;
}
‘’写入‘’代码
public class TestWrite { public static void main(String[] args) { //构建数据list集合 List<UserData> list = new ArrayList(); for (int i=0;i<10;i++) { UserData data = new UserData(); data.setUid(i); data.setUsername("lucy"+i); list.add(data); } //设置excel文件路径和文件名称 String fileName = "F:\\excel\\01.xlsx"; //调用方法实现写操作 EasyExcel.write(fileName, UserData.class).sheet("用户信息") .doWrite(list); } }
“读出”代码
需要监听器
public class ExcelListener extends AnalysisEventListener<UserData> { //一行一行读取excel内容,从第二行读取 @Override public void invoke(UserData userData, AnalysisContext analysisContext) { System.out.println(userData); } @Override public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { System.out.println("表头信息:"+headMap); } //读取之后执行 @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }
真正读出代码
public class TestRead {
public static void main(String[] args) {
// 读取文件路径
String fileName = "F:\\excel\\01.xlsx";
//调用方法实现读取操作
EasyExcel.read(fileName, UserData.class,new ExcelListener()).sheet().doRead();
}
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。