• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

phpExcel导出大量数据出现内存溢出错误的解决方法_php技巧

php 搞代码 3年前 (2022-01-26) 27次浏览 已收录 0个评论

phpExcel将读取的单元格信息保存在内存中,我们可以通过

 <BR>PHPExcel_Settings::setCacheStorageMethod()<BR>

来设置不同的缓存方式,已达到降低内存消耗的目的!

1、将单元格数据序列化后保存在内存中

 <BR>PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized; <BR>

2、将单元格序列化后再进行Gzip压缩,然后保存在内存中

 <BR>PHPExcel_CachedObjec<strong style="color:transparent">本&文来源gao@daima#com搞(%代@#码网@</strong><textarea>搞gaodaima代码</textarea>tStorageFactory::cache_in_memory_gzip; <BR>

3、缓存在临时的磁盘文件中,速度可能会慢一些

 <BR>PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;<BR>

4、保存在php://temp

 <BR>PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp; <BR>

5、保存在memcache中

PHPExcel_CachedObjectStorageFactory::cache_to_memcache<BR>

举例:

第4中方式:

 <BR>$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;  <BR>$cacheSettings = array( ' memoryCacheSize '  => '8MB'  <BR>                );  <BR>PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); <BR>

第5种:

 <BR>$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;  <BR>$cacheSettings = array( 'memcacheServer'  => 'localhost',  <BR>                        'memcachePort'    => 11211,  <BR>                        'cacheTime'       => 600  <BR>                      );  <BR>PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);<BR>

其它的方法

第一个方法,你可以考虑生成多个sheet的方式,不需要生成多个excel文件,根据你数据总量计算每个sheet导出多少行, 下面是PHPExcel生成多个sheet方法:

面是PHPExcel生成多个sheet方法:

 <BR>$sheet = $objPHPExcel->getActiveSheet(); <BR>$sheet->setCellValue('A1',$x);  <BR>$sheet->setCellValue('B1',$y);<BR>

第二个方法,你可以考虑ajax来分批导出,不用每次刷新页面。

 <BR>export to Excel <BR>$('#export').click(function() {  <BR>    $.ajax({  <BR>        url: "export.php",   <BR>        data: getData(),  //这个地方你也可以在php里获取,一般读数据库  <BR>        success: function(response){  <BR>            window.location.href = response.url;  <BR>        }  <BR>    })  <BR>});<BR>

 <BR><?php <BR>//export.php <BR>$data = $_POST['data'];<BR>$xls = new PHPExcel(); <BR>$xls->loadData($formattedData);<BR>$xls->exportToFile('excel.xls');<BR>$response = array( <BR>'success' => true, <BR>'url' => $url <BR>); <BR>header('Content-type: application/json'); <BR>echo json_encode($response); <BR>?><BR>

数据量很大的话,建议采用第二种方法,ajax来导出数据,上面方法简单给了个流程,具体你自己补充!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:phpExcel导出大量数据出现内存溢出错误的解决方法_php技巧

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址