easyexcel同步下载简单使用

1.加pom

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>easyexcelartifactId>
            <version>3.1.1version>
        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);
    }
}

你可能感兴趣的:(easyexcel,excel,java)