Easys Excel的表格导入(读)导出(写)-----java

一,EasyExcel官网:

可以学习一些新知识:

EasyExcel官方文档 - 基于Java的Excel处理工具 | Easy Excel

二,为什么要使用easyexcle

excel的一些优点和缺点

java解析excel的框架有很多 :

poi jxl,存在问题:非常的消耗内存,

easyexcel 我们遇到再大的excel都不会出现内存溢出的问题 能够将一个原本3M excel文件,poi来操作将会占用内存

100MB,使用easyexcel减低到几Mb,使用起来更加简单

poi读 1、创建xsshworkbook/hssfworkbook(inputstream in)

2、读取sheet

3、拿到当前sheet所有行row

4、通过当前行去拿到对应的单元格的值


easyexcel拟解决的问题

1.excel读写时内存溢出

2.使用简单

3.excel格式解析


工作原理

三,项目的步骤

依赖包:

基本上要用的的依赖包:

       <!--		mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.2</version></dependency><!--	mysql	--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.28</version></dependency><!--        reds依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId></dependency><!--     连接池依赖   --><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.11.1</version></dependency>
<!--        数据池--><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.14</version></dependency>
<!--    mapper    --><!-- https://mvnrepository.com/artifact/tk.mybatis/mapper --><dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>4.0.2</version></dependency><!--    easyexcel依赖    --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.3</version></dependency><!--log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>

一,导出(写)流程写法:

1. Server接口:

package com.example.excel01.generator.service;import com.example.excel01.generator.domain.Excel;
import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;/**
* @author zeng
* @description 针对表【excel】的数据库操作Service
* @createDate 2023-08-02 11:10:56
*/
public interface ExcelService extends IService<Excel> {public Integer getCount(); //总条数public List<Excel> getListBYPage(Integer pageOn); //分页查询}

2. 2.ServiceImpl类:

package com.example.excel01.generator.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.excel01.generator.domain.Excel;
import com.example.excel01.generator.mapper.ExcelMapper;
import com.example.excel01.generator.service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import java.util.List;/**
* @author zeng
* @description 针对表【excel】的数据库操作Service实现
* @createDate 2023-08-02 11:10:56
*/
@Service
@Transactional
public class ExcelServiceImpl extends ServiceImpl<ExcelMapper, Excel>implements ExcelService {@Autowiredprivate ExcelMapper excelMapper;@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic Integer getCount() {return excelMapper.selectCount(null);}@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic List<Excel> getListBYPage(Integer pageOn) {Integer begin=(pageOn-1)*50000+1;//第几页显示多少数据return excelMapper.ListPage(begin,50000);}
}

3. 3.Test测试:

    /*** 写数据* */@Testvoid contextLoads4(){//保存路径String path="E:\\aaa\\ciy2.xls";//查询数据总条数Integer count = excelService.getCount();//创建easyexcel的写出类构造器 参数 告诉构造器 我的excel将来要写到哪里 以及excel中数据是基于哪个java对象模板创建的ExcelWriter excelWriter = EasyExcel.write(path, Excel.class).build();//判断一页能放多少条数据Integer sheetNum = count % 50000 == 0 ? count / 50000 : count / 50000 + 1;log.info("sheetNum=={}",sheetNum);for(int i=1;i<sheetNum;i++){log.info("第"+i+"批次");//分页查询数据库List<Excel> listPage = excelService.getListBYPage(i);//创建sheet构造器WriteSheet sheet = EasyExcel.writerSheet("test").build();//使用excel对象将数据写入到创建的sheet当中excelWriter.write(listPage,sheet);}excelWriter.finish();log.info("导出成功");}

二,导入(读)流程写法

1. 监听器:

读取数据要通过监听器来实现,监听读取的每一条数据:


监听器:

package com.example.excel01.generator.listenner;import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.excel01.generator.domain.FmAddress;
import com.example.excel01.generator.service.FmAddressService;
import lombok.extern.slf4j.Slf4j;/*** 监听器*/@Slf4j
public class EasyExcelListenner extends AnalysisEventListener<FmAddress> {static Integer a = 0;@Overridepublic void invoke(FmAddress fmAddress, AnalysisContext analysisContext) {++a;log.info("a={}",a);if (a == 1000) {try {a=0;Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}}log.info(String.valueOf(fmAddress));//获取beanFmAddressService fmAddressService = SpringJobBeanFactory.getBean(FmAddressService.class);fmAddressService.insert(fmAddress);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {}
}

a. 知识点普及:

1.监听器我们是继承Excel中的AnalysisEventListener方法来

2.该监听器是不被Spring (bean) 容器管理的,我要手动注入容器


2. 手动将监听器注入Spring容器:

package com.example.excel01.generator.listenner;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/*** @ClassName: SpringJobBeanFactory*/
@Component
public class SpringJobBeanFactory implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringJobBeanFactory.applicationContext=applicationContext;}public static ApplicationContext getApplicationContext() {return applicationContext;}@SuppressWarnings("unchecked")public static <T> T getBean(String name) throws BeansException {if (applicationContext == null){return null;}return (T)applicationContext.getBean(name);}public static <T> T getBean(Class<T>  name) throws BeansException {if (applicationContext == null){return null;}return applicationContext.getBean(name);}
}

a. 普及知识点:

手动注入bean 调用的是ApplicationContextAware接口


3.Test测试类:

    /*** 读数据*/@Testvoid contextLoads5(){//保存路径String path="E:\\aaa\\ciy.xls";ExcelReader build = EasyExcel.read(path, FmAddress.class, new EasyExcelListenner()).build();ReadSheet build1 = EasyExcel.readSheet(0).build();log.info("build1={}",build1);build.read(build1);build.finish();}

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

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

相关文章

获取历史dokcer镜像项目,并上传gitlab,再打包镜像管理

今天遇到一个问题&#xff1a; 发现一个部署在Jenkins的脚本用的docker镜像是&#xff1a;test_project:v20191108&#xff0c;即这个项目是19年的一个版本&#xff0c;由于代码不断更新&#xff0c;用现在的最新代码运行该脚本&#xff0c;可能不能运行了&#xff0c;必须用19…

Linux 基础(七)常用命令 - 磁盘分区命令

磁盘分区命令 磁盘相关命令dudffreetree 分区相关命令查看设备挂载明细挂载/卸载 分区挂载卸载持久挂载 分区查看当前分区详情对硬盘分区分区为新分区构建文件系统给新分区挂载一个目录 常见文件系统类型 磁盘相关命令 du 查看目录或者文件占用&#xff1b;默认会递归查询子目…

基于扩频的数字视频水印嵌入和检测算法matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 matlab2022a 3.部分核心程序 ................................................................. for j 1:length(attens…

OpenCV实例(八)车牌字符识别技术(三)汉字识别

车牌字符识别技术&#xff08;三&#xff09;汉字识别 1.代码实例2.遇到问题3.汉字识别代码实例 相较于数字和英文字符的识别&#xff0c;汽车牌照中的汉字字符识别的难度更大&#xff0c;主要原因有以下4个方面&#xff1a; (1)字符笔画因切分误差导致非笔画或笔画流失。 (2…

安装使用IDEA,修改样式,配置服务,构建Maven项目(超级详细版)

目录 前言&#xff1a; 一&#xff0c;安装 1.1打开官网JetBrains: Essential tools for software developers and teams点击 Developer Tools&#xff0c;再点击 Intellij IDEA 2.点击下载​编辑 3.选择对应的版本&#xff0c;左边的 Ultimate 版本为旗舰版&#xff0c;需要…

阿里云服务器搭建Magento电子商务网站图文教程

本文阿里云百科分享使用阿里云服务器手动搭建Magento电子商务网站全流程&#xff0c;Magento是一款开源电商网站框架&#xff0c;其丰富的模块化架构体系及拓展功能可为大中型站点提供解决方案。Magento使用PHP开发&#xff0c;支持版本范围从PHP 5.6到PHP 7.1&#xff0c;并使…

MySQL数据库表的增删查改 - 进阶

一&#xff0c;数据库约束 1.1 约束对象 not null - 该列不能为空unique - 保证该列的每一行都不一样default - 规定没有给列赋值时的默认值&#xff08;自定义&#xff09;primary key - not null 和 unique 的结合&#xff0c;会给该列添加一个索引&#xff0…

CSS3 新特性

圆角阴影文字阴影线性渐变变换&#xff08;transform&#xff09;背景rgba伪元素&#xff1a;伪类 伪元素区别动画&#xff08;animate&#xff09;

中科亿海微浮点数转换定点数

引言 浮点数转换定点数是一种常见的数值转换技术&#xff0c;用于将浮点数表示转换为定点数表示。浮点数表示采用指数和尾数的形式&#xff0c;可以表示较大范围的数值&#xff0c;但存在精度有限的问题。而定点数表示则采用固定小数点位置的形式&#xff0c;具有固定的精度和范…

走进知识图谱(二)【世界知识图谱篇】知识表示的经典模型与平移模型及基于复杂关系建模的知识表示学习

上篇文章提到&#xff0c;该系列文章将主要围绕世界知识图谱和语言知识图谱这两大类知识图谱进行展开&#xff0c;并且提到知识图谱的主要研究包括了知识表示学习、知识自动获取和知识的推理与应用三大部分。今天主要介绍世界知识图谱的知识表示学习&#xff0c;其中包括经典的…

Android 13 Launcher——长按图标弹窗内容修改以及小组件等隐藏起来

目录 一.背景 二.实现思路 三.布局文件修改 四.隐藏代码中原先的view 一.背景 由于定制化开发需要将原先的长按图标原生弹窗界面隐藏,然后显示自定义的弹窗界面,如下就是我们来实现自定义的弹窗界面

redis Set类型命令

Redis中的Set是一种无序、不重复的集合数据结构&#xff0c;它提供了一系列的操作命令用于对Set进行添加、删除和查找等操作。以下是Redis中Set类型常见的一些命令&#xff1a; SADD key member [member …]&#xff1a;将一个或多个成员添加到指定的集合中。 示例&#xff1a;…

day-18 代码随想录算法训练营(19)二叉树 part05

513.找树左下角的值 思路一&#xff1a;层序遍历&#xff0c;每一层判断是不是最后一层&#xff0c;是的话直接返回第一个; 如何判断是不是最后一层呢&#xff0c;首先队列头部&#xff0c;其次记录左右子节点都没有的节点数是不是等于que.size()&#xff1b;或…

【ARM 嵌入式 编译系列 6 -- GCC objcopy, objdump, readelf, nm 介绍】

文章目录 GCC objcopy 简介objcopy 常用参数 GCC objdump 简介GCC readelf 介绍GCC nm 介绍 GCC objcopy 简介 objcopy 是 GNU二进制工具集(binutils)的一部分&#xff0c;主要用于复制和转换目标文件。 在ARM GCC中&#xff0c;arm-none-eabi-objcopy通常用于从链接后的ELF格…

C语言 字符指针

1、介绍 概念&#xff1a; 字符指针&#xff0c;就是字符类型的指针&#xff0c;同整型指针&#xff0c;指针指向的元素表示整型一样&#xff0c;字符指针指向的元素表示的是字符。 假设&#xff1a; char ch a;char * pc &ch; pc 就是字符指针变量&#xff0c;字符指…

数据库--数据类型

数据库相关链接&#xff1a; 数据库基础操作--增删改查&#xff1a;http://t.csdn.cn/189CF 数据库--三大范式、多表查询、函数sql&#xff1a;http://t.csdn.cn/udJSG 数据类型 创建表的时候&#xff0c;我们在类型这里给出了不同的选项&#xff0c;比如有int &#xff0c;…

python num循环怎么从1开始

如何实现python for循环从1开始&#xff1f; range()函数的作用和用法&#xff1a; 编写一个从数值1开始的循环&#xff1a; 执行后得到的结果 其他注意事项

HCIA---动态路由---RIP协议

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 目录 前言 一.动态路由 二.动态路由协议分类 IGP&#xff1a;内部网关协议 EGP:外部网关协议 三.RIP协议概述 RIP版本分类&#xff1a; RIP三要素&#xff1a; RIP…

3.0 Python 迭代器与生成器

当我们需要处理一个大量的数据集合时&#xff0c;一次性将其全部读入内存并处理可能会导致内存溢出。此时&#xff0c;我们可以采用迭代器Iterator和生成器Generator的方法&#xff0c;逐个地处理数据&#xff0c;从而避免内存溢出的问题。 迭代器是一个可以逐个访问元素的对象…