Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13877
  • 博文数量: 12
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 15:05
文章分类

全部博文(12)

文章存档

2011年(1)

2009年(11)

我的朋友
最近访客

分类: Java

2009-08-20 17:58:51

看过很多关于Excel导出时出现内存溢出的情况,也有很多解决方案。现提供如下解决方案,如有不妥,请指正:
    该项目使用B/S架构,由于POI、JXL在导出excel大数据量情况下会产生大量对象最终导致内存溢出。其实Excel可以另存为html文件,保存为html后的文件内容如下:
Html代码 复制代码
  1. <html xmlns:o="urn:schemas-microsoft-com:office:office"  
  2. xmlns:x="urn:schemas-microsoft-com:office:excel"  
  3. xmlns="">  
  4.   
  5. <head>  
  6. <meta http-equiv=Content-Type content="text/html; charset=gb2312">  
  7. <meta name=ProgId content=Excel.Sheet>  
  8. <meta name=Generator content="Microsoft Excel 11">  
  9. ……样式信息……   
  10. <body link=blue vlink=purple>  
  11. <table x:str border=0 cellpadding=0 cellspacing=0 width=620 style='border-collapse:   
  12.  collapse;table-layout:fixed;width:466pt'>  
  13.  <col width=129 style='mso-width-source:userset;mso-width-alt:4128;width:97pt'>  
  14.  <col class=xl25 width=72 span=2 style='width:54pt'>  
  15.  <col class=xl25 width=63 style='mso-width-source:userset;mso-width-alt:2016;   
  16.  width:47pt'>  
  17.  <col class=xl25 width=118 style='mso-width-source:userset;mso-width-alt:3776;   
  18.  width:89pt'>  
  19.  <col width=166 style='mso-width-source:userset;mso-width-alt:5312;width:125pt'>  
  20.  <tr height=19 style='height:14.25pt'>  
  21.   <td height=19 class=xl24 width=129 style='height:14.25pt;width:97pt'>字段1td>  
  22.   <td class=xl24 width=72 style='width:54pt'>字段2td>  
  23.   <td class=xl24 width=72 style='width:54pt'>字段3td>  
  24.   <td class=xl24 width=63 style='width:47pt'>字段4td>  
  25.   <td class=xl24 width=118 style='width:89pt'>字段5td>  
  26.   <td width=166 style='width:125pt'>td>  
  27.  tr>  
  28. ……数据……   
  29.  >  
  30.  <tr height=0 style='display:none'>  
  31.   <td width=129 style='width:97pt'>td>  
  32.   <td width=72 style='width:54pt'>td>  
  33.   <td width=72 style='width:54pt'>td>  
  34.   <td width=63 style='width:47pt'>td>  
  35.   <td width=118 style='width:89pt'>td>  
  36.   <td width=166 style='width:125pt'>td>  
  37.  tr>  
  38.  >  
  39. table>  
  40. body>  
  41. html>  

    至此,可通过数据生成如上格式的HTML文本信息则避开大量对象的建立,如果将该HTML直接以application/excel返回浏览器时则Excel文件会比普通大一点,可以通过配置过滤器对该HTML进行压缩即可,如下:
Java代码 复制代码
  1.        
  2. response.reset();    
  3. response.setContentType("application/zip;charset=GBK");   
  4. String s = "查询-" + new java.sql.Date(System.currentTimeMillis()).toString().replaceAll("-","") + ".xls";   
  5. String filename = s + ".zip";   
  6. response.addHeader("Content-Disposition""inline;filename=" + filename);  

    以上只是一种导出Excel大数据量的解决方案。请大家给予意见!谢谢!
阅读(1258) | 评论(1) | 转发(0) |
0

上一篇:exception handler

下一篇:XML Schema choice 元素

给主人留下些什么吧!~~

chinaunix网友2009-09-10 10:14:59

head 没有结束。。