• jeecg项目将workbook 的Excel流添加到zip压缩包里导出


    1、直接献出代码

    Map<String,List<ConfidentialInformation>> typeMap = new HashMap<>();
    try {
                    //设置导出
                    response.addHeader("Cache-Control","no-cache");
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("application/octet-stream;charset=UTF-8");
                    String ua = request.getHeader("user-agent");
                    ua = ua == null ? null : ua.toLowerCase();
                    String docFileName = dateMonth.toString()+"月凭证信息情况.zip";
                    if(ua != null && (ua.indexOf("firefox") > 0 || ua.indexOf("safari")>0)){
                        try {
                            docFileName = new String(docFileName.getBytes(),"ISO8859-1");
                            response.addHeader("Content-Disposition","attachment;filename=" + docFileName);
                        } catch (Exception e) {
                        }
                    }else{
                        try {
                            docFileName = URLEncoder.encode(docFileName, "utf-8");
                            response.addHeader("Content-Disposition","attachment;filename=" + docFileName);
                        } catch (Exception e) {
                        }
                    }
    
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    ZipOutputStream zipOut = new ZipOutputStream(out);
    
                    byte[]buffer=new byte[1024];
                    for (String names:typeMap.keySet()){
                        Map<String,Object> outMaps = new HashMap<>();
    
                        outMaps.put( "datac",typeMap.get( names ) );
    
                        //Excel处理成zip包
                        XLSTransformer transformer = new XLSTransformer();
                        String srcFilePath = request.getServletContext().getRealPath("/")+"/export/template/credentialReport.xls";
    
                        InputStream is = new BufferedInputStream(new FileInputStream(srcFilePath));
                        Workbook workbook =null;
    
                        workbook = transformer.transformXLS(is, outMaps);
    
    
                        //压缩包zip留。
                        ByteArrayOutputStream outtemp = new ByteArrayOutputStream();
                        workbook.write(outtemp);
                        ByteArrayInputStream bais =new ByteArrayInputStream(outtemp.toByteArray());
                        String nickname = ResourceUtil.getCacheDicDetail("apportion",names,"typename");
                        zipOut.putNextEntry(new ZipEntry(nickname+"凭证信息表.xls"));
                        int dataLen;
                        //读入需要下载的文件的内容,打包到zip文件
                        while((dataLen=bais.read(buffer))>0){
                            zipOut.write(buffer,0,dataLen);
    
                        }
                        zipOut.closeEntry();
                        bais.close();
                        outtemp.close();
                        is.close();
    
    
                    }
                    zipOut.close();
                    out.close();
    
                    byte[] zipByte=out.toByteArray();
    
    
                    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                    toClient.write(zipByte);
                    toClient.flush();
    
    
    
                }catch (Exception e){
                    e.printStackTrace();
                }
    

      

  • 相关阅读:
    工科物理实验()中国大学MOOC答案(已更新)
    类似jar文件使用java无法打开问题
    python9、10章
    nmap的理解与利用(初级)
    常见端口
    配置优化
    删除表操作
    万能的map
    测试
    Mapper.xml
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/10947636.html
Copyright © 2020-2023  润新知