public void exportExcelFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String filePath = "/templates/export/basicData/summary/export/test.xlsx";
InputStream tempIs = this.getClass().getResourceAsStream(filePath);
OutputStream os = null;
try {
String fileName = URLEncoder.encode("附件2 水利工程汛前检查汇总表", "UTF-8");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName.concat(".xlsx"));
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "max-age=0");
os = response.getOutputStream();
ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(tempIs).build();
//插入新增
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//多线程下载
CountDownLatch countDownLatch = new CountDownLatch(9);
final List[] list1 = new List[9];
Map<Integer, Object> paramsList = new HashMap<Integer, Object>();
new Thread(() -> {
//附件2-1 列表数据
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
new Thread(() -> {
//附件2-2 列表数据
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// 附件2-3
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}
}).start();
new Thread(() -> {
// 附件2-4
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
new Thread(() -> {
// 附件2-5
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
new Thread(() -> {
// 附件2-6
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
new Thread(() -> {
// 附件2-7
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
new Thread(() -> {
// 附件2-8
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
new Thread(() -> {
// 附件2-9
List list = new ArrayList();//这边写你自己dao查询list
Map<String, Object> waterParams = new HashMap<>();//这边写除了列表之外的参数
list1[0] = list;
paramsList.put(0,waterParams);
countDownLatch.countDown();
}).start();
countDownLatch.await(5, TimeUnit.MINUTES);
for (int i=0;i<list1.length;i++) {
WriteSheet writeSheet = EasyExcel.writerSheet(i).build();
excelWriter.fill(paramsList.get(i), writeSheet);
excelWriter.fill(list1[i],fillConfig, writeSheet);
}
excelWriter.finish();
os.flush();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}finally {
os.close();
tempIs.close();
}
}
提示:每个线程里面最好cry catch 一下,把countDownLatch.countDown();放finally里面,避免有异常导致countDownLatch.await(5, TimeUnit.MINUTES)等待时间过长问题。