poi apache compress实现excel压缩下载
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poi apache compress实现excel压缩下载相关的知识,希望对你有一定的参考价值。
try { //构造40W条数据 List<Branch> list = this.getBranch(); HSSFWorkbook workbook = Service.createHSSFWorkbook(list); String fileName = "中文名字哈哈哈"; OutputStream out = new BufferedOutputStream(response.getOutputStream()); //压缩下载 实测80M 压缩完16M 下载更快些 if (workbook != null) { fileName = fileName+ DateUtil.getExcelDate(new Date()) + ".xls"; response.setContentType( "application/octet-stream "); response.setHeader("Content-Disposition","attachment;filename=\""+ java.net.URLEncoder.encode(fileName, "UTF-8")+".zip"+"\""); ArchiveOutputStream archOuts = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,new BufferedOutputStream(out)); ZipArchiveOutputStream zipOut=(ZipArchiveOutputStream)archOuts; ZipArchiveEntry zipEntry=new ZipArchiveEntry(fileName); zipOut.putArchiveEntry(zipEntry); workbook.write(zipOut); zipOut.closeArchiveEntry(); zipOut.flush(); zipOut.finish(); out.close(); } } catch (Exception e) { logger.error("Controller>>>>>>>>>>>>>报表下载失败",e); } //生成workbook @Override public HSSFWorkbook createHSSFWorkbook(List<Branch> list) { HSSFWorkbook workbook = new HSSFWorkbook(); long totle = list.size();//获取总数,在excel分页 float res=Float.parseFloat(String.valueOf(totle)); int mus=40000; //xls文件一个sheet最多有65536条数据 float avg=res/mus; //分多少sheet页 for (int k = 0; k < avg; k++) { HSSFSheet sheet = workbook.createSheet("第"+k+"页"); sheet.setDefaultColumnWidth(30); sheet.setDefaultRowHeightInPoints(20); HSSFCellStyle style = workbook.createCellStyle(); // 创建字体对象 Font ztFont = workbook.createFont(); HSSFRow row = sheet.createRow(0); String[] headers = { "机构编号", "机构名称", "机构级别", "父机构名称", "机构负责人", "机构负责人联系方式", "机构状态", "座机电话"}; for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } //处理循环sheet页 List<Branch> tempList = null; int last = (int)(avg); if(k==(last-1)){ int index = (int) (k*mus); //tempList = list.subList(index, list.size()); //解决subList导致内存不回收,内存溢出 tempList = this.MySubList(list,index,list.size()); }else{ int index = (int) (k*mus); //tempList = list.subList(index,index+mus); tempList = this.MySubList(list,index,index+mus); } for (int i=1,j=0;j<tempList.size();i++,j++) { row = sheet.createRow(i); Branch br = tempList.get(j); HSSFCell cell = row.createCell(0); this.setCellStyle(style,ztFont,cell); cell.setCellValue(br.getBranchId() == null ? "" : br.getBranchId()); cell = row.createCell(1); this.setCellStyle(style,ztFont,cell); cell.setCellValue(br.getBranchName() == null ? "" : br.getBranchName()); cell = row.createCell(2); this.setCellStyle(style,ztFont,cell); String branchLevel = br.getBranchLevel(); if(branchLevel != null){ if(branchLevel.equals("1")){ cell.setCellValue("A"); }else if(branchLevel.equals("2")){ cell.setCellValue("B"); } }else{ cell.setCellValue(""); } cell = row.createCell(3); this.setCellStyle(style,ztFont,cell); cell.setCellValue(br.getParentbranchName() == null ? "" : br.getParentbranchName()); cell = row.createCell(4); this.setCellStyle(style,ztFont,cell); cell.setCellValue(br.getHeaderName() == null ? "" : br.getHeaderName()); cell = row.createCell(5); this.setCellStyle(style,ztFont,cell); cell.setCellValue(br.getHeaderPhone() == null ? "" : br.getHeaderPhone()); cell = row.createCell(6); this.setCellStyle(style,ztFont,cell); String branchState = br.getBranchState(); if(branchState != null){ if(branchState.equals("1")){ cell.setCellValue("启用"); }else if(branchState.equals("2")){ cell.setCellValue("未启用"); } }else{ cell.setCellValue(""); } cell = row.createCell(7); this.setCellStyle(style,ztFont,cell); cell.setCellValue(br.getBranchPhone() == null ? "" : br.getBranchPhone()); } } return workbook; }
以上是关于poi apache compress实现excel压缩下载的主要内容,如果未能解决你的问题,请参考以下文章