easyexcel同步下载简单使用

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 {

	/**
	 * 行号
	 */
	@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;
    }

}

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值