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(); }
自动化学习。