# EasyExcel
# 读数据
| package com.excel; |
| |
| import com.alibaba.excel.EasyExcel; |
| import com.alibaba.excel.ExcelReader; |
| import com.alibaba.excel.context.AnalysisContext; |
| import com.alibaba.excel.event.AnalysisEventListener; |
| import com.alibaba.excel.read.builder.ExcelReaderBuilder; |
| import com.alibaba.excel.support.ExcelTypeEnum; |
| @SuppressWarnings({"all"}) |
| public class Easy_Exceltest { |
| public static void main(String[]args){ |
| |
| |
| ExcelReaderBuilder builder = EasyExcel.read(); |
| |
| builder.file("C:\\Users\\Administrator\\Desktop\\新建文件夹\\练习时长两年半.xlsx"); |
| |
| builder.sheet("Sheet1"); |
| |
| builder.autoCloseStream(true); |
| |
| builder.excelType(ExcelTypeEnum.XLSX); |
| |
| builder.registerReadListener(new AnalysisEventListener() { |
| @Override |
| public void invoke(Object o, AnalysisContext analysisContext) { |
| System.out.println(o); |
| } |
| @Override |
| public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
| System.out.println("读取完毕!"); |
| } |
| }); |
| |
| ExcelReader reader = builder.build(); |
| |
| reader.readAll(); |
| |
| reader.finish(); |
| } |
| } |
可以通过泛型指定数据类型,但是还是 Map 集合
| builder.registerReadListener(new AnalysisEventListener<Map<Integer,String>>() { |
| @Override |
| public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) { |
| Set<Map.Entry<Integer,String>> entryset = integerStringMap.entrySet(); |
| Iterator<Map.Entry<Integer,String>> iterator = entryset.iterator(); |
| while(iterator.hasNext()){ |
| Map.Entry<Integer,String> ar = iterator.next(); |
| String name = ar.getValue(); |
| Integer id = ar.getKey(); |
| System.out.print(id+" : "+name+", "); |
| } |
| System.out.println(""); |
| } |
| @Override |
| public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
| System.out.println("读取完毕!"); |
| } |
| }); |
对上述代码进行简化
| package com.excel; |
| |
| import com.alibaba.excel.EasyExcel; |
| import com.alibaba.excel.context.AnalysisContext; |
| import com.alibaba.excel.event.AnalysisEventListener; |
| |
| import java.util.*; |
| |
| public class Easy_EasyExcelone { |
| public static void main(String[]args){ |
| List<Map<Integer,String>> list = new ArrayList<>(); |
| EasyExcel.read("C:\\Users\\Administrator\\Desktop\\新建文件夹\\练习时长两年半.xlsx").sheet("Sheet1").registerReadListener(new AnalysisEventListener<Map<Integer,String>>() { |
| @Override |
| public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) { |
| list.add(integerStringMap); |
| } |
| @Override |
| public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
| System.out.println("数据读取完毕"); |
| } |
| }).doRead(); |
| for(Map<Integer,String> i : list){ |
| Set set = i.keySet(); |
| Iterator<Integer> iterator = set.iterator(); |
| while(iterator.hasNext()){ |
| Integer key = iterator.next(); |
| System.out.print(key+" : "+i.get(key)+", "); |
| } |
| System.out.println(""); |
| } |
| } |
| } |
映射成指定对象,需要创建实体类,使用 @ExcelProperty 注解完成实体类成员变量和 Excel 字段的映射
导入 pom.xml
| <dependencies> |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>easyexcel</artifactId> |
| <version>2.2.6</version> |
| </dependency> |
| #-----------------导入如下内容到作用域中------------------ |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <version>1.18.16</version> |
| </dependency> |
| #----------------------------------------------------- |
| </dependencies> |
| import com.alibaba.excel.annotation.ExcelProperty; |
| public class EasyExcel_test { |
| @ExcelProperty("name") |
| private String name; |
| @ExcelProperty("id") |
| private Integer id; |
| @ExcelProperty("address") |
| private String address; |
| @ExcelProperty("uid") |
| private Integer uid; |
| } |
解析 Excel 文件的时候直接指定实体类即可
实体类
| package com.excel; |
| |
| import com.alibaba.excel.annotation.ExcelProperty; |
| import lombok.Data; |
| |
| @Data |
| public class EasyExcel_test { |
| @ExcelProperty("name") |
| private String name; |
| @ExcelProperty("id") |
| private Integer id; |
| @ExcelProperty("address") |
| private String address; |
| @ExcelProperty("uid") |
| private Integer uid; |
| } |
解析 Excel 类
| package com.excel; |
| |
| import com.alibaba.excel.EasyExcel; |
| import com.alibaba.excel.context.AnalysisContext; |
| import com.alibaba.excel.event.AnalysisEventListener; |
| |
| import java.util.*; |
| |
| public class Easy_EasyExcelone { |
| public static void main(String[]args){ |
| List<EasyExcel_test> list = new LinkedList<>(); |
| EasyExcel.read("C:\\Users\\Administrator\\Desktop\\新建文件夹\\练习时长两年半.xlsx") |
| .head(EasyExcel_test.class) |
| .sheet("Sheet1") |
| .registerReadListener(new AnalysisEventListener<EasyExcel_test>() { |
| @Override |
| public void invoke(EasyExcel_test easyExcel_test, AnalysisContext analysisContext) { |
| list.add(easyExcel_test); |
| } |
| @Override |
| public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
| System.out.println("数据读取完毕"); |
| } |
| }).doRead(); |
| for (EasyExcel_test i : list) { |
| System.out.println(i); |
| } |
| } |
| } |
# 写数据
| package com.excel; |
| |
| import com.alibaba.excel.EasyExcel; |
| import com.alibaba.excel.context.AnalysisContext; |
| import com.alibaba.excel.event.AnalysisEventListener; |
| import com.alibaba.excel.support.ExcelTypeEnum; |
| |
| import java.util.LinkedList; |
| import java.util.List; |
| |
| public class WriteExcel { |
| public static void main(String[]args){ |
| |
| List<EasyExcel_test> list = new LinkedList<>(); |
| |
| EasyExcel.write("C:\\Users\\Administrator\\Desktop\\新建文件夹\\练习时长两年半(2).xlsx") |
| |
| .head(EasyExcel_test.class) |
| |
| .excelType(ExcelTypeEnum.XLSX) |
| |
| .sheet("Sheet1") |
| |
| .doWrite(read()); |
| } |
| |
| public static List<EasyExcel_test> read(){ |
| |
| List<EasyExcel_test> list = new LinkedList<>(); |
| |
| EasyExcel.read("C:\\Users\\Administrator\\Desktop\\新建文件夹\\练习时长两年半.xlsx") |
| |
| .head(EasyExcel_test.class) |
| |
| .sheet("Sheet1") |
| |
| .registerReadListener(new AnalysisEventListener<EasyExcel_test>(){ |
| |
| @Override |
| public void invoke(EasyExcel_test easyExcel_test, AnalysisContext analysisContext) { |
| list.add(easyExcel_test); |
| } |
| |
| @Override |
| public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
| System.out.println("读取完成"); |
| } |
| }).doRead(); |
| return list; |
| } |
| } |