Spring Boot 导出EXCEL模板以及导入EXCEL数据(阿里Easy Excel实战)
Spring Boot 导出EXCEL模板以及导入EXCEL数据(阿里Easy Excel实战)
导入pom依赖
.png)
编写导出模板
@ApiOperation("导出xxx模板")
@GetMapping("/downTemplates")
public void download(HttpServletResponse response) throws FileNotFoundException, IOException {
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
File temFile=new File("src/main/resources/templates/xxx导入模版.xlsx");
String fileName = URLEncoder.encode("xxx导入模版", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8' '" + fileName +".xlsx");
FileCopyUtils.copy(new FileInputStream(temFile), response.getOutputStream());
response.getOutputStream().close(); }
导入数据处理
编写监听器
@Component
public class xxxListener extends AnalysisEventListener<Entity> { private static final Logger LOGGER = LoggerFactory.getLogger(xxxListener.class);
//读取数据初始化值
private static final int BATCH_COUNT = 50;
List<Entity> list = new ArrayList<Entity>();
@Resource
private xxxxService xxxxService; public xxxxListener() {
xxxxService=new xxxxServiceImpl();
} public xxxxListener(xxxxService xxxxService) {
this.xxxxService=xxxxService;
} @Override
public void invoke(xxxx data, AnalysisContext analysisContext) {
list.add(data);
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
if (list.size() >= BATCH_COUNT) {
saveData();
// 存储完成清理 list
list.clear();
}
} /**
* 所有excel表中数据解析完成了 都会来调用这个
* 解释为什么要保存数据?
*初始化读取数量为50,表中信息已经加载完毕,,假设excel表中最后只剩下30行遗留数据,所以为了防止存在遗留数据 尽量判断下集合是否为空,不为空在进行存储(这是我的逻辑需要判断,如果不需要也可进行不判断)
* @param analysisContext
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
if(list.size()==0){
return;
}
saveData();
LOGGER.info("所有数据解析完成!");
} private void saveData() {
//代码实现类层保存数据
xxxxService.saveBatch(list);
LOGGER.info("存储数据库成功!");
}编写数据转换器
public class XXXConverter implements Converter<Integer> {
//在java中保护单位是用数字来标识的所以是int
@Override
public Class supportJavaTypeKey() {
return Integer.class;
} // 在excel中是string
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
} //将excel的数据类型转为java数据类型 @Override
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
String stringValue = cellData.getStringValue();
if (stringValue == null) {
throw new RuntimeException("数据为空");
} if ("(自己的数据)".equals(stringValue)) {
return 1;
} return 0;
} //将java的数据类型转为excel数据类型
@Override
public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new WriteCellData<>();
}
}
Spring Boot 导出EXCEL模板以及导入EXCEL数据(阿里Easy Excel实战)的更多相关文章
- Spring Boot 导出Excel表格
Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...
- Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用
Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用 原创 2017-04-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章介绍了一些基础,但都是静 ...
- 【spring boot】9.spring boot+spring-data-jpa的入门使用,实现数据持久化
spring-data-jpa官方使用说明文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ spring-d ...
- Spring Boot、微服务架构和大数据
一文读懂 Spring Boot.微服务架构和大数据治理三者之间的故事 https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶 ...
- Spring Boot 2.X(二):集成 MyBatis 数据层开发
MyBatis 简介 概述 MyBatis 是一款优秀的持久层框架,支持定制化 SQL.存储过程以及高级映射.它采用面向对象编程的方式对数据库进行 CRUD 的操作,使程序中对关系数据库的操作更方便简 ...
- Spring Boot下的一种导入Excel文件的代码框架
1.前言 Spring Boot下如果只是导入一个简单的Excel文件,是容易的.网上类似的文章不少,有的针对具体的实体类,代码可重用性不高:有的利用反射机制或自定义注解,开发了Excel导入工具 ...
- 下载excel模板,导入数据时需要用到
页面代码: <form id="form1" enctype="multipart/form-data"> <div style=" ...
- Spring Boot中使用模板引擎Thymeleaf
一.Thymeleaf简介 Thymeleaf[taɪm lif],百里香叶,是一个流行的模板引擎,该模板引擎采用Java语言开发.Java中常见的模板引擎有Velocity.Freemaker.Th ...
- Spring Boot导出jar包发布
一:事由 现在的项目组开发项目使用的是Spring Boot的技术,开发的时候是直接通过一个入口主函数来启动项目的.如果将项目交给客户,怎样才能正确的发布运行呢?百度了一下有关的知识,大概了解到是通过 ...
- Spring Boot整合 Thymeleaf 模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
随机推荐
- 10.4 认识Capstone反汇编引擎
Capstone 是一款开源的反汇编框架,目前该引擎支持的CPU架构包括x86.x64.ARM.MIPS.POWERPC.SPARC等,Capstone 的特点是快速.轻量级.易于使用,它可以良好地处 ...
- the solution of Mining Your Own Business
the description of problem (我看的是 PDF 里面的原题所以这里描述会和题目不一样,但是大意一致) 给定一个未必连通的无向图,问最少在几个点设置出口,可以保证任意一个点坍塌 ...
- C++ STL标准容器的特点和典型的使用场景
概念和作用 C++标准模板库(Standard Template Library,STL)提供了一组通用的模板类和函数,用于处理常见的数据结构和算法.STL中的标准容器是其中的重要组成部分,它们提供了 ...
- 【Spring】AOP实现原理
注册AOP代理创建器 在平时开发过程中,如果想开启AOP,一般会使用@EnableAspectJAutoProxy注解,这样在启动时,它会向Spring容器注册一个代理创建器用于创建代理对象,AOP使 ...
- 飞码LowCode前端技术:如何便捷配置出页面
简介 飞码是京东科技平台研发部研发的低代码产品,可使营销运营域下web页面快速搭建.本文将从三个方面来讲解如何便捷配置出页面,第一部分从数据.事件.业务支持三个方面进行分析,第二部分从模板与页面收藏与 ...
- animate.css 动画种类(详细)
作者:WangMin 格言:努力做好自己喜欢的每一件事 以下为各种动画类型包含的不同动画效果类,仅供参考.具体可查看animate.css 官网. bounce 弹跳 2. flash 闪烁 3. p ...
- 【pwn】[SDCTF 2022]Horoscope--栈溢出,atoi函数绕过
checksec检查一下,发现只开了nx,然后ida打开直接看主函数 发现fgets函数往s里面读入320个字节的数据,此处可造成溢出,再看看test和debug函数 void debug(){ t ...
- Excel 数据处理
博客地址:https://www.cnblogs.com/zylyehuo/ 2023 年高教社杯全国大学生数学建模竞赛题目 -- B 题 多波束测线问题 图表格式 import numpy as n ...
- 打造我的 Windows 开发环境
@charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...
- Vivado生成bitstream时报错[Opt 31-67] Problem: A LUT3 cell in the design is missing a connection on input pin I1, which is used by the LUT equation
这个原因主要是因为有一个引脚没有用到,解决方法. 1.打开Schematic. 2.根据提示的模块去找,比如说我的报错. [Opt 31-67] Problem: A LUT3 cell in the ...