easyExcel多sheet导入表格

	@PostMapping("/importExcelOther")@ApiOperationSupport(order = 9)@ApiOperation(value = "导入excel数据", notes = "传入excel文件")public R importExcelOther(@RequestParam("file") MultipartFile file) {if (file.getSize()==0){throw new RuntimeException("文件大小不能为空");}ETLExecutionThreadLocal.setStartTime(System.currentTimeMillis());String s = odsPvtService.importAllSheet(file);dwPvtServiceImpl.ODdsToDw();if (s.equals("success")) {return R.success("导入成功");} else {throw new RuntimeException(s);}}

下面开始进入正题

public interface IOdsPvtService {String importAllSheet(MultipartFile file);
}
	 @EtlLog@Transactional@Overridepublic String importAllSheet(MultipartFile file) {try (InputStream inputStream = file.getInputStream()) {// 创建临时文件File tempFile = createTempFile(file);// 读取Excel数据List<LunchDataMultiSheetListener> listenerList = readExcelData(tempFile);// 处理Excel数据Map<String, List<Object>> stringListMap = handleExcelData(listenerList);for (String s : stringListMap.keySet()) {List<Object> objects = stringListMap.get(s);switch (s) {case "特质焦虑问卷":List<OdsPvtSasEntity> entities = (List<OdsPvtSasEntity>) (List<?>) objects;odsPvtSasService.saveBatch(entities);break;case "压力知觉量表":List<OdsPvtPssEntity> pssEntities = (List<OdsPvtPssEntity>) (List<?>) objects;odsPvtPssService.saveBatch(pssEntities);break;case "生活满意度量表":List<OdsPvtSwlsEntity> shsEntities = (List<OdsPvtSwlsEntity>) (List<?>) objects;odsPvtSwlsService.saveBatch(shsEntities);break;case "主观幸福感量表":List<OdsPvtShsEntity> sdsEntities = (List<OdsPvtShsEntity>) (List<?>) objects;odsPvtShsService.saveBatch(sdsEntities);break;case "抑郁自评量表":List<OdsPvtSdsEntity> mlqEntities = (List<OdsPvtSdsEntity>) (List<?>) objects;odsPvtSdsService.saveBatch(mlqEntities);break;case "生命意义感量表":List<OdsPvtMlqEntity> mlqEntities1 = (List<OdsPvtMlqEntity>) (List<?>) objects;odsPvtMlqService.saveBatch(mlqEntities1);break;case "自尊量表":List<OdsPvtSesEntity> sesEntities = (List<OdsPvtSesEntity>) (List<?>) objects;odsPvtSesService.saveBatch(sesEntities);break;case "人格特质维度问卷":List<OdsPvtDyEntity> dyEntities = (List<OdsPvtDyEntity>) (List<?>) objects;odsPvtDyService.saveBatch(dyEntities);break;case "大五人格问卷":List<OdsPvtNeoFfiEntity> neoFfiEntity = (List<OdsPvtNeoFfiEntity>) (List<?>) objects;odsPvtNeoFfiService.saveBatch(neoFfiEntity);break;case "TKI冲突模式量表":List<OdsPvtTkiEntity> tkiFfiEntity = (List<OdsPvtTkiEntity>) (List<?>) objects;odsPvtTkiService.saveBatch(tkiFfiEntity);break;case "人际关系量表":List<OdsPvtNriEntity> nriEntity = (List<OdsPvtNriEntity>) (List<?>) objects;odsPvtNriService.saveBatch(nriEntity);break;case "亲社会行为倾向量表":List<OdsPvtPtmEntity> ptmEntity = (List<OdsPvtPtmEntity>) (List<?>) objects;odsPvtPtmService.saveBatch(ptmEntity);break;case "Buss-Warren攻击问卷":List<OdsPvtBwaqEntity> waqEntity = (List<OdsPvtBwaqEntity>) (List<?>) objects;odsPvtBwaqService.saveBatch(waqEntity);break;case "加工速度测评":List<OdsPvtSgEntity> waqEntity1 = (List<OdsPvtSgEntity>) (List<?>) objects;odsPvtSgService.saveBatch(waqEntity1);break;case "执行功能测评":List<OdsPvtTsEntity> tsEntity = (List<OdsPvtTsEntity>) (List<?>) objects;odsPvtTsService.saveBatch(tsEntity);break;case "情景记忆测评":List<OdsPvtEmEntity> waqEntity2 = (List<OdsPvtEmEntity>) (List<?>) objects;odsPvtEmService.saveBatch(waqEntity2);break;case "工作记忆测评":List<OdsPvtWmEntity> waqEntity3 = (List<OdsPvtWmEntity>) (List<?>) objects;odsPvtWmService.saveBatch(waqEntity3);break;case "注意力测评":List<OdsPvtSzhxEntity> waqEntity4 = (List<OdsPvtSzhxEntity>) (List<?>) objects;odsPvtSzhxService.saveBatch(waqEntity4);break;case "逻辑推理测评":List<OdsPvtRpmsEntity> waqEntity5 = (List<OdsPvtRpmsEntity>) (List<?>) objects;odsPvtRpmsService.saveBatch(waqEntity5);break;case "视空间测评":List<OdsPvtMrEntity> waqEntity6 = (List<OdsPvtMrEntity>) (List<?>) objects;odsPvtMrService.saveBatch(waqEntity6);break;case "特质应对方式问卷":List<OdsPvtTcsqEntity> waqEntity7 = (List<OdsPvtTcsqEntity>) (List<?>) objects;odsPvtTcsqService.saveBatch(waqEntity7);break;case "情绪调节量表":List<OdsPvtErqEntity> waqEntity8 = (List<OdsPvtErqEntity>) (List<?>) objects;odsPvtErqService.saveBatch(waqEntity8);break;case "心理弹性量表":List<OdsPvtCdRiscEntity> waqEntity9 = (List<OdsPvtCdRiscEntity>) (List<?>) objects;odsPvtCdRiscService.saveBatch(waqEntity9);break;case "自我效能感量表":List<OdsPvtGsesEntity> waqEntity10 = (List<OdsPvtGsesEntity>) (List<?>) objects;odsPvtGsesService.saveBatch(waqEntity10);break;case "基本信息调查表":List<OdsPvtBaseInfoEntity> odsPvtBaseInfoEntities = (List<OdsPvtBaseInfoEntity>) (List<?>) objects;odsPvtBaseInfoService.saveBatch(odsPvtBaseInfoEntities);break;default:// 可选:如果存在无法识别的实体类名称,可以选择抛出异常或进行适当处理break;}}return "success";} catch (IOException e) {e.printStackTrace();return e.getMessage();}}/*** 读取Excel数据*/private List<LunchDataMultiSheetListener> readExcelData(File file) {ExcelReader excelReader = EasyExcel.read(file).build();List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();List<LunchDataMultiSheetListener> listenerList = sheets.stream().map(sheet -> {LunchDataMultiSheetListener listener = new LunchDataMultiSheetListener();ReadSheet readSheet = EasyExcel.readSheet(sheet.getSheetNo()).registerReadListener(listener).build();excelReader.read(readSheet);List<List<String>> dataList = listener.getDataList();return listener;}).collect(Collectors.toList());excelReader.finish();return listenerList;}/*** 创建临时文件*/private File createTempFile(MultipartFile file) throws IOException {String originalFilename = file.getOriginalFilename();String[] filename = originalFilename.split("\\.");File tempFile = File.createTempFile(filename[0], "." + filename[1] + ".");file.transferTo(tempFile);tempFile.deleteOnExit();return tempFile;}//业务层private Map<String, List<Object>> handleExcelData(List<LunchDataMultiSheetListener> listenerList) {DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");Map<String, List<Object>> entityMap = new HashMap<>();listenerList.forEach(listener -> listener.getDataList().forEach(lineData -> {System.out.println("lineData = " + lineData);// 其它业务处理。。String type = lineData.get(2);Class entityClassBySheetName = getEntityClassBySheetName(type);if (entityClassBySheetName == null) {return;}// 使用反射创建新的对象try {Object entity = entityClassBySheetName.newInstance();// 设置属性值Field[] fields = entityClassBySheetName.getDeclaredFields();for (int i = 0; i < lineData.size(); i++) {if (i == lineData.size() - 1) {try {Field field = fields[i];field.setAccessible(true);String dateTimeString = lineData.get(i);LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);field.set(entity, localDateTime);continue;} catch (Exception e) {//最后一个字段类型可能是String类型,如果出问题,那就直接填字符串Field field = fields[i];field.setAccessible(true);field.set(entity, lineData.get(i));continue;}}Field field = fields[i];field.setAccessible(true);field.set(entity, lineData.get(i));}Field importTimeField =fields[lineData.size()];importTimeField.setAccessible(true);importTimeField.set(entity, LocalDateTime.now());// ...// 将对象添加到对应的列表中List<Object> entityList = entityMap.getOrDefault(type, new ArrayList<>());entityList.add(entity);entityMap.put(type, entityList);} catch (InstantiationException | IllegalAccessException e) {e.printStackTrace();// 处理异常}}));return entityMap;}// 根据 sheetName 返回对应的实体类型private Class getEntityClassBySheetName(String sheetName) {// 在此根据 sheetName 返回对应的实体类型,例如:if (sheetName.equals("特质焦虑问卷")) {return OdsPvtSasEntity.class;} else if (sheetName.equals("压力知觉量表")) {return OdsPvtPssEntity.class;} else if (sheetName.equals("生活满意度量表")) {return OdsPvtSwlsEntity.class;} else if (sheetName.equals("主观幸福感量表")) {return OdsPvtShsEntity.class;} else if (sheetName.equals("抑郁自评量表")) {return OdsPvtSdsEntity.class;} else if (sheetName.equals("生命意义感量表")) {return OdsPvtMlqEntity.class;} else if (sheetName.equals("自尊量表")) {return OdsPvtSesEntity.class;} else if (sheetName.equals("人格特质维度问卷")) {return OdsPvtDyEntity.class;} else if (sheetName.equals("大五人格问卷")) {return OdsPvtNeoFfiEntity.class;} else if (sheetName.equals("TKI冲突模式量表")) {return OdsPvtTkiEntity.class;} else if (sheetName.equals("人际关系量表")) {return OdsPvtNriEntity.class;} else if (sheetName.equals("亲社会行为倾向量表")) {return OdsPvtPtmEntity.class;} else if (sheetName.equals("Buss-Warren攻击问卷")) {return OdsPvtBwaqEntity.class;} else if (sheetName.equals("加工速度测评")) {return OdsPvtSgEntity.class;} else if (sheetName.equals("执行功能测评")) {return OdsPvtTsEntity.class;} else if (sheetName.equals("情景记忆测评")) {return OdsPvtEmEntity.class;} else if (sheetName.equals("工作记忆测评")) {return OdsPvtWmEntity.class;} else if (sheetName.equals("注意力测评")) {//注意力测评的表结构有问题 todoreturn OdsPvtSzhxEntity.class;} else if (sheetName.equals("逻辑推理测评")) {return OdsPvtRpmsEntity.class;} else if (sheetName.equals("视空间测评")) {return OdsPvtMrEntity.class;} else if (sheetName.equals("特质应对方式问卷")) {return OdsPvtTcsqEntity.class;} else if (sheetName.equals("情绪调节量表")) {return OdsPvtErqEntity.class;} else if (sheetName.equals("心理弹性量表")) {return OdsPvtCdRiscEntity.class;} else if (sheetName.equals("自我效能感量表")) {return OdsPvtGsesEntity.class;} else if (sheetName.equals("基本信息调查表")){return OdsPvtBaseInfoEntity.class;}return null;//返回空为了后续做准备}
}

必须建立一个监听器

package org.springblade.common.listener;import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;@Slf4j
@Data
public class LunchDataMultiSheetListener extends AnalysisEventListener<Map<Integer, String>> {private List<List<String>> dataList;public LunchDataMultiSheetListener() {this.dataList = new ArrayList<>();}/*** 每读到一行数据都调用invoke方法** @param integerObjectMap* @param context*/@Overridepublic void invoke(Map<Integer, String> integerObjectMap, AnalysisContext context) {Integer rowIndex = context.readRowHolder().getRowIndex();System.out.println("rowIndex = " + rowIndex);// key为列号,value为单元格的内容log.info("解析到数据:{}", integerObjectMap);// 获取当前解析的sheet的信息String sheetName = context.readSheetHolder().getSheetName();
//        Integer sheetIndex = context.readSheetHolder().getSheetNo();
//        System.out.println("当前解析的sheet名称:" + sheetName);
//        System.out.println("当前解析的sheet索引:" + sheetIndex);// 把数据放到dataList里面,便于统一处理LinkedList<String> strings = new LinkedList<>();integerObjectMap.forEach((k, v) -> {strings.add(v);});this.dataList.add(strings);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {// 读完所有数据,做统一处理。// 当然还可以拿到listener之外处理log.info("数据读取完成");}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/730752.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CMake笔记

CMake笔记 文章目录 CMake笔记1 工程项目一般形式2 常见命令2.1 project2.2 set2.3 message2.4 add_executable()2.5 语法原则2.6 add_subdirectory2.7 add_library2.8 list 3 安装3.1 安装.h文件/文本文件3.2 安装工程脚本3.3 安装目录/目录下内容3.4 安装库文件3.5安装过程 4…

cocos creator 3.7.2使用shader实现图片扫光特效

简介 功能&#xff1a;图片实现扫光效果 引擎&#xff1a;cocos Creator 3.7.2 开发语言&#xff1a;ts 完整版链接 链接https://lengmo714.top/284d90f4.html 效果图 shader代码 // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd. CCEffect %{techniques:- pas…

CorelDRAW Graphics Suite2024免费试用体验15天版下载

使用基于全球知名的 Corel Painter 画笔技术构建的 100 款逼真像素画笔&#xff0c;以全新的方式将您独特的想法变为现实&#xff01;试用 CorelDRAW 的全新美术画笔&#xff0c;探索您的创意想法。 使用 CorelDRAW 中现在可用的远程字体&#xff0c;畅享更多创作自由&#xf…

【linux驱动开发】IO模型之同步IO、异步IO、IO多路复用

文章目录 IO的概述IO模型的实现阻塞IO非阻塞IOIO多路复用信号驱动异步IO 编译与测试说明 IO的概述 io&#xff0c;英文名称为inoput与output&#xff0c;表示输入与输出。 在冯诺依曼结构计算机中&#xff0c;计算机由 运算器、控制器、存储器、输入、输出五部分组成&#xf…

warning: #188-D: enumerated type mixed with another type

警告解释&#xff1a;枚举类型混合了其它的数据类型&#xff1b; 解决方法&#xff1a; 1&#xff1a;检查代码&#xff0c;是不是存在混用&#xff1b;&#xff1b; 2&#xff1a;结构体初始化为 0 报warning&#xff0c;不能将结构体的第一个变量&#xff0c;使用枚举类型&am…

安全防御第七次实验

需求&#xff1a;在FW7和FW8之间建立一条IPSEC通道保证10.0.2.0/24网段可以正常访问到192.168.1.0/24 一、NAT配置 FW4&#xff1a; FW6&#xff1a; 二、在FW4上做服务器映射 三、配置IPSEC FW5&#xff1a; FW6&#xff1a; 四、防火墙上的安全策略 FW4&#xff1a; FW5:…

VBA中类的解读及应用第十讲:限制文本框的输入,使其只能输入数值(上)

《VBA中类的解读及应用》教程【10165646】是我推出的第五套教程&#xff0c;目前已经是第一版修订了。这套教程定位于最高级&#xff0c;是学完初级&#xff0c;中级后的教程。 类&#xff0c;是非常抽象的&#xff0c;更具研究的价值。随着我们学习、应用VBA的深入&#xff0…

【动态规划.3】[IOI1994]数字三角形 Number Triangles

题目 https://www.luogu.com.cn/problem/P1216 观察下面的数字金字塔。 写一个程序来查找从最高点到底部任意处结束的路径&#xff0c;使路径经过数字的和最大。每一步可以走到左下方的点也可以到达右下方的点。 7→3→8→7→5 的路径产生了最大权值。 分析 这是一个动态规划…

抽象版忘记密码模板0.1版本

背景&#xff1a;用户密码忘了&#xff0c;无法登录&#xff0c;怎么办&#xff01;急&#xff01;急&#xff01;急&#xff01; 前置工作——Srpingboot3-maven项目&#xff0c;核心框架如下 <dependency><groupId>org.springframework.boot</groupId><…

Redis核心数据结构之字典(二)

字典 解决键冲突 当有两个或以上数量的键被分配到了一个哈希表数组的同一个索引上面&#xff0c;我们称这些键发生了冲突(collision)。 Redis的哈希表使用链地址法(separate chaining)来解决键冲突&#xff0c;每个哈希表节点都有一个next指针&#xff0c;多个哈希表节点可以…

数据库原理实验课(1)

目录 实验内容 安装头歌中的相关内容 具体过程 完结撒花~ 我也是第一次接触oracle的相关软件和操作&#xff0c;所以是一次傻瓜式教学记录 实验内容 安装头歌中的相关内容 具体过程 这是我在百度网盘中下载解压出来的oracle文件夹内的全部内容&#xff08;可能有因为安装完…

CodeSys通过C函数接口调用Qt

建议先查看之前的文章【CodeSys中调用C语言写的动态库】&#xff0c;了解如何创建一个能够被codesys调用的动态库。 假如想要在函数中使用Qt或者第三方库&#xff08;比如opencv等&#xff09;&#xff0c;可以在其自动生成的makefile文件中设置好相应的参数。 比如我这里就是…

2024037期传足14场胜负前瞻

2024037期售止时间为3月9日&#xff08;周六&#xff09;20点00分&#xff0c;敬请留意&#xff1a; 本期深盘多&#xff0c;1.5以下赔率4场&#xff0c;1.5-2.0赔率5场&#xff0c;其他场次是平半盘、平盘。本期14场整体难度中等。以下为基础盘前瞻&#xff0c;大家可根据自身…

力扣刷题Days13-101对称二叉树(js)

目录 1,题目 2&#xff0c;代码 2.1递归思想 2.2队列--迭代思想 3&#xff0c;学习与总结 1,题目 给你一个二叉树的根节点 root &#xff0c; 检查它是否轴对称。 2&#xff0c;代码 2.1递归思想 return dfs(left.left, right.right) && dfs(left.right, right.l…

【力扣每日一题】力扣232用栈实现队列

题目来源 力扣232用栈实现队列 题目概述 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作&#xff08;push、pop、peek、empty&#xff09;&#xff1a; 实现 MyQueue 类&#xff1a; void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的…

IDS入侵检测知识整理及lDS入侵检查表

【IDS入侵检测知识整理及lDS入侵检查表】 1. 网络入侵检测IDS概念 2. IDS分类 3. IDS的工作原理 4. IDS在网络中的部署 5. 入侵检查表 项目建设全套资料获取通道&#xff1a;软件开发全套资料_数字中台建设指南-CSDN博客

echarts中toolbox 中文乱码问题

问题描述 本地引用的echarts源文件&#xff0c;页面其他部分编码显示正常&#xff0c;唯独toolbox鼠标悬停在上面时提示信息显示乱码。 如图所示&#xff1a; 尝试过的方法 使用sublime text 3&#xff0c;notepad&#xff0c;记事本更改文件编码为utf-8引入时&#xff0c;在sc…

办公电脑换成MacBookPro半年之后……

小白是从2008年开始接触电脑的&#xff0c;当时朋友给我注册的第一个QQ账号是2008年4月。 从此&#xff0c;小白一直认为电脑全部都是Windows系统。直到上大学那年&#xff0c;看到了外教老师的MacBookPro…… 折腾电脑的开始居然是起源于诺基亚手机&#xff0c;给半智能S40的…

程序员有哪些证书值得考?

证书可以作为第三方机构对于程序员特定技能或知识掌握程度的认可&#xff0c;在求职市场上&#xff0c;一些公司尤其是大型企事业单位或者政府项目可能更看重证书作为衡量应聘者专业能力的标准之一。备考证书的过程&#xff0c;也可以促使程序员系统地学习和巩固相关知识&#…

力扣hot100:438.找到字符串中所有字母异位词(滑动窗口)

26个字符&#xff0c;我复制怎么了&#xff1f;26个字符我比较个数怎么了&#xff1f; 顶多时间复杂度*26 本题用固定窗口大小的滑动窗口每次比较包含26个元素的数组次数&#xff0c;最容易写。 动态窗口大小哈希表存数值&#xff08;双指针差值&#xff09;难想难写。 一、动态…