/*** Description: 获取日前交易excel数据** @param file* @return*/
private Map<String,List<SpotDailyDealImportVO>> SpotDailyDealLoadExcelData(MultipartFile file){Map<String,List<SpotDailyDealImportVO>> map = new HashMap<>();try {//获取所有sheet页List<ReadSheet> readSheets = ExcelImportHelper.listSheet(file.getInputStream());List<ReadSheet> arrayList = readSheets.stream().collect(// 将集合先放到 treeSet去重,然后将他们转换成listcollectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getSheetName()))), ArrayList::new));if (arrayList.size() < readSheets.size()){throw new BusinessException("存在相同机组名称!");}if (ObjectUtils.isNotEmpty(readSheets)){//读取数据int i = 0;//从第一页开始读for (ReadSheet readSheet : readSheets){map.put(readSheet.getSheetName(),ExcelImportHelper.loadExcelDataFully(ExcelImportHelper.getExcelTypeByName(file.getOriginalFilename()), file.getInputStream(), i ,MagicNumConstant.ONE, SpotDailyDealImportVO.class));i++;}}else throw new BusinessException("无法获取到sheet页信息!");} catch (IOException e) {log.error("error",e);throw new BusinessException("上传文件格式不正确" + e.getMessage());}if (map.size() == 0){throw new BusinessException("数据异常,没有数据!");}return map;
}

工具类

/** Project Name: fsc* File Name: ExcelImportHelper.java* Class Name: ExcelImportHelper** Copyright 2018 Hengtian Software Inc** Licensed under the Hengtiansoft** http://www.hengtiansoft.com** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or* implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package com.hengtiansoft.framework.common.excel;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.hengtiansoft.framework.common.excel.read.XlsReader;
import com.hengtiansoft.framework.common.excel.read.XlsxReader;
import com.hengtiansoft.framework.common.excel.read.handle.ExcelDataHandler;
import com.hengtiansoft.framework.common.excel.read.handle.impl.FullyReadExcelDataHandler;
import lombok.extern.slf4j.Slf4j;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** Description: Excel工具类** @author qianqianzhu1, weiyangzhu* @since 29.09.2018*/
@Slf4j
public final class ExcelImportHelper {public static final String OFFICE_EXCEL_XLS = ".xls";public static final String OFFICE_EXCEL_XLSX = ".xlsx";public static final String OFFICE_EXCEL_XLSM = ".xlsm";private ExcelImportHelper() {}/*** 根据文件扩展名获得Excel文件类型** @param excelName 文件名,包含扩展名* @return Excel类型*/public static ExcelType getExcelTypeByName(String excelName) {if (excelName == null) {throw new IllegalArgumentException("Excel name is null.");}if (excelName.endsWith(OFFICE_EXCEL_XLS)) {return ExcelType.XLS;} else if (excelName.endsWith(OFFICE_EXCEL_XLSX)) {return ExcelType.XLSX;} else if (excelName.endsWith(OFFICE_EXCEL_XLSM)) {return ExcelType.XLSM;} else {throw new IllegalArgumentException("Invalid excel name: " + excelName);}}/*** 将指定Excel文件中第0个Sheet全部读入内存,跳过标题行** @param excelFile Excel文件* @param titleLine  需要跳过的标题行数* @param clazz     数据类描述* @param <T>       数据类型* @return 从Excel中读取的数据列表* @throws IOException IO异常*/public static <T> List<T> loadExcelDataAtSheet0Fully(File excelFile, Integer titleLine, Class<T> clazz) throws IOException {if (excelFile == null) {throw new IllegalArgumentException("Excel file is null.");}ExcelType excelType = getExcelTypeByName(excelFile.getName());return loadExcelDataAtSheet0Fully(excelType, new FileInputStream(excelFile), titleLine, clazz);}/*** 将指定输入流中的Excel文件中第0个Sheet全部读入内存,跳过标题行** @param excelType   Excel文件类型* @param inputStream Excel文件输入流* @param titleLine  需要跳过的标题行数* @param clazz       数据类描述* @param <T>         数据类型* @return 从Excel中读取的数据列表* @throws IOException IO异常*/public static <T> List<T> loadExcelDataAtSheet0Fully(ExcelType excelType, InputStream inputStream, Integer titleLine, Class<T> clazz) throws IOException {return loadExcelDataFully(excelType, inputStream, 0, true, titleLine, clazz);}/*** 将指定输入流中的Excel文件中的数据全部读入内存** @param excelType    Excel文件类型* @param inputStream  Excel文件输入流* @param sheetIndex   需要读取的sheet索引号,从0开始,-1表示读取全部sheet* @param skipFirstRow true表示跳过标题行* @param titleLine  需要跳过的标题行数* @param clazz        数据类描述* @param <T>          数据类型* @return 从Excel中读取的数据列表* @throws IOException IO异常*/public static <T> List<T> loadExcelDataFully(ExcelType excelType, InputStream inputStream, int sheetIndex,boolean skipFirstRow, Integer titleLine, Class<T> clazz) throws IOException {return loadExcelDataFully(excelType, inputStream, sheetIndex, skipFirstRow ? titleLine : 0, clazz);}/*** 将指定输入流中的Excel文件中的数据全部读入内存** @param excelType    Excel文件类型* @param inputStream  Excel文件输入流* @param sheetIndex   需要读取的sheet索引号,从0开始,-1表示读取全部sheet* @param dataStartRow 开始读取数据的行号(包含该行),从0开始* @param clazz        数据类描述* @param <T>          数据类型* @return 从Excel中读取的数据列表* @throws IOException IO异常*/public static <T> List<T> loadExcelDataFully(ExcelType excelType, InputStream inputStream, int sheetIndex,int dataStartRow, Class<T> clazz) throws IOException {FullyReadExcelDataHandler<T> dataHandler = new FullyReadExcelDataHandler<>();loadExcelData(excelType, inputStream, sheetIndex, dataStartRow, clazz, dataHandler);if (dataHandler.hasError()) {String errorMsg = "Reading excel occurs error";if (dataHandler.getErrorSheetName() != null) {errorMsg = errorMsg + " at sheet: " + dataHandler.getErrorSheetName();}if (dataHandler.getErrorRow() != -1) {errorMsg = errorMsg + " row: " + dataHandler.getErrorRow();}if (dataHandler.getErrorCol() != -1) {errorMsg = errorMsg + " col: " + dataHandler.getErrorCol();}throw new IOException(errorMsg, dataHandler.getLastError());}return dataHandler.getData();}/*** 使用读取处理器处理输入流中的Excel文件,以文件流方式读入,可能会占用更多一些内存** @param excelType    Excel类型* @param inputStream  输入流* @param sheetIndex   需要读取的sheet索引号,从0开始,-1表示读取全部sheet* @param dataStartRow 开始读取数据的行号(包含该行),从0开始* @param clazz        数据类描述* @param dataHandler  Excel数据处理器* @param <T>          数据类型*/public static <T> void loadExcelData(ExcelType excelType, InputStream inputStream, int sheetIndex,int dataStartRow, Class<T> clazz, ExcelDataHandler<T> dataHandler) {switch (excelType) {case XLS:XlsReader.loadExcelData(inputStream, sheetIndex, dataStartRow, clazz, dataHandler);break;case XLSX:case XLSM:XlsxReader.loadExcelData(inputStream, sheetIndex, dataStartRow, clazz, dataHandler);break;default:throw new IllegalArgumentException("Invalid excel type: " + excelType);}}/*** 使用读取处理器处理输入流中的Excel文件,以文件方式读入会节约一些内存,发生OOM的概率小一些** @param excelType    Excel类型* @param file         Excel文件* @param sheetIndex   需要读取的sheet索引号,从0开始,-1表示读取全部sheet* @param dataStartRow 开始读取数据的行号(包含该行),从0开始* @param clazz        数据类描述* @param dataHandler  Excel数据处理器* @param <T>          数据类型*/public static <T> void loadExcelData(ExcelType excelType, File file, int sheetIndex,int dataStartRow, Class<T> clazz, ExcelDataHandler<T> dataHandler) {switch (excelType) {case XLS:XlsReader.loadExcelData(file, sheetIndex, dataStartRow, clazz, dataHandler);break;case XLSX:XlsxReader.loadExcelData(file, sheetIndex, dataStartRow, clazz, dataHandler);break;default:throw new IllegalArgumentException("Invalid excel type: " + excelType);}}/*** 获取excel文件所有sheet页* @date 2022/11/25 17:13* @param inputStream 文件流* @return java.util.List<com.alibaba.excel.read.metadata.ReadSheet>*/public static List<ReadSheet> listSheet(InputStream inputStream){if(inputStream == null){throw new RuntimeException("inputStream is null");}ExcelReader build = EasyExcel.read(inputStream).build();List<ReadSheet> readSheets = build.excelExecutor().sheetList();log.info(String.valueOf(readSheets));return readSheets;}
}

easyExcel多sheet页导入相关推荐

  1. EasyExcel多sheet页导出详细代码记录

    一.引入pom依赖 <!--excel--><dependency><groupId>com.alibaba</groupId><artifact ...

  2. 4. java使用easyexcel导入excel-多个sheet页、每个sheet页存在多个表头导入的情况、踩坑记录、可在线拉取成品demo、也可参详详细演示流程、贴心手把手操作

    文章目录 1.EXCEL模板数据格式.导入结果展示 2.避坑防雷招待所[♥] 2.1.与poi-Jar包文件版本冲突 2.2.传入后台读取文件流报空指针 3.在线获取代码及模板 3.1.Github获 ...

  3. EasyExcel怎么读取下载多个sheet页数据

    工作中用到excel的多sheet页数据的读取和下载多sheet页的数据,之前都是手动一个一个表格自己拼装数据.最近研究了EasyExcel,话不多说直接上代码 1.导入sheet页面的实体类 @Da ...

  4. easyexcel多个sheet导入_Java中Easypoi实现excel多sheet表导入导出功能

    Easypoi简化了开发中对文档的导入导出实现,并不像poi那样都要写大段工具类来搞定文档的读写. 第一步引入Easypoi依赖 cn.afterturn easypoi-spring-boot-st ...

  5. easyexcel多个sheet导入_Easypoi实现excel多sheet表导入导出功能

    Easypoi简化了开发中对文档的导入导出实现,并不像poi那样都要写大段工具类来搞定文档的读写. 第一步引入Easypoi依赖 cn.afterturn easypoi-spring-boot-st ...

  6. EasyExcel删除模版Sheet页

    EasyExcel简介 git地址:https://github.com/alibaba/easyexcel 文档:https://easyexcel.opensource.alibaba.com/d ...

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

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

  8. EasyExcel 实现多个Sheet页导出

    1 Maven依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel< ...

  9. EasyExcel获取excel文件中sheet页总数,及对应sheet页名称及下标

    问题: 多工作表excel文件读取,获取文件中总共有多少个sheet页,及获取对应sheet页名称 代码 import com.alibaba.excel.EasyExcel; import com. ...

最新文章

  1. Wcf 基础教程 服务寄宿之 Windows 服务寄宿
  2. python中yield使用
  3. JavaScript中的Window窗口对象【转载】
  4. java 怎么判别注释符_java学习笔记二(注释、关键字、标识符)
  5. 一次http完整的请求tcp报文分析
  6. 《大数据》第1期“聚焦”——从系统角度审视大数据计算
  7. VMweare 安装 Kali Linux 系统
  8. 用 Python 识别图片中的文字
  9. 为什么c相电路在前面_Buck电路的多角度分析
  10. CodeForces 292D Connected Components (并查集+YY)
  11. Xftp6-连接Linux传输文件---干货!!!(无私奉献无需积分)
  12. 19张网络工程师必看的思维导图
  13. 51nod 1359 循环探求
  14. 阿里P7晒出1月工资单:狠补了这个,真香...
  15. java爬取今日头条文章
  16. EditText更改光标的大小和颜色
  17. 园区网络三层架构实验
  18. AsyncTask的优缺点
  19. MindSpore【数据集功能】无法查看数据集
  20. mysql 省市县镇(乡)四级地区数据库

热门文章

  1. Linux 计算器 bc
  2. 3gpp协议_NSA组网下SN add/release/delete流程协议学习
  3. 电子元器件基础1---电阻
  4. 贫僧自东土大唐而来, 前往西天拜佛求经 ( 研发 )
  5. 银河麒麟v10-arm离线部署k8s集群(v1.23.4)
  6. Chrome 仍是目前最佳的 Android 浏览器
  7. 5G风起,未来数据库有哪些关键词?
  8. 华为 中兴 贝尔 OLT ONU状态查询工具(一)
  9. 【华为机试真题Java】火星文计算
  10. 螳螂捕蝉java面向对象,苏教版六年级下册语文课文《螳螂捕蝉》补充习题答案...