批量导出导出是多个excel或者单个excel问题

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void particularsDownloadExcel(String combination) throws BaseException {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse();
boolean flag;
try {

        String[] reportGroups = combination.split(",");
        String reportId = reportGroups[0];
        String submitFreqID = reportGroups[2];
        //产品品种
        String productTypeName = reportGroups[3];
        String cpCode = reportGroups[4];
        String[] finPfIds = reportGroups[1].split("_");
        Map<String, Workbook> workbookMap = new HashMap<>();
        Map<String, String> combinationMap;
        ExecutorService pool = Executors.newCachedThreadPool();
        long bt = System.currentTimeMillis();

        map.put("submitFreqID", submitFreqID);
        map.put("cpCode", cpCode);
        map.put("reportId", reportId);

        for (int i = 0; i < finPfIds.length; i++) {
            combinationMap = new HashMap<>(16);
            combinationMap.put("reportId", reportId);
            combinationMap.put("FIN_PF_ID", finPfIds[i]);
            combinationMap.put("submitFreqID", submitFreqID);
            combinationMap.put("cpCode", cpCode);
            Map<String, Workbook> tempMap = downloadExcel1(combinationMap, productTypeName);
            for (String fileName : tempMap.keySet()) {
                workbookMap.put(fileName, tempMap.get(fileName));
            }
        }

        String path = PBCParticularsDownloadBiz.class.getClassLoader().getResource("").getFile() + "tempZip";
        OutputStream out = new BufferedOutputStream(response.getOutputStream());
        //如果生成多个Excel压缩成一个zip文件 如果生成一个excel 直接导出
        if (workbookMap.size() > 1) {
            //压缩后的名字
            String targetFile = "多个组合" + ".zip";
            response.setContentType("application/octet-stream;charset=utf-8");
            String fileName = URLEncoder.encode(targetFile, "utf-8");
            response.addHeader("Content-Disposition", "attachment; filename="
                    + fileName + "; filename*=utf-8''" + fileName);
            File zipFile = toZip(workbookMap, targetFile, path);
            FileInputStream fis = new FileInputStream(zipFile);
            byte[] buffer = new byte[1024];
            while (fis.read(buffer) != -1) {
                out.write(buffer);
            }
        } else {
            for (String targetFile : workbookMap.keySet()) {
                Workbook workbook = workbookMap.get(targetFile);
                // 设置response的Header
                response.setContentType("application/-excel;charset=utf-8");
                String fileName = URLEncoder.encode(targetFile + ".xls", "utf-8");
                response.addHeader("Content-Disposition", "attachment; filename="
                        + fileName + "; filename*=utf-8''" + fileName);
                workbook.write(out);
            }
        }
        out.flush();
        out.close();
        //删除生成后的临时zip文件
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                files[i].delete();
            }
            file.delete();
        }
        flag = true;
    } catch (Exception ioe) {
        log.error("导出失败", ioe);
        flag = false;
    }
}

/**
 * @return void
 * @throws RuntimeException 压缩失败会抛出运行时异常
 * @Author 
 * @Description //压缩成ZIP 方法
 * @Date 2019/2/16 20:18
 * @Param [workbookMap, zipName]
 **/
public static File toZip(Map<String, Workbook> workbookMap, String zipName, String tempFilePath) throws BaseException {
    List<File> srcFiles = new ArrayList<>();
    for (String workbookName : workbookMap.keySet()) {
        try {
            File pathFile = new File(tempFilePath);
            if (!pathFile.exists()) {
                pathFile.mkdir();
            }
            File file = new File(tempFilePath + "/" + workbookName + ".xls");
            OutputStream os = new FileOutputStream(file);
            workbookMap.get(workbookName).write(os);
            os.flush();
            os.close();
            srcFiles.add(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    File file = new File(tempFilePath + "/" + zipName);
    OutputStream out = null;
    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    long start = System.currentTimeMillis();
    ZipOutputStream zos = null;
    try {
        zos = new ZipOutputStream(out);
        for (File srcFile : srcFiles) {
            byte[] buf = new byte[BUFFER_SIZE];
            zos.putNextEntry(new ZipEntry(srcFile.getName()));
            int len;
            FileInputStream in = new FileInputStream(srcFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            in.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("压缩完成,耗时:" + (end - start) + " ms");
    } catch (Exception e) {
        throw new BaseException("zip error from ZipUtils", e);
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return file;
}

/**
 * @return
 * @Author zhanghaoxing
 * @Description //查询字典的  填报机构名称,社会信用代码,金融机构编码
 * @Date 2019/2/24 20:33
 * @Param
 **/
public Map<String, String> selectReportForm() {
    HashMap<String, String> objectObjectHashMap = new HashMap<>();
    List<LinkedHashMap<String, String>> linkedHashMaps = pBCDExcelMapper.selectReportForm();

    for (int i = 0; i < linkedHashMaps.size(); i++) {
        LinkedHashMap<String, String> map1 = linkedHashMaps.get(i);
        String keyId = null;
        String keyName = null;
        for (Map.Entry<String, String> entry : map1.entrySet()) {
            if (entry.getKey().equals("KEY_ID")) {
                keyId = entry.getValue();
                objectObjectHashMap.put(keyId, null);
            } else if (entry.getKey().equals("KEY_NAME")) {
                keyName = entry.getValue();
                objectObjectHashMap.put(keyId, keyName);
            }
        }
    }
    return objectObjectHashMap;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值