`
hello_wangyi
  • 浏览: 2444 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

转载!!!java操作Excel导出大数据量解决方案

poi 
阅读更多
看过很多关于Excel导出时出现内存溢出的情况,也有很多解决方案。现提供如下解决方案,如有不妥,请指正:
    该项目使用B/S架构,由于POI、JXL在导出excel大数据量情况下会产生大量对象最终导致内存溢出。其实Excel可以另存为html文件,保存为html后的文件内容如下: Html代码
<html xmlns="urn:schemas-microsoft-comfficeffice" 
xmlns:x="urn:schemas-microsoft-comffice: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'>字段1</td> 
  <td class=xl24 width=72 style='width:54pt'>字段2</td> 
  <td class=xl24 width=72 style='width:54pt'>字段3</td> 
  <td class=xl24 width=63 style='width:47pt'>字段4</td> 
  <td class=xl24 width=118 style='width:89pt'>字段5</td> 
  <td width=166 style='width:125pt'></td> 
</tr> 
……数据……  
<![if supportMisalignedColumns]> 
<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> 
<![endif]> 
</table> 
</body> 
</html> 

<html xmlns="urn:schemas-microsoft-comfficeffice"
xmlns:x="urn:schemas-microsoft-comffice: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'>字段1</td>
  <td class=xl24 width=72 style='width:54pt'>字段2</td>
  <td class=xl24 width=72 style='width:54pt'>字段3</td>
  <td class=xl24 width=63 style='width:47pt'>字段4</td>
  <td class=xl24 width=118 style='width:89pt'>字段5</td>
  <td width=166 style='width:125pt'></td>
</tr>
……数据……
<![if supportMisalignedColumns]>
<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>
<![endif]>
</table>
</body>
</html>

    至此,可通过数据生成如上格式的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);
分享到:
评论

相关推荐

    java解决大批量数据导出Excel产生内存溢出的方案

    java解决大批量数据导出Excel产生内存溢出的方案

    java多线程导出excel(千万级别)优化

    轻松解决普通poi形式导出Excel的中出现的栈溢出问题,此资源可实现千万级数据分批导出csv文件,csv大数据量导出(千万级别,不会内存溢出),多线程导出 ,生产环境已经很稳定的使用着

    java web开发中大量数据导出Excel超时(504)问题解决

    开发测试时候导入数据遇到大数据导入的问题,整理了下,需要的朋友可以参考下

    poi多sheet页导出工具类和实例 包含多个excel导出

    改资源包含 数据量超过20万以上导出解决方案,还有动态获取数据公共类 1) 把数据分成多个sheet导出。 2) 把数据分成多个excel 导出。 全部都有,还有实例。

    poi大量数据读取gc内存溢出解决方案

    poi读取大量数据会造成gc内存溢出的报错,由于垃圾回收机制无法将大量的对象及时的回收,而这些对象又会保存在内存中,会导致内存不够用的情况,这时候我们就需要使用新的方法,读取...此解决方案可支持千万数据的传输不报错

    gridexcel:基于功能编程和POI EventModel的用于简单读写Excel的通用解决方案

    基于POI EventModel,在读写数据量非常大的Excel时,降低内存占用避免OOM与频繁FullGC 基于函数编程,支持关联对象等多种复杂情况的处理,学习成本低 支持流式API,使代码编写和理解更简单,更直观 支持使用滚动窗口...

    word源码java-x4j-analytic:用于JAVA的Excel报表库

    应用程序中的嵌入式库,以实现成熟的报告解决方案。 执照 该项目是根据以下条款获得许可的 表现 X4J 引擎旨在生成相对较大的报告,并在报告服务器、WEB 应用程序、批处理报告上消耗合理的内存量。 此实现使用顺序...

    智能调度平台系统技术要求.pdf

    5、数据备份的要求 5.1应能永久保存数据,并提供永久备份数据的解决方案。 5.2应能定期备份数据,采用本地保存或异地存储的方式。 5.3当数据容量过大时,提示并辅助实施相关数据的导出备份。 5.4系统采用每日增量...

    weixin家庭记账本的设计与实现+ssm(源码+SSM+小程序+vue).zip

    数据导出:支持将账目数据导出为Excel文件。 权限管理:确保只有授权的用户可以访问和修改敏感信息。 微信登录:利用微信小程序实现快速登录和身份验证。 数据备份与恢复:防止数据丢失,支持手动备份和恢复数据。 ...

    asp.net知识库

    一完美的关于请求的目录不存在而需要url重写的解决方案! 在C#中实现MSN消息框的功能 XmlHttp实现无刷新三联动ListBox 鼠标放在一个连接上,会显示图片(类似tooltip) 使用microsoft.web.ui.webcontrols的TabStrip与...

    史上最好传智播客就业班.net培训教程60G 不下会后悔

    主要功能点:高安全性的用户管理体系、高安全性的异构系统数据导入导出、拼音检索、复合检索、无限级次数据管理、个性化邮件群发、Excel文件导入导出。 技术点 ADO.Net技术应用、SQLServer、MD5安全算法、基于NPOI的...

    timelord:一个简单的客户端应用程序,用于捕获本地系统上的时间跟踪信息

    在一周结束时,将您的数据导出到 Excel 中并将其上传或复制粘贴到您的公司时间跟踪解决方案中。 建造 该项目是使用 Maven 构建的: mvn package OSX Bundle 是使用 Oracle Bundler Ant Task 构建的: ant bundl

    基于jbpm与activiti的工作流平台技术架构介绍

    目前支持FineReport及Jasper Report两种报表引擎,前者是商业报表,其功能非常强大,可以实现多样式数据呈现方式,支持HTML、PDF、EXCEL、Word、TXT、Flash样式呈,能完美解决中国式报表难题,无论数据库内原始数据...

    易语言程序免安装版下载

    易语言5.1 相对于易语言5.0更新说明: ... 修改XML解析支持库,增加写出CDATA数据功能,解决解析XML时错误的丢弃换行和TAB字符的BUG,解决读取节点值时对CDATA数据进行转义处理的BUG。 20. 修改扩展界面支持库...

Global site tag (gtag.js) - Google Analytics