导出.zip

控制层

@PostMapping("/questionListExport")
@ApiOperation(httpMethod = "POST",value = "",notes = "列表导")
public WGJsonResult questionListExport(@RequestBody BackQuestionExport param, HttpServletResponse response) throws Exception {
    return backQuestionService.questionListExport(param,response);
}

服务层

@Override
public WGJsonResult questionListExport(BackQuestionExport param, HttpServletResponse response) throws IOException {
    PageHelper.startPage(param.getPageNo(),param.getPageSize());
   List eports = backQuestionMapper.questionListExport(param);
   Map> dataMap = eports.stream().collect(Collectors.groupingBy(p->p.getInvitName()+"_"+p.getInvitId(), Collectors.toList()));
   exportExcel(response, dataMap);
  //导出之后记录日志 ask_exp_imp_record
    addLog(eports,1);
   return success("下载成功");
}

private void exportExcel(HttpServletResponse response, Map> map) throws IOException {
    SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
    String nowDate = sdf4.format(new Date());
    String fileName = nowDate + ".zip";
    // 获取项目根路径
    String rootPath =System.getProperty("user.dir")+"_"+nowDate;
//        System.out.println("-----------"+System.getProperty("user.dir"));
    File f = new File(rootPath);
    if (!f.exists()) {
        f.mkdirs();
    }
    String tempPath = "";
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(rootPath + File.separator + fileName));
    try {
        for (Map.Entry> entry : map.entrySet()) {
            String k = entry.getKey();
            List value = entry.getValue();
            tempPath = rootPath + File.separator + k + ".xlsx";
            EasyExcel.write(tempPath, BackQuestionExport.class).sheet("问答数据").doWrite(value);
            //创建输入流读取文件
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(tempPath));
            //将文件写入zip内,即将文件进行打包
            zipOutputStream.putNextEntry(new ZipEntry(nowDate+File.separator+k + ".xlsx"));
            //写入文件的方法,同上
            int size = 0;
            byte[] buffer = new byte[1024];  //设置读取数据缓存大小
            while ((size = bis.read(buffer)) > 0) {
                zipOutputStream.write(buffer, 0, size);
            }
            bis.close();
        }
        //关闭数据流,注意关闭的顺序
        zipOutputStream.close();
        FileInputStream fis = new FileInputStream(rootPath + File.separator + fileName);
        BufferedInputStream buff = new BufferedInputStream(fis);
        byte[] b = new byte[1024];// 相当于我们的缓存
        int k = 0;// 该值用于计算当前实际下载了多少字节
        OutputStream out = response.getOutputStream();// 从response对象中得到输出流,准备下载
        // 开始循环下载
        while ((k = buff.read(b)) > 0) {
            out.write(b, 0, k);
        }
        out.close();
        buff.close();

    } catch (Exception e) {
        System.out.println(e.fillInStackTrace());
    } finally {
        if (!f.exists())
            return;
        if (f.isFile()) {
            f.delete();
            return;
        }
        File[] files = f.listFiles();
        for (int i = 0; i < files.length; i++) {
            files[i].delete();
        }
        f.delete();
    }
}

sql

你可能感兴趣的:(excelspringboot)