1.加pom
<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 {
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) {
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) {
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;
@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;
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);
}
}