多线程导出EXCEL多sheet

该代码示例展示了如何在Java中使用EasyExcel库来导出Excel文件。通过创建多个线程,分别处理不同的数据列表,然后合并到同一模板中,实现了多线程下载。同时,使用CountDownLatch进行同步,确保所有线程执行完毕后再完成Excel的填充和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)等待时间过长问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值