免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2223 | 回复: 0
打印 上一主题 下一主题

excel 导出大数据量问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-20 17:58 |只看该作者 |倒序浏览
看过很多关于Excel导出时出现内存溢出的情况,也有很多解决方案。现提供如下解决方案,如有不妥,请指正:
    该项目使用B/S架构,由于POI、JXL在导出excel大数据量情况下会产生大量对象最终导致内存溢出。其实Excel可以另存为html文件,保存为html后的文件内容如下:
Html代码

  • html xmlns:o="urn:schemas-microsoft-com:office:office"   
  • xmlns:x="urn:schemas-microsoft-com:office:excel"   
  • xmlns="http://www.w3.org/TR/REC-html40">   
  •    
  • head>   
  • meta http-equiv=Content-Type content="text/html; charset=gb2312">   
  • meta name=ProgId content=Excel.Sheet>   
  • meta name=Generator content="Microsoft Excel 11">   
  • ……样式信息……   
  • body link=blue vlink=purple>   
  • table x:str border=0 cellpadding=0 cellspacing=0 width=620 style='border-collapse:   
  • collapse;table-layout:fixed;width:466pt'>   
  • col width=129 style='mso-width-source:userset;mso-width-alt:4128;width:97pt'>   
  • col class=xl25 width=72 span=2 style='width:54pt'>   
  • col class=xl25 width=63 style='mso-width-source:userset;mso-width-alt:2016;   
  • width:47pt'>   
  • col class=xl25 width=118 style='mso-width-source:userset;mso-width-alt:3776;   
  • width:89pt'>   
  • col width=166 style='mso-width-source:userset;mso-width-alt:5312;width:125pt'>   
  • tr height=19 style='height:14.25pt'>   
  •   td height=19 class=xl24 width=129 style='height:14.25pt;width:97pt'>字段1td>   
  •   td class=xl24 width=72 style='width:54pt'>字段2td>   
  •   td class=xl24 width=72 style='width:54pt'>字段3td>   
  •   td class=xl24 width=63 style='width:47pt'>字段4td>   
  •   td class=xl24 width=118 style='width:89pt'>字段5td>   
  •   td width=166 style='width:125pt'>td>   
  • tr>   
  • ……数据……   
  • >   
  • tr height=0 style='display:none'>   
  •   td width=129 style='width:97pt'>td>   
  •   td width=72 style='width:54pt'>td>   
  •   td width=72 style='width:54pt'>td>   
  •   td width=63 style='width:47pt'>td>   
  •   td width=118 style='width:89pt'>td>   
  •   td width=166 style='width:125pt'>td>   
  • tr>   
  • >   
  • table>   
  • body>   
  • html>  
    ……样式信息……






      字段1
      字段2
      字段3
      字段4
      字段5
      

    ……数据……


      
      
      
      
      
      


        至此,可通过数据生成如上格式的HTML文本信息则避开大量对象的建立,如果将该HTML直接以application/excel返回浏览器时则Excel文件会比普通大一点,可以通过配置过滤器对该HTML进行压缩即可,如下:
    Java代码

  •         
  • response.reset();     
  • response.setContentType("application/zip;charset=GBK");   
  • String s = "查询-" + new java.sql.Date(System.currentTimeMillis()).toString().replaceAll("-","") + ".xls";   
  • String filename = s + ".zip";   
  • response.addHeader("Content-Disposition", "inline;filename=" + filename);         
    response.reset();
    response.setContentType("application/zip;charset=GBK");
    String s = "查询-" + new java.sql.Date(System.currentTimeMillis()).toString().replaceAll("-","") + ".xls";
    String filename = s + ".zip";
    response.addHeader("Content-Disposition", "inline;filename=" + filename);
        以上只是一种导出Excel大数据量的解决方案。请大家给予意见!谢谢!


    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103458/showart_2033111.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP