easyExcel 所需的maven 依赖
easyExcel 所需的maven 依赖
补充:这些依赖不是都需要导入,我这里用到了xlsx这个表格,所以需要poi-ooxml
详情可以看这篇博客,根据需要去导入依赖,不然很有可能冲突,读写不了数据
POI各Jar包的作用 - 秋心无波 - 博客园 (cnblogs.com)
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.6</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apche.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
1. 写excel
package com.example.demo.excel; import com.alibaba.excel.EasyExcel; import java.util.ArrayList; import java.util.List; public class TestEasyExcel { public static void main(String[] args) { //1.设置文件夹的地址和名称 String filename = "E:\\easyExcelDocument\\write.xlsx"; //2.调用easyExcel里的方法 EasyExcel.write(filename,DemoData.class).sheet("学生列表").doWrite(getData()); } //上面的静态方法,不能调用非静态方法,因此得声明此方法为静态方法 private static List<DemoData> getData(){ List<DemoData> list = new ArrayList<>(); for (int i = 0; i < 10000; i++) { DemoData data = new DemoData(); data.setSno(i); data.setSname("学生"+i); list.add(data); } return list; } }
1.1 实体类
package com.example.demo.excel; import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data; @Data public class DemoData { //设置表头名称 @ExcelProperty("学生编号") private Integer sno; @ExcelProperty("学生姓名") private String sname; }
2. 读操作(写的话直接写就行,但读取excel得额外新建监听器,实现相应的方法)
2.1 实体类
package com.example.demo.excel; import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data; @Data public class DemoData { //设置表头名称 @ExcelProperty(value = "学生编号",index = 0) private Integer sno; @ExcelProperty(value = "学生姓名",index = 1) private String sname; } 增加index属性
2.2 监听器(用于处理read 执行前后的操作)
package com.example.demo.excel; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import java.util.Map; public class ExcelListener extends AnalysisEventListener<DemoData> { //一行一行的读取excel内容 @Override public void invoke(DemoData demoData, AnalysisContext analysisContext) { System.out.println("****"+demoData); //数据太多的话控制台会自动清除 } //读取表头内容 @Override public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { System.out.println("表头"+headMap); } //读取完成之后 @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }
3. 测试
package com.example.demo.excel; import com.alibaba.excel.EasyExcel; import java.util.ArrayList; import java.util.List; public class TestEasyExcel { public static void main(String[] args) { //***********写操作 // //1.设置文件夹的地址和名称 // String filename = "E:\\easyExcelDocument\\write.xlsx"; // // //2.调用easyExcel里的方法 // EasyExcel.write(filename,DemoData.class).sheet("学生列表").doWrite(getData()); //**********读操作 String filename = "E:\\easyExcelDocument\\write.xlsx"; EasyExcel.read(filename,DemoData.class,new ExcelListener()).sheet("学生列表").doRead(); // System.out.println(EasyExcel.read(filename)); } //上面的静态方法,不能调用非静态方法,因此得声明此方法为静态方法 private static List<DemoData> getData(){ List<DemoData> list = new ArrayList<>(); for (int i = 0; i < 10000; i++) { DemoData data = new DemoData(); data.setSno(i); data.setSname("学生"+i); list.add(data); } return list; } }