所需依赖

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.6</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency>

只需将下面三个类copy进项目、即可实现Excel的导入导出功能

注:第二个和第三个类,非必须的,只不过是对功能加强,工具类里面的方法部分依赖下面两个类。(为工具类完整性,最好添加进项目里)
1:EasyExcelUtil Excel工具类
2:DefaultExcelListener 异步监听对Excel操作
3:EasyExcelWriterFactory 链式导出多个sheet的Excel

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.write.handler.WriteHandler;
import org.apache.poi.ss.formula.functions.T;import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*** Author:         ww* Datetime:       2020\8\12 0012* Description:    注:如果方法中无以下参数,则使用默认值*                    Integer sheetNo       默认0读取第一张sheet*                    Integer headRowNum    默认1不读取首行,从第二行开始读取*                 返回值return:List<Map<Integer,String>>: Integer:列数  String:列数对应的value*/
public class EasyExcelUtil {/*** 同步无模型读(默认读取sheet0,从第2行开始读)* @param filePath excel文件的绝对路径*/public static List<Map<Integer, String>> syncRead(String filePath){return EasyExcelFactory.read(filePath).sheet().doReadSync();}/*** 同步无模型读(自定义读取sheetX,从第2行开始读)* @param filePath excel文件的绝对路径* @param sheetNo sheet页号,从0开始*/public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo){return EasyExcelFactory.read(filePath).sheet(sheetNo).doReadSync();}/*** 同步无模型读(指定sheet和表头占的行数)* @param filePath* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();}/*** 同步无模型读(指定sheet和表头占的行数)* @param inputStream* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static List<Map<Integer, String>> syncRead(InputStream inputStream, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();}/*** 同步无模型读(指定sheet和表头占的行数)* @param file* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static List<Map<Integer, String>> syncRead(File file, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();}
//====================================================无JAVA模型读取excel数据===============================================================//====================================================将excel数据同步到JAVA模型属性里===============================================================/*** 同步按模型读(默认读取sheet0,从第2行开始读)* @param filePath* @param clazz 模型的类类型(excel数据会按该类型转换成对象)*/public static List<T> syncReadModel(String filePath, Class clazz){return EasyExcelFactory.read(filePath).sheet().head(clazz).doReadSync();}/*** 同步按模型读(默认表头占一行,从第2行开始读)* @param filePath* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo sheet页号,从0开始*/public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo){return EasyExcelFactory.read(filePath).sheet(sheetNo).head(clazz).doReadSync();}/*** 同步按模型读(指定sheet和表头占的行数)* @param inputStream* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static List<T> syncReadModel(InputStream inputStream, Class clazz, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();}/*** 同步按模型读(指定sheet和表头占的行数)* @param file* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static List<T> syncReadModel(File file, Class clazz, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();}/*** 同步按模型读(指定sheet和表头占的行数)* @param filePath* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();}/*** 异步无模型读(默认读取sheet0,从第2行开始读)* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener){EasyExcelFactory.read(filePath, excelListener).sheet().doRead();}/*** 异步无模型读(默认表头占一行,从第2行开始读)* @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param sheetNo sheet页号,从0开始*/public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo){EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).doRead();}/*** 异步无模型读(指定sheet和表头占的行数)* @param inputStream* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static void asyncRead(InputStream inputStream, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum){EasyExcelFactory.read(inputStream, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();}/*** 异步无模型读(指定sheet和表头占的行数)* @param file* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static void asyncRead(File file, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum){EasyExcelFactory.read(file, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();}/*** 异步无模型读(指定sheet和表头占的行数)* @param filePath* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)* @return*/public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum){EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();}/*** 异步按模型读取(默认读取sheet0,从第2行开始读)* @param filePath* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param clazz 模型的类类型(excel数据会按该类型转换成对象)*/public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz){EasyExcelFactory.read(filePath, clazz, excelListener).sheet().doRead();}/*** 异步按模型读取(默认表头占一行,从第2行开始读)* @param filePath* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo  sheet页号,从0开始*/public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo){EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).doRead();}/*** 异步按模型读取* @param inputStream* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo  sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static void asyncReadModel(InputStream inputStream, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum){EasyExcelFactory.read(inputStream, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();}/*** 异步按模型读取* @param file* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo  sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static void asyncReadModel(File file, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum){EasyExcelFactory.read(file, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();}/*** 异步按模型读取* @param filePath* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @param sheetNo  sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)*/public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum){EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();}/*** 无模板写文件* @param filePath* @param head 表头数据* @param data 表内容数据*/public static void write(String filePath, List<List<String>> head, List<List<Object>> data){EasyExcel.write(filePath).head(head).sheet().doWrite(data);}/*** 无模板写文件* @param filePath* @param head 表头数据* @param data 表内容数据* @param sheetNo sheet页号,从0开始* @param sheetName sheet名称*/public static void write(String filePath, List<List<String>> head, List<List<Object>> data, Integer sheetNo, String sheetName){EasyExcel.write(filePath).head(head).sheet(sheetNo, sheetName).doWrite(data);}/*** 根据excel模板文件写入文件* @param filePath* @param templateFileName* @param headClazz* @param data*/public static void writeTemplate(String filePath, String templateFileName, Class headClazz, List data){EasyExcel.write(filePath, headClazz).withTemplate(templateFileName).sheet().doWrite(data);}/*** 根据excel模板文件写入文件* @param filePath* @param templateFileName* @param data*/public static void writeTemplate(String filePath, String templateFileName, List data){EasyExcel.write(filePath).withTemplate(templateFileName).sheet().doWrite(data);}/*** 按模板写文件* @param filePath* @param headClazz 表头模板* @param data 数据*/public static void write(String filePath, Class headClazz, List data){EasyExcel.write(filePath, headClazz).sheet().doWrite(data);}/*** 按模板写文件* @param filePath* @param headClazz 表头模板* @param data 数据* @param sheetNo sheet页号,从0开始* @param sheetName sheet名称*/public static void write(String filePath, Class headClazz, List data, Integer sheetNo, String sheetName){EasyExcel.write(filePath, headClazz).sheet(sheetNo, sheetName).doWrite(data);}/*** 按模板写文件* @param filePath* @param headClazz 表头模板* @param data 数据* @param writeHandler 自定义的处理器,比如设置table样式,设置超链接、单元格下拉框等等功能都可以通过这个实现(需要注册多个则自己通过链式去调用)* @param sheetNo sheet页号,从0开始* @param sheetName sheet名称*/public static void write(String filePath, Class headClazz, List data, WriteHandler writeHandler, Integer sheetNo, String sheetName){EasyExcel.write(filePath, headClazz).registerWriteHandler(writeHandler).sheet(sheetNo, sheetName).doWrite(data);}/*** 按模板写文件(包含某些字段)* @param filePath* @param headClazz 表头模板* @param data 数据* @param includeCols 包含字段集合,根据字段名称显示* @param sheetNo sheet页号,从0开始* @param sheetName sheet名称*/public static void writeInclude(String filePath, Class headClazz, List data, Set<String> includeCols, Integer sheetNo, String sheetName){EasyExcel.write(filePath, headClazz).includeColumnFiledNames(includeCols).sheet(sheetNo, sheetName).doWrite(data);}/*** 按模板写文件(排除某些字段)* @param filePath* @param headClazz 表头模板* @param data 数据* @param excludeCols 过滤排除的字段,根据字段名称过滤* @param sheetNo sheet页号,从0开始* @param sheetName sheet名称*/public static void writeExclude(String filePath, Class headClazz, List data, Set<String> excludeCols, Integer sheetNo, String sheetName){EasyExcel.write(filePath, headClazz).excludeColumnFiledNames(excludeCols).sheet(sheetNo, sheetName).doWrite(data);}/*** 多个sheet页的数据链式写入* ExcelUtil.writeWithSheets(outputStream)*                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")*                 .write(headData, data,"sheetName2")*                 .finish();* @param outputStream*/public static EasyExcelWriterFactory writeWithSheets(OutputStream outputStream){EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(outputStream);return excelWriter;}/*** 多个sheet页的数据链式写入* ExcelUtil.writeWithSheets(file)*                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")*                 .write(headData, data,"sheetName2")*                 .finish();* @param file*/public static EasyExcelWriterFactory writeWithSheets(File file){EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(file);return excelWriter;}/*** 多个sheet页的数据链式写入* ExcelUtil.writeWithSheets(filePath)*                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")*                 .write(headData, data,"sheetName2")*                 .finish();* @param filePath*/public static EasyExcelWriterFactory writeWithSheets(String filePath){EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(filePath);return excelWriter;}/*** 多个sheet页的数据链式写入(失败了会返回一个有部分数据的Excel)* ExcelUtil.writeWithSheets(response, exportFileName)*                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")*                 .write(headData, data,"sheetName2")*                 .finish();* @param response* @param exportFileName 导出的文件名称*/public static EasyExcelWriterFactory writeWithSheetsWeb(HttpServletResponse response, String exportFileName) throws IOException{response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码String fileName = URLEncoder.encode(exportFileName, "UTF-8");response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(response.getOutputStream());return excelWriter;}
}

