一、Excel导入导出的应用场景
1、数据导入:减轻录入工作量
2、数据导出:统计信息归档
3、数据传输:异构系统之间数据传递
二、EasyExcel简介
1、EasyExcel特点
Java领域解析、生成Excel比较有名的框架有Apache poi、jxl等。但他们都存在一个严重的问题就是非常的耗内存。如果你的系统并发量不大的话可能还行,但是一旦并发上来后一定会OOM或者JVM频繁的full gc。
EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。EasyExcel能大大减少占用内存的主要原因是在解析Excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一行行读取数据,逐个解析。
EasyExcel采用一行一行的解析模式,并将一行的解析结果以观察者的模式通知处理(AnalysisEventListener)。
使用easyExcel进行写操作
思维概况:
0、首先引入easyExcel的导出依赖 和 添加名称的工具类
1、编写映射实体类也就是编写需要导出的数据的列
2、在mapper类中编写指定的导出数据的方法
3、再编写ExcelController指定路径导出的方法
第一步:引入easyExcel的pom依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
第二步创建一个和Excel对应的一个实体类
/**
* @author huiXing
* @version 1.0.0
* @description 设置excel表头的名称
*/
@Data
@ContentRowHeight(15) //表格行高
@HeadRowHeight(20) //表头行高
@ColumnWidth(25) //列宽
public class DemoData {
//设置excel表头的名称
@ExcelProperty("学生的编号")
private Integer sno;
@ExcelProperty("学生的姓名")
private String sname;
}
创建测试类:
package com.atguigu.demo.excel;
import com.alibaba.excel.EasyExcel;
import java.util.ArrayList;
import java.util.List;
/**
* @author huiXing
* @version 1.0.0
* @description
*/
public class TestEasyExcel {
public static void main(String[] args) {
// 实现excel写的操作
// 1、设置写入文件夹地址和excel文件名称
String fileName = "E:\\write.xlsx";
// 2、调用easyExcel里面的方法实现写操作 第一个参数是文件的名称,第二个参数是实体类的class
EasyExcel.write(fileName, DemoData.class).sheet("学生列表").doWrite(getData());
}
// 创建一个方法返回list集合
private static List<DemoData> getData() {
List<DemoData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DemoData demoData = new DemoData();
demoData.setSno(i);
demoData.setSname("lucy" + i);
list.add(demoData);
}
return list;
}
}
使用easyExcel读操作
第一步 创建和excel对应实体类,标记对应列关系
@Data
public class DemoData {
//设置excel表头的名称
@ExcelProperty(value = "学生的编号",index = 0)
private Integer sno;
@ExcelProperty(value = "学生的姓名",index = 1)
private String sname;
}
第二步创建监听类进行excel文件读取
package com.atguigu.demo.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.Map;
/**
* @author huiXing
* @version 1.0.0
* @description 创建监听
*/
public class ExcelListener extends AnalysisEventListener<DemoData> {
//一行一行的读取excel内容
@Override
public void invoke(DemoData demoData, AnalysisContext analysisContext) {
System.out.println("***"+demoData);
//在这里进行SQL语句的插入操作,不建议一条条插入,最好是批量一次性插入
}
//读取表头内容
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头:"+headMap);
}
//读取完成之后做的事情
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
第三步进行方法的调用
@Test
void contextLoads() {
//实现excel读操作
String fileName = "E:\\write.xlsx";
EasyExcel.read(fileName, DemoData.class,new ExcelListener()).sheet().doRead();
}
使用easyExcel导出图片
第一步导入架包:
<!--进行excel架包-->
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
第二步编辑实体类:
/**
* @author huiXing
* @version 1.0.0
* @description 测试图片
*/
@Data
@ContentRowHeight(100)
@ColumnWidth(100 / 8)
public class ImageData {
// private File file;
// private InputStream inputStream;
/**
* 如果string类型 必须指定转换器,string默认转换成string
*/
// @ExcelProperty(converter = StringImageConverter.class)
// private String string;
// private byte[] byteArray;
/**
* 根据url导出
* @since 2.1.1
*/
private URL url;
}
第三部编辑测试类:
package com.fuyuan.ssm;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
import com.fuyuan.ssm.dao.InventoryDao;
import com.fuyuan.ssm.dao.Product2Dao;
import com.fuyuan.ssm.pojo.ImageData;
import com.fuyuan.ssm.pojo.Inventory;
import com.fuyuan.ssm.pojo.excel.InventoryDemo;
import com.fuyuan.ssm.service.InventoryService;
import com.fuyuan.ssm.utils.TestFileUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* @author huiXing
* @version 1.0.0
* @description
*/
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Junit {
@Autowired
private InventoryService inventoryService;
@Autowired
private InventoryDao inventoryDao;
@Autowired
private Product2Dao product2Dao;
/**
* 图片导出
* <p>
* 1. 创建excel对应的实体对象
* <p>
* 2. 直接写即可
*/
@Test
public void imageWrite() throws Exception {
//生成excel的路径
String fileName = "E:\\image.xlsx";
List<ImageData> list = new ArrayList<ImageData>();
ImageData imageData = new ImageData();
list.add(imageData);
//放你的图片url地址
imageData.setUrl(new URL( "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.wotucdn.com%2F18%2F93%2F18%2F18931874-bbfd4c21f0421f2dc034a85219c232d6-0.jpg&refer=http%3A%2F%2Fbpic.wotucdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621675114&t=c6327627920bcb4020f2b77fee84c91e"));
EasyExcel.write(fileName, ImageData.class).sheet().doWrite(list);
}
}
如果得到帮助了,麻烦点个赞!
如有不明白的请留意,作者会在加完班之后或者吃饭的时候查看一下信息
如果你想了解更多:https://github.com/alibaba/easyexcel