谷粒 | 12 |easyExcel使用
一、引入easyexcel依赖
<!--easyExcel依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
二、根据excel数据格式创建实体类
@Data
public class SubjectData {
@ExcelProperty(value = "一级标题",index = 0) //easyexcel注释,列名和列索引值
private String firstLevelSubject;
@ExcelProperty(value = "二级标题",index = 1)
private String secondLevelSubject;
}
三、控制层和业务层接口
//添加课程分类
//获取上传过来的文件,把文件内容读取出来
@PostMapping("addSubject")
@ApiOperation(value = "上传并读取excel表")
public Result addSubject(MultipartFile file) {
//上传过来excel文件
subjectService.saveSubject(file,subjectService);
return Result.ok();
}
public interface EduSubjectService extends IService<EduSubject> {
//添加课程分类
void saveSubject(MultipartFile file,EduSubjectService subjectService);
List<FirstLevelSubject> getFirstAndSecondSubjects();
}`
@Override
public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
try {
//获取文件输入流
InputStream inputStream = file.getInputStream();
EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
} catch (IOException e) {
e.printStackTrace();
}
}
四、监听器
public class SubjectExcelListener extends AnalysisEventListener {
//因为SubjectExcelListener不交给spring进行管理,不能注入其他对象,无法操作数据库
public EduSubjectService subjectService;
public SubjectExcelListener() {
}
public SubjectExcelListener(EduSubjectService subjectService) {
this.subjectService = subjectService;
}
@Override
public void invoke(Object data, AnalysisContext context) {
SubjectData subjectData = (SubjectData) data;
if (data == null) {
throw new MyException(20001, "文件数据为空");
}
//一行一行读取,每次读取两个值,第一个值一级分类,第二个值二级分类
//判断一级分类是否重复,一级分类不能重复添加
EduSubject existOneSubject = existFirstLevelSubject(subjectService, subjectData.getFirstLevelSubject());
if (existOneSubject == null) {
existOneSubject = new EduSubject();
existOneSubject.setParentId("0");
existOneSubject.setTitle(subjectData.getFirstLevelSubject());
subjectService.save(existOneSubject);
}
String pid = existOneSubject.getId();
//判断二级分类是否重复
EduSubject existSecondSubject = existSecondLevelSubject(subjectService, subjectData.getSecondLevelSubject(), pid);
if (existSecondSubject == null) {
existSecondSubject = new EduSubject();
existSecondSubject.setParentId(pid);
existSecondSubject.setTitle(subjectData.getSecondLevelSubject());
subjectService.save(existSecondSubject);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
//判断一级分类不能重复添加
private EduSubject existFirstLevelSubject(EduSubjectService subjectService, String name) {
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.eq("title", name);
wrapper.eq("parent_id", "0");
EduSubject firstSubject = subjectService.getOne(wrapper);
return firstSubject;
}
//判断二级分类是否重复
private EduSubject existSecondLevelSubject(EduSubjectService subjectService, String name, String pid) {
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.eq("title", name);
wrapper.eq("parent_id", pid);
EduSubject secondSubject = subjectService.getOne(wrapper);
return secondSubject;
}
}
谷粒 | 12 |easyExcel使用的更多相关文章
- python 各模块
01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
- 大数据技术之_08_Hive学习_05_Hive实战之谷粒影音(ETL+TopN)+常见错误及解决方案
第10章 Hive实战之谷粒影音10.1 需求描述10.2 项目10.2.1 数据结构10.2.2 ETL原始数据10.3 准备工作10.3.1 创建表10.3.2 导入ETL后的数据到原始表10.3 ...
- 阿里 EasyExcel 7 行代码优雅地实现 Excel 文件生成&下载功能
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- 【EasyExcel】使用easyExcel过程中,项目报错的解决集合
报错:Can not close IO [ERROR] 2019-11-02 13:51:21.210 [ProExportSkuDataJob-1455-TaskThread-1] [com.dma ...
- 【Easyexcel】java导入导出超大数据量的xlsx文件 解决方法
解决方法: 使用easyexcel解决超大数据量的导入导出xlsx文件 easyexcel最大支持行数 1048576. 官网地址: https://alibaba-easyexcel.github. ...
- springboot easyexcel
pom..xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel&l ...
- EasyExcel 自定义单元格式的问题。
最近在做一个关于性能测试管理系统,一个新的需求,需要导出测试报告,直接使用了ali的封装的EasyExcel,但是在复杂头与一些样式,就缺少了自定义的灵活性,在官方demo中没有找到很好的解决方法. ...
随机推荐
- php 日期相关的类 DateInterval DateTimeZone DatePeriod
* DateInterval <?php /** * Created by PhpStorm. * User: Mch * Date: 7/18/18 * Time: 21:30 */ $dat ...
- 修改为阿里的yum源
如果没有wget,先安装一个.(如果有请蹦过) yum install wget -y 备份本地yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.r ...
- bzoj3729-Gty的游戏【Splay,博弈论】
正题 题目链接:https://darkbzoj.tk/problem/3729 题目大意 给出\(n\)个点的一棵树,第\(i\)个节点上有\(a_i\)个石子,然后每次可以选择不超过\(L\)个石 ...
- IdentityServer4系列[6]授权码模式
授权码模式是一种混合模式,是目前功能最完整.流程最严密的授权模式.它主要分为两大步骤:认证和授权.其流程为: 用户访问客户端,客户端将用户导向Identity Server. 用户填写凭证信息向客户端 ...
- 【深度学习】softmax回归——原理、one-hot编码、结构和运算、交叉熵损失
1. softmax回归是分类问题 回归(Regression)是用于预测某个值为"多少"的问题,如房屋的价格.患者住院的天数等. 分类(Classification)不是问&qu ...
- DIVIDEMIX: LEARNING WITH NOISY LABELS AS SEMI-SUPERVISED LEARNING
论文阅读: DIVIDEMIX: LEARNING WITH NOISY LABELS AS SEMI-SUPERVISED LEARNING 作者说明 版权声明:本文为博主原创文章,遵循CC 4.0 ...
- SpringBoot+WebSocket实时监控异常
写在前面 此异常非彼异常,标题所说的异常是业务上的异常. 最近做了一个需求,消防的设备巡检,如果巡检发现异常,通过手机端提交,后台的实时监控页面实时获取到该设备的信息及位置,然后安排员工去处理. 因为 ...
- Min_25筛 学习小记
前言 为什么叫学习小记呢?因为暂时除了模板题就没有做其他的东西了.(雾 这个东西折磨了我一整天,看得我身不如死,只好结合代码理解题解,差点死在机房.(话说半天综合半天竞赛真是害人不浅) 为了以后忘了再 ...
- 洛谷5038 [SCOI2012]奇怪的游戏(二分+网络流+判断奇偶)
寒假的时候就听过这个题.但是一直没有写. qwq 首先,我们发现题目中的图是个网格图,然后每次可以将相邻两个格子加一. 很容易就想到是黑白染色.那么每次操作,就相当于同时操作一个白点,一个黑点. 我们 ...
- SpringBoot入门02-配置类
引入 Spring Boot的底层已经有了Spring MVC Spring Boot习惯优先的思想,很多配置都是可省的 不需要配置web.xml文件 不需要服务层的xml配置 不需要dao层的xml ...