EasyExcel的基本使⽤⼀、使⽤EasyExcel 读取excel数据并转换成对象
1、使⽤的maven依赖及版本:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</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-scratchpad</artifactId>
<version>3.17</version>
</dependency>
2、代码如下:将⽤到三个类,MyExcelListener.java,StudentInfo.java,ExcelUtil.java StudentInfo.java
@Getter
@Setter
public class StudentInfo implements Serializable {
private static final long serialVersionUID = 7566235228027597205L;
private String studnetName;
private String gender;
private String classes;
private double score;
}
ExcelListener.java
ExcelListener.java
public static class ExcelListener extends AnalysisEventListener<Object> {
private int headSize;    //头部⼤⼩,⼀般是指对象的总属性
private List<Object> datas = new ArrayList<>();  // excel⽂档的内容存储
/**
* 逐⾏解析
* object : 当前⾏的数据
*/
@Override
public void invoke(Object object, AnalysisContext context) {
if (object != null) {
datas.add(object);
}
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {      super.invokeHeadMap(headMap, context);
headSize = headMap.size();
}
public int getHeadSize() {
return headSize;
}
public void setHeadSize(int headSize) {
this.headSize = headSize;
}
/**
* 解析完所有数据后会调⽤该⽅法
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
fastjson怎么用
//销毁不⽤的资源
}
public List<Object> getDatas() {
return datas;
}
public void setDatas(List<Object> datas) {
this.datas = datas;
}
}
3、ExcelUtil.java 包含两个主要⽅法 [1]读取excel的数据、[2]解析成对象package form.office.easyexcel;
import l.EasyExcelFactory;
import l.ExcelReader;
import l.ExcelWriter;
import l.context.AnalysisContext;
import l.event.AnalysisEventListener;
import adata.ReadSheet;
import adata.WriteSheet;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.*;
import flect.Field;
import java.util.*;
public class ExcelUtil {
private static ReadSheet initReadSheet;
private static WriteSheet initWriteSheet;
static {
initReadSheet = new ReadSheet(1,"sheet");
initWriteSheet = new WriteSheet();
initWriteSheet.setSheetName("sheet");
initWriteSheet.setSheetNo(1);
}
/*
* 1、读取excel
* @param filePath
* @param sheet
* @return
*/
public static List<Object> readRow(String filePath, ReadSheet sheet){
if(!StringUtils.hasText(filePath)){
return null;
}
sheet =  sheet != null ?sheet :initReadSheet;
InputStream fileStream = null;
ExcelListener excelListener = new ExcelListener();
ExcelReader excelReader = null;
try{
fileStream = new FileInputStream(filePath);
excelReader = ad(fileStream, excelListener).autoTrim(true).doReadAll();      Datas();
}catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try{
if(fileStream != null){
fileStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 2、转换成对象
* @param classPath  映射模型的类的全路径
* @param objectList excel内容
* @return
*/
private static JSONArray dataReflet(String classPath, List<Object> objectList) {
JSONArray jsonArr = new JSONArray();
try {
Class<?> clazz = Class.forName(classPath);
Field[] importField = DeclaredFields();
for (Object obj : objectList) {
Map<Integer,String>  map = (LinkedHashMap)obj;
String[] values = map.values().toArray(new String[0]);
JSONObject jsonObj = new JSONObject();
for (int i = 0; i <= values.length; i++) {
Field field = importField[i];
/
/ 字段为 serialVersionUID时不读取
if ("serialVersionUID".Name())) {
continue;
}else{
jsonObj.Name(),values[i-1]);
}
}
jsonArr.add(jsonObj);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return jsonArr;
}
/**
* 3) 测试
*
*/
public static void main(String[] args) {
// 数据导⼊
String filePath = "D:\\software\\study\\data\\ExcelImport.xlsx";
readRow(filePath,null);
List<Object> objectList = adRow(filePath, null);
String classPath = "form.office.easyexcel.StudentInfo";
JSONArray jsonArr = dataReflet(classPath, objectList);
List<StudentInfo> dtoList = new ArrayList<>();
dtoList = JSONArray.JSONString(), StudentInfo.class);
System.out.String());
}
测试结果如下所⽰:
[{"classes":"1","gender":"男","score":0.0,"studnetName":"张三"}, {"classes":"2","score":89.12,"studnetName":"李四"}, {"classes":"1","gender":"男","score":96.14,"stu

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。