2.0.5版本做了很大改变,记录2.1.6的所使用的工具类及方法

其实持续对easyexcel的git进行关注是最方便的,上面也有完整的demo以及工具类等等

1.easyExcel的pom坐标

 <!-- easyexcel --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.6</version></dependency>

2.easyExcel工具类

package com.chinargb.baseadmin.util;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;/*** @author: * @date: * @description:*/
public class EasyExcelUtil
{/*** 同步无模型读(默认读取sheet0,从第2行开始读)* @param filePath* @return*/public static List<Map<Integer, String>> syncRead(String filePath){return EasyExcelFactory.read(filePath).sheet().doReadSync();}/*** 同步无模型读(默认表头占一行,从第2行开始读)* @param filePath* @param sheetNo sheet页号,从0开始* @return*/public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo){return EasyExcelFactory.read(filePath).sheet(sheetNo).doReadSync();}/*** 同步无模型读(指定sheet和表头占的行数)* @param inputStream* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)* @return List<Map<colNum, cellValue>>*/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)* @return List<Map<colNum, cellValue>>*/public static List<Map<Integer, String>> syncRead(File file, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();}/*** 同步无模型读(指定sheet和表头占的行数)* @param filePath* @param sheetNo sheet页号,从0开始* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)* @return List<Map<colNum, cellValue>>*/public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo, Integer headRowNum){return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();}/*** 同步按模型读(默认读取sheet0,从第2行开始读)* @param filePath* @param clazz 模型的类类型(excel数据会按该类型转换成对象)* @return*/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开始* @return*/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)* @return*/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)* @return*/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)* @return*/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)* @return*/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开始* @return*/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)* @return*/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)* @return*/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* @return*/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* @return*/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* @return*/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 导出的文件名称* @return*/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;}
}/*** 默认按模型读取的监听器* @param <T>*/
@Slf4j
class DefaultExcelListener<T> extends AnalysisEventListener<T>
{private final List<T> rows = new ArrayList();@Overridepublic void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {log.info("解析到一条头数据:{}", JSON.toJSONString(headMap));}@Overridepublic void invoke(T object, AnalysisContext context) {rows.add(object);// 实际数据量比较大时,rows里的数据可以存到一定量之后进行批量处理(比如存到数据库),// 然后清空列表,以防止内存占用过多造成OOM}@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {log.info("read {} rows", rows.size());}/*** 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。* @param exception* @param context* @throws Exception*/@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());}}public List<T> getRows() {return rows;}
}

3.EasyExcel工厂类

package com.chinargb.baseadmin.util;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;import java.io.File;
import java.io.OutputStream;
import java.util.List;/*** 自定义EasyExcel写工厂*/
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();}
}

4.模板模型对象

package com.chinargb.baseadmin.util.excelModel;import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
import lombok.EqualsAndHashCode;/*** excel模型对象*/
@EqualsAndHashCode(callSuper = true)
@Data
public class TableHeaderExcelProperty extends BaseRowModel {/*** value: 表头名称* index: 列的号, 0表示第一列*/@ExcelProperty(value = "姓名", index = 0)private String name;@ExcelProperty(value = "年龄",index = 1)private int age;@ExcelProperty(value = "学校",index = 2)private String school;
}

5.excel导入测试及数据结果

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseAdminApplicationTests {@Testpublic void easyExcelTest() {String FilePath="D:/学生信息.xlsx";List<T> tList = EasyExcelUtil.syncReadModel(FilePath, TableHeaderExcelProperty.class);System.out.println(tList);}
}

