pom文件:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
package com.unicom.common.utils.easyExcel;
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 org.springframework.stereotype.Component; 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; /**
* EasyExcel工具类
*
* @author: WeiJingKun
* @since: 2021-03-09
* @modified:
* @version: 1.0
*/
@Component
public class EasyExcelUtils { /**
* 单例模式
* 通过{@link EasyExcelUtils#getInstance()}获取对象实例
*/
private static volatile EasyExcelUtils easyExcelUtils; /**
* 双检锁保证绝对线程安全
*/
public static EasyExcelUtils getInstance() {
if (null == easyExcelUtils) {
synchronized (EasyExcelUtils.class) {
if (null == easyExcelUtils) {
easyExcelUtils = new EasyExcelUtils();
}
}
}
return easyExcelUtils;
} /**
* 检验文件格式是否是Excel
*
* @param fileName
* @return
*/
public static boolean validateExcelFormat(String fileName) {
boolean result = is2003Excel(fileName) || is2007Excel(fileName);
return result;
} /**
* 验证文件格式是否是 .xls
*
* @param filePath
* @return
*/
public static boolean is2003Excel(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
} /**
* 验证文件格式是否是 .xlsx
*
* @param filePath
* @return
*/
public static boolean is2007Excel(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
} /**
* 构建xls文件
* @param response
* @param exportFileName 需要导出的文件名
* @return
* @throws IOException
*/
public static String encodeFileName2003(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");
fileName = fileName + ".xls";
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
return fileName;
} /**
* 构建xlsx文件
* @param response
* @param exportFileName 需要导出的文件名
* @return
* @throws IOException
*/
public static String encodeFileName2007(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");
fileName = fileName + ".xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
return fileName;
} /**
* 同步无模型读(默认读取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 <T> List<T> syncReadModel(String filePath, Class<T> clazz){
return EasyExcelFactory.read(filePath).sheet().head(clazz).doReadSync();
} /**
* 同步按模型读(默认表头占一行,从第2行开始读)
* @param filePath
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @return
*/
public static <T> List<T> syncReadModel(String filePath, Class<T> clazz, Integer sheetNo){
return EasyExcelFactory.read(filePath).sheet(sheetNo).head(clazz).doReadSync();
} /**
* 同步按模型读(默认表头占一行,从第2行开始读;sheet页号,从0开始)
* @param inputStream
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @return
*/
public static <T> List<T> syncReadModel(InputStream inputStream, Class<T> clazz){
return EasyExcelFactory.read(inputStream).sheet(0).head(clazz).doReadSync();
} /**
* 同步按模型读(指定sheet和表头占的行数)
* @param inputStream
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
* @return
*/
public static <T> List<T> syncReadModel(InputStream inputStream, Class<T> 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 <T> List<T> syncReadModel(File file, Class<T> 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 <T> List<T> syncReadModel(String filePath, Class<T> 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<T> 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<T> 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<T> 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<T> 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<T> 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页的数据链式写入
* EasyExcelUtils.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页的数据链式写入
* EasyExcelUtils.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页的数据链式写入
* EasyExcelUtils.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)
* EasyExcelUtils.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;
} }

EasyExcel工具使用的更多相关文章

  1. Excel解析easyexcel工具类

    Excel解析easyexcel工具类 easyexcel解决POI解析Excel出现OOM <!-- https://mvnrepository.com/artifact/com.alibab ...

  2. Excel映射到实体-easyexcel工具

    来源 项目需要把Excel进行解析,并映射到对象属性,实现类似Mybatis的ORM的效果.使用的方式是自定义注解+POI,这种方式代码复杂而且不易于维护. easyexcel是阿里巴巴开源的一个框架 ...

  3. EasyExcel示例(阿里巴巴)基于Maven

    首先感谢阿里巴巴提供了easyexcel工具类,github地址:https://github.com/alibaba/easyexcel 注意!!这里只是一个简单的示例,VC大法即可使用,对于复杂的 ...

  4. 使用easyexcel时遇到Could not initialize class net.sf.cglib.beans.BeanMap$Generator

    可以访问 这里 查看更多关于大数据平台建设的原创文章. 上一篇文章 Maven项目为什么会产生NoClassDefFoundError的jar包冲突 结合了大量的图解,详细介绍了Maven项目产生ja ...

  5. 阿里的Easyexcel读取Excel文件(最新版本)

      本篇文章主要介绍一下使用阿里开源的Easyexcel工具处理读取excel文件,因为之前自己想在网上找一下这个简单的立即上手的博客,发现很多文章的教程都针对比较旧的版本的Easyexcel,没有使 ...

  6. 史上最全的Excel导入导出之easyexcel

    喝水不忘挖井人,感谢阿里巴巴项目组提供了easyexcel工具类,github地址:https://github.com/alibaba/easyexcel 文章目录 环境搭建 读取excel文件 小 ...

  7. EasyExcel对大数据量表格操作导入导出

    前言 最近有个项目里面中有大量的Excel文档导入导出需求,数据量最多的文档有上百万条数据,之前的导入导出都是用apache的POI,于是这次也决定使用POI,结果导入一个四十多万的文档就GG了,内存 ...

  8. SpringBoot集成MongoDB之导入导出和模板下载

    前言 自己很对自己在项目中集成MongoDb做的导入导出以及模板下载的方法总结如下,有不到之处敬请批评指正! 1.pom.xml依赖引入 <!-- excel导入导出 --> <de ...

  9. SpringBoot 实现 excel 全自由导入导出,性能强的离谱,用起来还特优雅

    一.简介 在实际的业务系统开发过程中,操作 Excel 实现数据的导入导出基本上是个非常常见的需求. 之前,我们有介绍一款非常好用的工具:EasyPoi,有读者提出在数据量大的情况下,EasyPoi ...

  10. Excel解析工具easyexcel全面探索

    1. Excel解析工具easyexcel全面探索 1.1. 简介 之前我们想到Excel解析一般是使用POI,但POI存在一个严重的问题,就是非常消耗内存.所以阿里人员对它进行了重写从而诞生了eas ...

随机推荐

  1. bash 和 zsh 中while循环的方式

    bash: while true; do ./a.out; done zsh: while true; do ./a.out;

  2. linux 操作命令大全

    mysql 授权远程访问 1.进入cd /usr/local/mysql/bin 下执行 ./mysql -uroot -pInfosec@2020  (-p后面是数据库密码) 2.use mysql ...

  3. Too many requests in 1 hour. Try again later.的解决办法

    原因 你的梯子用的人太多了,openAI本身就有问答频率限制. 解决办法 换个相对独立,没那么多人用的梯子,找个"安静点"的地区.

  4. 524. 通过删除字母匹配到字典里最长单词 (Medium)

    问题描述 524. 通过删除字母匹配到字典里最长单词 (Medium) 给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary 中最长的字符串,该字符串可以通过 ...

  5. PTA---求月天数

    最近做了几次模拟考试,对于求月天数这个题目有了更深一点的理解. 这个题的题目基本就是让用户输入年份和月份,给出该月有多少天. 对于这个题,首先就要考虑年份的问题,因为闰年和非闰年在二月是有一点不同的, ...

  6. 2021.09 ccf csp 第四题 收集卡牌

    2021.09 ccf csp 第四题 收集卡牌 思路 这题如果直接计算,因为不同的分类种数太多,枚举所有的分类情况是一个几乎不可能的复杂任务. 但不同摸牌次数,不同已摸出牌种类的子问题的答案之间,具 ...

  7. quasar+vue、Input组件绑定两个值

    项目中关于一个只读input绑定两个值,例如input显示取值范围,通过查看vue及quasar文档找出解决方法,如下代码: <q-input v-bind:value="`${det ...

  8. python中周日历与时间的相互转换

    python中周日历与时间的相互转换 周日历(ISO国际标准)介绍 在线周日历(2022年) 基本介绍 在开发过程中,有些汇总咨询需要以周为单位统计,所以介绍下如何进行相互转换. 使用datetime ...

  9. 题解[CF674D]Bearish_Fanpages

    题意 原意简述版 有 \(n\) 个公司,每个公司在某社交媒体拥有一个粉丝专页.该社交媒体推出了"关注"功能,每个粉丝专页必须"关注"一个粉丝专页,保证不会有自 ...

  10. JVM(一) --- 什么是JVM

    写在文章前:本系列博客是学习黑马程序员JVM完整教程所做笔记.若有错误希望大佬们评论区修正. 一.什么是JVM      Java Virtual Machine - java程序运行时所需环境(ja ...