背景:因项目需要导出3万+行,90列+的数据到excel,使用传统的apache poi 直接导出,导致504连接超时无法导出。然后改造方法,异步导出。
一、准备一个导出类,属性有id,outputstrream,finleName,err,exception
public class Export {
private String id;
private OutputStream outputStream;
private String fileName;
private boolean err;
private Exception exception;
}
二、后端controller准备两个方法
(1)pullInfoStream方法用来生成导出流,将workbook对象写入ByteArrayOutputStream中,放入全局map中。请求进来,每个用户生成一个uuid,开启一个线程去处理excel,同时返回uuid。处理完毕后将输出流放入到map中,key是uuid。
private static final Map<String, Export> hashMap = new ConcurrentHashMap<>();
@PostMapping("/export/id")
public String pullInfoStream(String[] fields, QueryExamineeParam queryExamine