EasyExcel项目使用相关推荐

  1. SpringBoot 项目实现 Excel 导入导出功能

    背景 Excel 导入与导出是项目中经常用到的功能,在 Java 中常用 poi 实现 Excel 的导入与导出.由于 poi 占用内存较大,在高并发下很容易发生 OOM 或者频繁 fullgc,阿里 ...

  2. EasyExcel web下载excel,多sheet页demo

    EasyExcel web下载excel,多sheet页demo pom.xml <dependency><groupId>com.alibaba</groupId> ...

  3. Excel解析easyexcel工具类

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

  4. Java操作Excel三种方式POI、Hutool、EasyExcel

    Java操作Excel三种方式POI.Hutool.EasyExcel 1. Java操作Excel概述 1.1 Excel需求概述 1.2 Excel操作三种方式对比 2. ApachePOIExc ...

  5. 阿里开源项目合集,你值得拥有

    写在前面 拿走不谢,顺手留个关注和点个赞,嘿嘿嘿,废话不多说,直接上资源. 分布式应用服务开发的一站式解决方案 项目地址 分布式应用服务开发的一站式解决方案 Spring Cloud AlibabaS ...

  6. springboot项目实现excel导出

    项目中经常会有列表查询,然后导出excel的功能,以下是其中一种方法,简单写个Demo ,先看项目结构: pom.xml <properties><spring-boot.versi ...

  7. SpringBoot 项目优雅实现 Excel 导入导出功能

    背景 Excel 导入与导出是项目中经常用到的功能,在 Java 中常用 poi 实现 Excel 的导入与导出.由于 poi 占用内存较大,在高并发下很容易发生 OOM 或者频繁 fullgc,阿里 ...

  8. 清理easyexcel导出07版Excel时产生的poi-sxssf-sheet*.xml临时文件

    最新编辑 临时文件问题已经在官方2.x版本修复了,此文档不具有参考性了 事件起源 因为生产环境每天有大量的Excel文件导出,以前是导出03版,但是03版的每个sheet行数有限而且同样的数据文件本身 ...

  9. 一行代码完成Java的Excel读写 侵立删

    转自:https://mp.weixin.qq.com/s/X9341NNXZe0tkbQu4xbi8Q 前段时间在 github 上发现了阿里的 EasyExcel 项目,觉得挺不错的,就写了一个简 ...

最新文章

  1. 院士谈如何做好研究生:要逼着自己去想问题,最大的浪费是聪明人不思考
  2. python基础学习语法和函数
  3. @Value 注解获取properties值
  4. boost::mp11::mp_intersperse相关用法的测试程序
  5. 如何在Windows上使用Git创建一个可执行脚本?
  6. ul li横向排列及圆点处理
  7. sdl2和ffmpeg显示摄像头数据
  8. matplotlib绘制K线图
  9. CentOS6离线bash漏洞—再修复方法
  10. linux下运行jar
  11. 互亿无线短信接口开发
  12. 拼多多上架助手用哪个?拼多多商家必备工具
  13. 剑指Offer——完美+今日头条笔试题+知识点总结
  14. 世界33种名车标志及来历
  15. python浏览器复制粘贴到word里(带格式的)
  16. 接口限流算法(关于临界点处理)
  17. 将一个数组分成2个数组,使得2个数组的差值最小
  18. 埃睿迪展台热度持续 为绿色城市建设进言献策
  19. C语言编程:三(n)子棋游戏
  20. 3、JavaWeb中Service层的作用、MyBatis的重要组件、mybatis-config.xml中的别名映射、properties配置、#{}和${}的区别、获取插入数据的主键值

热门文章

  1. 如何用python计算levenshteindistance_Levenshtein计算相似度距离
  2. 根据工序画出aoe网_这些金刚网纱窗竟然含“毒”!选错就得病
  3. 椭圆极点极线性质_又见阿氏圆——适合作椭圆大题的小题
  4. MySQL查询语句转postGRE语句_PostgreSQL DBA常用SQL查询语句
  5. Java 多个异常处理
  6. Java Integer类详解
  7. IDEA里的web.xml页面的Servlet名称报错下方出现红色下划线
  8. JAVA50道经典编程题
  9. python 自动化出报表,python实现报表自动化详解
  10. Python小白的数学建模课-05.0-1规划