需求
easyexcel实现excel导入和导出,你百分百遇到过这类需求。有时候我们需要快速写个demo来验证下自己的猜想,全网找代码还是挺麻烦的,这里我给了一个例子供大家使用。
代码实现
pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
本地测试
package com.example.demo.easyexcel;
import com.alibaba.excel.EasyExcel;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
public class TestWrite {
@Test
public void write() {
//表示Excel要存储的位置,这里我存在当前项目下
String fileName = "D:\home\easyexcel.xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
List<ExcelDemo> data = new ArrayList<>();
data.add(new ExcelDemo(01,"张三",18,"海南"));
data.add(new ExcelDemo(02,"李四",19,"大理"));
data.add(new ExcelDemo(03,"王五",20,"西藏"));
EasyExcel.write(fileName, ExcelDemo.class).sheet("第一个表格").doWrite(data);
}
@Test
public void read() {
String fileName = "D:\home\easyexcel.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(fileName, ExcelDemo.class, new DemoDataListener()).sheet().doRead();
}
}
实体类
package com.example.demo.easyexcel;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExcelDemo {
//value表示表头名字
@ExcelProperty(value = "编号")
private Integer id;
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "年龄")
private Integer age;
@ExcelIgnore//表示不显示在Excel表格里
private String address;
}
web下载
import com.alibaba.excel.EasyExcel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @program: easyexcel
* @description:
* @author: liuzhw
* @create: 2023-05-03 16:20
**/
@Controller
public class UploadController {
/**
* 下载导出
*/
@GetMapping("download")
public void downloadFailedUsingJson(HttpServletResponse response) throws IOException {
// 这里需要设置不关闭流
List<ExcelDemo> data = new ArrayList<>();
data.add(new ExcelDemo(01,"张三",18,"海南"));
data.add(new ExcelDemo(02,"李四",19,"大理"));
data.add(new ExcelDemo(03,"王五",20,"西藏"));
//1.创建字节流
ByteArrayOutputStream os = new ByteArrayOutputStream();
//2、excel写入字节流
EasyExcel.write(os, ExcelDemo.class).autoCloseStream(Boolean.FALSE).sheet("sheet01").doWrite(data);
//3、下载导出
DownloadUtils.download(os,response,"人事报表.xlsx");
}
/**
* 读取excel
*/
@GetMapping("read")
public void read(MultipartFile file) throws IOException{
//1 获取文件输入流
InputStream inputStream = file.getInputStream();
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(inputStream, ExcelDemo.class, new DemoDataListener()).sheet().doRead();
}
}
下载工具类
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
public class DownloadUtils {
public static void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
response.setContentType("application/octet-stream");
returnName = URLEncoder.encode(returnName, "UTF-8");
//加上就是下载 否则就是预览
response.addHeader("Content-Disposition","attachment;filename="+new String(returnName.getBytes("ISO8859-1")));
response.setContentLength(byteArrayOutputStream.size());
response.addHeader("Content-Length", "" + byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream();
byteArrayOutputStream.writeTo(outputstream);
byteArrayOutputStream.close();
outputstream.flush();
}
}
解析excel类
package com.example.demo.easyexcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
// 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
@Slf4j
public class DemoDataListener implements ReadListener<ExcelDemo> {
/**
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 100;
/**
* 缓存的数据
*/
private List<ExcelDemo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
public DemoDataListener() {
}
/**
* 这个每一条数据解析都会来调用
*
* @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
* @param context
*/
@Override
public void invoke(ExcelDemo data, AnalysisContext context) {
log.info("解析到一条数据:{}", JSON.toJSONString(data));
cachedDataList.add(data);
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
if (cachedDataList.size() >= BATCH_COUNT) {
saveData();
// 存储完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
/**
* 所有数据解析完成了 都会来调用
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 这里也要保存数据,确保最后遗留的数据也存储到数据库
saveData();
log.info("所有数据解析完成!");
}
/**
* 加上存储数据库
*/
private void saveData() {
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
log.info("存储数据库成功!");
}
}
原文始发于微信公众号(干货食堂):easyexcel实现excel导入和导出
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/258474.html