二、DefaultExcelListener

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** Author:         ww* Datetime:       2020\8\12 0012* Description:    excel读取监听器*/
@Slf4j
public class DefaultExcelListener<T> extends AnalysisEventListener<T> {private final List<T> rows = new ArrayList();/*** 读取excel数据前操作(只有不读取表头数据时才会触发此方法)*/@Overridepublic void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {log.info("======================================================");log.info("解析第一行数据:{}"+ JSON.toJSONString(headMap));log.info("======================================================");}/*** 读取excel数据操作* @param object* @param context*/@Overridepublic void invoke(T object, AnalysisContext context) {rows.add(object);log.info("list容量"+rows.size()+"---"+object);/** 数据量不是特别大,可以不需要打开// 实际数据量比较大时,rows里的数据可以存到一定量之后进行批量处理(比如存到数据库),// 然后清空列表,以防止内存占用过多造成OOMif(rows.size() >= 500){log.info("存入数据库ing");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}rows.clear();}*/}/*** 读取完excel数据后的操作*/@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {log.info("成功读取【"+ rows.size()+"】条数据");}/*** 在读取excel异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。*/@Overridepublic void onException(Exception exception, AnalysisContext context) {log.error("解析失败,但是继续解析下一行:{}", exception.getMessage());if (exception instanceof ExcelDataConvertException) {ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception;log.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(),excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());}}/*** @return 返回读取excel总数据*/public List<T> getRows() {return rows;}
}

