EasyExcel的简单读写
//1:导入easyexcel依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
//2 创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ContentRowHeight(25)//指定行高
@ColumnWidth(10) //这样表示将表中的所有列都设置成指定的宽度
@HeadRowHeight(30)//指定表头高度
public class User {
@ExcelProperty(value = "名字",index = 0) //后面额序号 是排序位置的.前面的value表示这个Excel的标题
private String name;
@ExcelProperty(value = "性别",index = 1)
private String gender;
@ExcelProperty(value = "年纪",index = 2)
private Integer age;
@ExcelProperty(value = "电话",index = 3)
private String phone;
@ExcelProperty(value = "地址",index = 4)
private String address;
}
//3:创建一个方法 生成数据
public static List<User> getDate() {
List<User> list = new ArrayList<>();
User u1 = new User("张三", "男", 23, "18888888888", "深圳市");
User u2 = new User("李四", "男", 20, "16666666666", "北京市");
User u3 = new User("王五", "女", 18, "19999999999", "上海市");
User u4 = new User("赵六", "女", 34, "15555555555", "广州市");
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
return list;
}
//4:写出方法,sheet2是写出sheet页的名字,括号里面的data是需要写出的数据
public static void write(){
List<User> date = getDate();
String fileName = "D:\\test.xlsx";
EasyExcel.write(fileName, User.class)
.sheet("sheet2")
.doWrite(date);
}
//5读入------------------------------
//读取excel
public static void read(){
String fileName = "D:\\test.xlsx";
EasyExcel.read(fileName, User.class, new UserExcelListener())
.sheet("sheet2")
.doRead();
}
//读取excel,读取监听器 (必须要有这个监听器的)
public static void read(){
String fileName = "D:\\test.xlsx";
EasyExcel.read(fileName, User.class, new UserExcelListener())
.sheet("sheet2")
.doRead();
}
更多的例子 可以到语雀官网https://www.yuque.com/easyexcel/doc/easyexcel