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;
} }

最新文章

  1. 开发一款完备的android应用所必备的知识
  2. LINQ to SQL Count/Sum/Min/Max/Avg Join
  3. 关于myeclipse中导入的项目修改项目名使得发布到tomcat访问路径正确
  4. 挺有意思的HBase日志+Splunk
  5. 杭州电acm理工大舞台版
  6. stm32 复位后 引起引脚的变化,输出电平引起的问题
  7. 为什么win记事本编辑的shell在linux中运行会报错
  8. ubuntu远程桌面连接命令rdesktop连接windows远程桌面详解
  9. Java项目源码为什么要做代码混淆(解释的很好)
  10. Linux的pwd命令详解
  11. Spark大型电商项目实战-及其改良(1) 比对sparkSQL和纯RDD实现的结果
  12. Ansible运维工具
  13. iOS app签名原理
  14. whmcs之全民idc
  15. sphinx/Coreseek 4.1 执行make出错
  16. Django---渲染到模板
  17. WannaflyCamp 平衡二叉树(DP)题解
  18. C++网络爬虫的实现——WinSock编程
  19. 使用UITableView实现图片视差效果
  20. 【转】NHibernate 各种数据库配置

热门文章

  1. 第七周作业-N67044-张铭扬
  2. STM32 I2C介绍和软件模拟I2C编程要点
  3. C:\Windows\System32\drivers\etc\hosts中的文件修改后无法保存
  4. 2023 年 CCF 春季测试赛模拟赛 - 2 题解
  5. 04-python垃圾回收机制
  6. vue3新特性的使用
  7. Javaweb 登陆与验证码
  8. leecode70. 爬楼梯
  9. vue将地区以对象、数组的格式传给后端
  10. enzyme文档