三、EasyExcelWriterFactory

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;import java.io.File;
import java.io.OutputStream;
import java.util.List;
/*** Author:         ww* Datetime:       2020\8\12 0012* Description:    链式添加sheet表*/
public class EasyExcelWriterFactory {private int sheetNo = 0;private ExcelWriter excelWriter = null;public EasyExcelWriterFactory(OutputStream outputStream) {excelWriter = EasyExcel.write(outputStream).build();}public EasyExcelWriterFactory(File file) {excelWriter = EasyExcel.write(file).build();}public EasyExcelWriterFactory(String filePath) {excelWriter = EasyExcel.write(filePath).build();}/*** 链式模板表头写入* @param headClazz 表头格式* @param data 数据 List<ExcelModel> 或者List<List<Object>>* @return*/public EasyExcelWriterFactory writeModel(Class headClazz, List data, String sheetName){excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(headClazz).build());return this;}/*** 链式自定义表头写入* @param head* @param data 数据 List<ExcelModel> 或者List<List<Object>>* @param sheetName* @return*/public EasyExcelWriterFactory write(List<List<String>> head, List data, String sheetName){excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(head).build());return this;}/*** 使用此类结束后,一定要关闭流*/public void finish() {excelWriter.finish();}
}

测试代码

注:为方便演示,就拿参数为文件路径的方法演示

将测试实体类导入项目

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** Author:         ww* Datetime:       2020\8\12 0012* Description:    测试模型*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ScoreData {@ExcelProperty(value = "学号", index = 0)private int id;@ExcelProperty(value = "年级", index = 1)private String senior;@ExcelProperty(value = "学生", index = 2)private String name;@ExcelProperty(value = "语文", index = 3)private int chinaese;@ExcelProperty(value = "英语", index = 4)private int english;@ExcelProperty(value = "数学", index = 5)private int math;@ExcelProperty(value = "综合科", index = 6)private int comprehensive;
}

首先先通过测试方法导出生成一个Excel表数据,用来测试Excel的导入

 @Testpublic void test8(){//生成用来测试导出读取excel数据的成绩汇总表String fileName = UUID.randomUUID().toString();
//        String path = "C:\\Users\\Administrator\\Desktop\\"+fileName+".xls";    //导出路径String path = "C:\\Users\\Administrator\\Desktop\\成绩汇总表.xls";    //导出路径List<ScoreData> senior1 = new ArrayList<>();List<ScoreData> senior2 = new ArrayList<>();List<ScoreData> senior3 = new ArrayList<>();Random random = new Random();//初始化成绩表数据for(int i = 1;i<=1000;i++){ScoreData score1 = new ScoreData(i,"高一","学生"+i,random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(130)+20,random.nextInt(200)+100);ScoreData score2 = new ScoreData(i,"高二","学生"+i,random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(130)+20,random.nextInt(200)+100);ScoreData score3 = new ScoreData(i,"高三","学生"+i,random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(130)+20,random.nextInt(200)+100);senior1.add(score1);senior2.add(score2);senior3.add(score3);}//导出Excel,链式添加sheet(使用此方法前提要将上文提到的三个类到导入项目)EasyExcelWriterFactory res = EasyExcelUtil.writeWithSheets(path).writeModel(ScoreData.class, senior1, "高一成绩表").writeModel(ScoreData.class, senior2, "高二成绩表").writeModel(ScoreData.class, senior3, "高三成绩表");res.finish(); //关闭流,不然会报错}

测试Excel的导入读取数据

** 读取Excel数据测试类(不关联实体类) **

/*** Author:         ww* Datetime:       2020\8\12 0012* Description:*/
public class ExcelDemo {private String filePath = "C:\\Users\\Administrator\\Desktop\\成绩汇总表.xls";@Testpublic void test1(){//同步读取excel数据(无实体类模型)//参数1:文件绝对路径      参数2:Excel的第几个sheet 0:第一个     参数3:从第几行开始读取 0:第一行         List<Map<Integer, String>> data = EasyExcelUtil.syncRead(filePath,0,0);for(Iterator it = data.iterator(); it.hasNext();){Map<Integer,String> obj = (Map<Integer, String>) it.next();System.out.println(obj);}}@Testpublic void test2(){//同步读取excel数据(无实体类模型)----读取excel表的高二成绩表数据,从第一行开始读取//参数1:文件绝对路径      参数2:Excel的第几个sheet 0:第一个     参数3:从第几行开始读取 0:第一行List<Map<Integer, String>> data = EasyExcelUtil.syncRead(filePath,1,0);for(Iterator it = data.iterator(); it.hasNext();){Map<Integer,String> obj = (Map<Integer, String>) it.next();System.out.println(obj);}}
}

test1运行结果:

{0=学号, 1=年级, 2=学生, 3=语文, 4=英语, 5=数学, 6=综合科}
{0=1.0, 1=高一, 2=学生1, 3=134.0, 4=113.0, 5=46.0, 6=293.0}
{0=2.0, 1=高一, 2=学生2, 3=129.0, 4=73.0, 5=69.0, 6=256.0}
{0=3.0, 1=高一, 2=学生3, 3=119.0, 4=82.0, 5=129.0, 6=232.0}
{0=4.0, 1=高一, 2=学生4, 3=107.0, 4=85.0, 5=40.0, 6=273.0}
{0=5.0, 1=高一, 2=学生5, 3=132.0, 4=112.0, 5=75.0, 6=106.0}
{0=6.0, 1=高一, 2=学生6, 3=80.0, 4=52.0, 5=141.0, 6=260.0}
..............

测试读取Excel数据(将Excel数据关联实体类)

 @Testpublic void test3(){//同步读取excel数据(实体类模型)----读取excel表的第二张sheet高二成绩表数据,从第一行开始读取List<T> obj = EasyExcelUtil.syncReadModel(filePath, ScoreData.class,1,1);Iterator<T> it = obj.iterator();while(it.hasNext()){System.out.println("TODO插入数据库"+it.next());}}

test3运行结果:

TODO插入数据库ScoreData(id=1, senior=高二, name=学生1, chinaese=85, english=142, math=50, comprehensive=168)
TODO插入数据库ScoreData(id=2, senior=高二, name=学生2, chinaese=90, english=72, math=48, comprehensive=178)
TODO插入数据库ScoreData(id=3, senior=高二, name=学生3, chinaese=141, english=121, math=48, comprehensive=227)
TODO插入数据库ScoreData(id=4, senior=高二, name=学生4, chinaese=134, english=127, math=130, comprehensive=171)
TODO插入数据库ScoreData(id=5, senior=高二, name=学生5, chinaese=149, english=52, math=103, comprehensive=294)
TODO插入数据库ScoreData(id=6, senior=高二, name=学生6, chinaese=127, english=130, math=24, comprehensive=106)
.......

** 测试异步导入读取Excel数据(关联实体类)涉及到监听器类 **

 @Testpublic void test4(){//异步读取excel数据(无实体类模型)----读取excel表的第一张sheet高一成绩表数据,从第一行开始读取EasyExcelUtil.asyncRead(filePath,new DefaultExcelListener<>(),0,1);//异步读取的数据会在监听器类里面处理数据}@Testpublic void test5(){//异步读取excel数据(有实体类模型)----读取excel表的第一张sheet高一成绩表数据,从第一行开始读取EasyExcelUtil.asyncReadModel(filePath,new DefaultExcelListener<>(),ScoreData.class,0,1);//异步读取的数据会在监听器类里面处理数据}

test4测试结果:

09:53:30.019 [main] INFO com.excellence.scm.excel.DefaultExcelListener - ======================================================
09:53:30.019 [main] INFO com.excellence.scm.excel.DefaultExcelListener - 解析第一行数据:{}{0:"学号",1:"年级",2:"学生",3:"语文",4:"英语",5:"数学",6:"综合科"}
09:53:30.019 [main] INFO com.excellence.scm.excel.DefaultExcelListener - ======================================================
09:53:30.024 [main] INFO com.excellence.scm.excel.DefaultExcelListener - list容量1---{0=1.0, 1=高一, 2=学生1, 3=134.0, 4=113.0, 5=46.0, 6=293.0}
09:53:30.025 [main] INFO com.excellence.scm.excel.DefaultExcelListener - list容量2---{0=2.0, 1=高一, 2=学生2, 3=129.0, 4=73.0, 5=69.0, 6=256.0}
...........
09:53:33.236 [main] INFO com.excellence.scm.excel.DefaultExcelListener - 成功读取【1000】条数据

**测试导出Excel **

@Testpublic void test6(){//导出Excel(模拟从数据库查询的数据)String fileName = UUID.randomUUID().toString();String path = "C:\\Users\\Administrator\\Desktop\\"+fileName+".xls";    //导出路径List<ScoreData> scores = new ArrayList<>();Random random = new Random();for(int i = 0; i < 10; i++){ScoreData score1 = new ScoreData(i,"高一","学生"+i,random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(130)+20,random.nextInt(200)+100);scores.add(score1);}EasyExcelUtil.write(path,ScoreData.class,scores,0,"成绩表");}

test6测试结果:

温馨提示:以上使用FilePath作为参数方便演示,实战中应用,可使用参数为File和InputStream的方法(根据需求来使用),常用的方法都已在EasyExcelUtil类里封装好,拿来即用,特殊需求可自行封装。

EasyExcel工具类(开箱即用)相关推荐

  1. easyexcel工具类_阿里巴巴程序员常用的 15 款开发者工具

    从人工到自动化,从重复到创新,技术演进的历程中,伴随着开发者工具类产品的发展. 阿里巴巴将自身在各类业务场景下的技术积淀,通过开源.云上实现或工具等形式对外开放,本文将精选了一些阿里巴巴的开发者工具, ...

  2. easyexcel 工具类_问了个在阿里的同学,他们常用的15款开发者工具!

    来源:jianshu.com/p/58ec32eef2d4 整理自公众号:程序员闪充宝 从人工到自动化,从重复到创新,技术演进的历程中,伴随着开发者工具类产品的发展. 阿里巴巴将自身在各类业务场景下的 ...

  3. easyexcel 工具类_阿里程序员常用的 15 款开发者工具~

    从人工到自动化,从重复到创新,技术演进的历程中,伴随着开发者工具类产品的发展. 阿里巴巴将自身在各类业务场景下的技术积淀,通过开源.云上实现或工具等形式对外开放,本文将精选了一些阿里巴巴的开发者工具, ...

  4. Excel解析easyexcel工具类

    本文使用基于阿里的easyexcel编写的工具类对xls后缀的Excel文件(即03版)读取并写成xlsx后缀的Excel文件(即07版),中间转换过程使用String二维数组和对象列表两种形式. e ...

  5. EasyExcel工具类封装, 做到一个函数完成简单的读取和导出

    目录 工具包目录和依赖 工具类 Service实现 Dto类 Controller实现 工具包目录和依赖 工具包目录 依赖(请根据自己需要自行修改版本) <properties><e ...

  6. easyExcel工具类

    引入pom文件 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</ ...

  7. EasyExcel使用与详细说明,EasyExcel工具类

    文章目录 1.Apache POI 1.1 学习使用成本较高 1.2 POI的内存消耗较大 1.3 特点 2. 初识EasyExcel 2.1 重写了POI对07版Excel的解析 2.2 特点 3. ...

  8. EasyExcel使用的正确姿势,工具类封装

    项目中很可能用到导出excel文件的需求. easyexcel代码量较小,使用简单,而且性能较佳,是一个非常好的选择. 源码地址:https://github.com/alibaba/easyexce ...

  9. Excel工具类 - POI / Easyexcel

    在项目中经常使用Excel 文件做导入导出功能,下面介绍两种经常使用的工具类 Apache POI ,Ali EasyExcel. Excel 分为03 版 (xls), 07版(xlsx),下面介绍 ...

最新文章

  1. python判断英文字母_Python判断两个单词的相似度
  2. 【读书笔记】 网页关闭确认框
  3. python获取输入框内容长度_python3 tkinter 获取输入字符串长度
  4. python绘制不带颜色曲线图_Python数据可视化库-Matplotlib——折线图,子图绘制
  5. AsyncEx - async/await 的辅助库
  6. Linux —— 常用命令集合
  7. File “/usr/bin/yum“, line 30 及 File “/usr/libexec/urlgrabber-ext-down“, line 28
  8. 菲波那契数列编程实现
  9. IOS检查更新的方法
  10. 天下没有免费的午餐 ---- 360 的伪免费
  11. 毕业设计-两轮自平衡小车主控板
  12. 代理模式和装饰模式的区别
  13. Overload 和 Override 的区别
  14. [渝粤教育] 四川轻化工大学 化工设备机械基础 参考 资料
  15. 项目集管理-PgMP
  16. 以太网交换机的用途有哪些?
  17. 字节题--雀魂启动!
  18. 测试论坛和网站资源分享
  19. 从字符串中筛选出连续的数字,并将其放入另一个数组!
  20. 二进制补码是如何把减法转变为加法的

热门文章

  1. 日本超人气洛比(Robi)声控机器人
  2. 论文阅读(10) 基于吸力的推进是动物高效游泳的基础(2015)
  3. 赵雅智_名片夹(4)_Android中listview可折叠伸缩仿手风琴效果(静态)
  4. 薅羊毛软件-抢福袋源码分享
  5. 贵有恒,何必三更起五更眠;最无益,只怕一日曝十日寒
  6. 贵州学计算机,在贵州省计算机学校学习计算机专业如何?
  7. 恢复误删excel工作薄中的表格
  8. redis的几种常见客户端
  9. webservice 实现与his系统对接_一键放牧,一架管 1000 头牛!以色列牧民用大疆无人机实现自主放牛;中国重汽对接北斗卫星导航系统,车辆定位精度达到厘米级别!...
  10. 基于MATLAB的雷达的杂波模拟器