zcl_ccc 发表于 2018-11-20 06:40:52

poi apache compress实现excel压缩下载

try {
//构造40W条数据
    List 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 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);
      cell.setCellValue(text);
}
//处理循环sheet页
    List 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
页: [1]
查看完整版本: poi apache compress实现excel压缩下载