• easyexcel同步下载简单使用


    1.加pom

            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>easyexcelartifactId>
                <version>3.1.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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();
    		}
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    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++;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    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;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    package com.fn.health.common.bean;
    
    import com.alibaba.excel.annotation.ExcelIgnore;
    import com.alibaba.excel.annotation.ExcelProperty;
    
    public abstract class UploadDataBean {
    
    	/**
    	 * 行号
    	 */
    	@ExcelIgnore
    	protected Long excelNumberNo;
    	/**
    	 * 记录是否空行
    	 */
    	@ExcelIgnore
    	protected Boolean isBlank;
    	/**
    	 * 判定记录是否有效
    	 */
    	@ExcelIgnore
    	protected Boolean isValid;
        /**
         * 上传的数据一次
         */
    	@ExcelProperty(value = "错误详情")
        protected String uploadErrorMsg;
    
    	/// getter,setter
    
    	public 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;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    3.写个例子测试

    List<Map<String, Object>> replyList = rwsPatientCaseService.queryRecruitReplyList(recruitId, doctorId, status);
                //设置分页数据
                RwsPatientCaseHandler.download(replyList,response);
    
    • 1
    • 2
    • 3
    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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    阿里云存储解决方案,助力轻舟智航“将无人驾驶带进现实”
    网络必知题目
    17.2、JavaWeb-简介、JDBC的缺点、入门使用、mybatis的使用、条件查询、mybatis的参数传递
    ThinkPHP6.0 Session 问题
    【JVM系列】- 寻觅·方法区的内容
    离子液体定制|甜菜碱离子液体[BetaineC16][H2PO4]修饰猪胰脂肪酶(PPL)
    imx6ul链接地址、运行地址、加载地址、位置无关、mmu的关系
    R可视乎|灯芯柱状图代码解读
    C语言的特点
    【Python】PyWebIO 初体验:用 Python 写网页
  • 原文地址:https://blog.csdn.net/qq_39432715/article/details/133034963