easyexcel同步下载简单使用

news/2024/10/20 11:53:38/

1.加pom

        <!-- 阿里EasyExcel --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.1</version></dependency>

2. 写工具类

package com.fn.health.common.utils;import com.fn.health.common.domain.DownloadResultDomain;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;public class WebUtil {/*** 下载文件需要的必要设置** @param res* @param downloadResultDomain*/public static void downloadFile(HttpServletResponse res, DownloadResultDomain downloadResultDomain) {OutputStream out;InputStream inputStream;try {res.setContentType("application/octet-stream");/** 读取服务器端模板文件*/res.setHeader("Content-Disposition","attachment;fileName=" + URLEncoder.encode(downloadResultDomain.getFileName(), "utf-8").replaceAll("\\+", "%20"));inputStream = downloadResultDomain.getInputStream();out = res.getOutputStream();int b = 0;byte[] buffer = new byte[1024];while ((b = inputStream.read(buffer)) != -1) {// 4.写到输出流(out)中out.write(buffer, 0, b);}inputStream.close();if (out != null) {out.flush();out.close();}} catch (IOException e) {e.printStackTrace();}}public static void showImage(HttpServletResponse res,InputStream inputStream){OutputStream out;try {res.setContentType("image/png");out = res.getOutputStream();int b = 0;byte[] buffer = new byte[1024];while ((b = inputStream.read(buffer)) != -1) {// 4.写到输出流(out)中out.write(buffer, 0, b);}inputStream.close();if (out != null) {out.flush();out.close();}} catch (IOException e) {e.printStackTrace();}}}
package com.fn.health.common.domain;import com.fn.health.common.bean.UploadDataBean;
import lombok.Data;import java.util.ArrayList;
import java.util.List;@Data
public class UploadDataResultDomain {/*** 错误数量*/private int errorCount = 0;/*** 完成数量*/private int successCount = 0;/**上传错误数据*/private List<UploadDataBean> uploadErrorData = new ArrayList<>();/*** 导入的批次号*/private String importNo;public void errorAdd() {errorCount++;}public void successAdd() {successCount++;}}
package com.fn.health.common.domain;import lombok.Data;import java.io.InputStream;
import java.io.Serializable;/*** @author owen.chen* @date 2020/9/27 9:18*/
@Data
public class DownloadResultDomain implements Serializable {public DownloadResultDomain(String fileName, InputStream inputStream) {this.fileName = fileName;this.inputStream = inputStream;}private String fileName;private InputStream inputStream;}
package com.fn.health.common.bean;import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;public abstract class UploadDataBean {/*** 行号*/@ExcelIgnoreprotected Long excelNumberNo;/*** 记录是否空行*/@ExcelIgnoreprotected Boolean isBlank;/*** 判定记录是否有效*/@ExcelIgnoreprotected Boolean isValid;/*** 上传的数据一次*/@ExcelProperty(value = "错误详情")protected String uploadErrorMsg;/// getter,setterpublic Long getExcelNumberNo() {return excelNumberNo;}public void setExcelNumberNo(Long excelNumberNo) {this.excelNumberNo = excelNumberNo;}public Boolean getBlank() {return isBlank;}public void setBlank(Boolean blank) {isBlank = blank;}public Boolean getValid() {return isValid;}public void setValid(Boolean valid) {isValid = valid;}public String getUploadErrorMsg() {return uploadErrorMsg;}public void setUploadErrorMsg(String uploadErrorMsg) {this.uploadErrorMsg = uploadErrorMsg;}}

3.写个例子测试

List<Map<String, Object>> replyList = rwsPatientCaseService.queryRecruitReplyList(recruitId, doctorId, status);//设置分页数据RwsPatientCaseHandler.download(replyList,response);
package com.fn.health.download.rwsPatientCase;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fn.health.common.domain.DownloadResultDomain;
import com.fn.health.common.utils.WebUtil;import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;public class RwsPatientCaseHandler {public  static void download(List<Map<String, Object>> replyList, HttpServletResponse response){List<RwsPatientCaseExcel> excelList=RwsPatientCaseExcel.ToList(replyList);ByteArrayOutputStream out = new ByteArrayOutputStream();EasyExcel.write(out, RwsPatientCaseExcel.class).sheet("导出数据").doWrite(excelList);ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());DownloadResultDomain downloadResultDomain = new DownloadResultDomain("患者数据.xlsx", byteArrayInputStream);WebUtil.downloadFile(response, downloadResultDomain);}
}

http://www.ppmy.cn/news/1116555.html

相关文章

JPEG算法及例程

JPEG&#xff08;Joint Photographic Experts Group&#xff09;是一种常见的图像压缩算法&#xff0c;用于减小图像文件的大小。它是一种有损压缩算法&#xff0c;即通过牺牲一定的图像质量来实现压缩。 以下是一个简单的JPEG压缩算法的例程&#xff1a; 将输入图像转换为YUV…

什么是AJAX?如何使用原生JavaScript进行AJAX请求?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 什么是AJAX&#xff1f;⭐如何使用原生JavaScript进行AJAX请求&#xff1f;⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 欢迎来到前端入门之旅&#xff01;感兴趣的可以订阅本专栏哦&#xff01;这个专栏是为…

vscode:连接服务器进行远程开发调试

以下推荐一个使用远端服务器开发调试的方法&#xff0c;可以让我们像在本地编写调试程序一样简单&#xff1a;使用vscode vscode-remote插件 一、安装SSH 1.windows自带的openssh经测试不可用&#xff0c;建议先安装git&#xff0c;使用git中自带的ssh&#xff0c;方法如下&a…

华为HCIA(五)

Vlan id 在802.1Q中 高级ACL不能匹配用户名和源MAC 2.4G频段被分为14个交叠的&#xff0c;错列的20MHz信道&#xff0c;信道编码从1到14&#xff0c;邻近的信道之间存在一定的重叠范围 STA通过Probe获取SSID信息 Snmp报文 网络管理设备异常发生时会发送trap报文 D类地址是…

数据可视化 -- ECharts 入门

文章目录 引言1. ECharts的基本使用1.1 ECharts的快速上手1.2 相关配置讲解 2. ECharts常用图表2.1 图表1 柱状图2.1.1 柱状图的实现步骤2.1.2 柱状图的常见效果2.1.3 柱状图特点2.1.4 通用配置 2.2 图表2 折线图2.2.1 折线图的实现步骤2.2.2 折线图的常见效果2.2.3 折线图的特…

k8s储存卷

卷的类型 In-Tree存储卷插件 ◼ 临时存储卷 ◆emptyDir ◼ 节点本地存储卷 ◆hostPath, local ◼ 网络存储卷 ◆文件系统&#xff1a;NFS、GlusterFS、CephFS和Cinder ◆块设备&#xff1a;iSCSI、FC、RBD和vSphereVolume ◆存储平台&#xff1a;Quobyte、PortworxVolume、Sto…

循环神经网络-02文本预处理

https://zh-v2.d2l.ai/chapter_recurrent-neural-networks/text-preprocessing.html 本节中&#xff0c;我们将解析文本的常见预处理步骤。 这些步骤通常包括&#xff1a; 将文本作为字符串加载到内存中。 将字符串拆分为词元&#xff08;如单词和字符&#xff09;。 建立一个…

CPP-Templates-2nd--第 23 章 元编程

目录 23.1 现代 C元编程的现状 2.3.1.1 值元编程&#xff08;Value Metaprogramming&#xff09; 23.1.2 类型元编程 23.1.3 混合元编程 23.1.4 将混合元编程用于“单位类型” 23.2 反射元编程的维度 23.3 递归实例化的代价 23.3.1 追踪所有的实例化过程 23.4 计算完…