转:http://blog.chinaunix.net/u3/103458/showart_2033111.html
看过很多关于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'>字段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: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'>字段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);
以上只是一种导出Excel大数据量的解决方案。请大家给予意见!谢谢!
分享到:
相关推荐
Java_批量导出大数据量Excel方法.zip
jxl 导出Excel 支持大数据量导出 导出数据量大 速度也快
根据Microsoft.Office.Interop.Excel,优化大数据量快速导出到 Excel 的方法
见到导出大量数据 自己定义Excel表格样式 用html当时定义见到导出大量数据 自己定义Excel表格样式 用html当时定义见到导出大量数据 自己定义Excel表格样式 用html当时定义见到导出大量数据 自己定义Excel表格样式 用...
java版本的使用POI导出大数据量到EXCEL
java poi导出大量数据到Excel
C#大量数据导出到Excel,超过65536行时自动分页导出
其次,在实验过程中,大数据量的导出很容易引发内存溢出,调整JVM的内存大小治标不治本。很多人建议保存为.CSV格式的文件。不过,.CSV方式导出也存在问题:首先,如果用excel来打开csv,超过65536行的数据都会看不见...
直接可以用的项目。数据量大,导出多个excel然后压缩成zip文件。。。
支持模板导出 ,支持分页查询 , 支持大数据量,采用SXSSFWorkbook
导出大量数据到excel,本人已亲测,对几百万数据已测试过,导出数据比较乐观,里面自带压缩文件功能。
解决大批量数据导出Excel产生内存溢出的方案
解决poi大数据量导出excel的代码,该代码中所用的jar包是poi3.9。在测试过程中单个sheet中导出20万数据没有问题。
我之前做excel导出的时候,效率很差,尤其是数据量达到上万条的时候,要等上将近20分钟了。后来努力下,找到了简单的导出方法,几千条数据,用了不到1分钟,就轻松导出了。下面是思路和源码,供大家参考
前端导出数据为excel(兼容chrome 和 IE10以上版本),列举了网上常用的两种方法外,新增了可以导出超过5W行数据的方法
POI百万级大数据量EXCEL导出 - 请叫我猿叔叔的博客 - CSDN博客.htm
Excel数据批量导入与Excel文件的导出,Excel数据批量导入与Excel文件的导出
POI技术实现Excel导出,支持自定义表头,2层标题,大数据量
轻松解决普通poi形式导出Excel的中出现的栈溢出问题,此资源可实现千万级数据分批导出csv文件,csv大数据量导出(千万级别,不会内存溢出),多线程导出 ,生产环境已经很稳定的使用着
Java_批量导出大数据量Excel方法