转载!!!java操作Excel导出大数据量解决方案
看过很多关于Excel导出时出现内存溢出的情况,也有很多解决方案。现提供如下解决方案,如有不妥,请指正: 该项目使用B/S架构,由于POI、JXL在导出excel大数据量情况下会产生大量对象最终导致内存溢出。其实Excel可以另存为html文件,保存为html后的文件内容如下: 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);