zoukankan      html  css  js  c++  java
  • 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();
                }
    

      

  • 相关阅读:
    极简风格的LOGO,收集一波!
    如何利用AI软件中的混合器工具制作文字
    国内有哪些非常有设计感的 App?
    如何用PS制作花型背景图
    怎样去调整摄影作品的背景颜色
    如何用PS把数码拍摄的荷花照片制作成中国风的效果
    PS 抠人像如何抠得干净?
    如何用 Ps 制作毛玻璃穿透效果?
    化装品经过这样PS包装,身价立马翻10倍
    有哪些漂亮的中国风 LOGO 设计?
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/10947636.html
Copyright © 2011-2022 走看看