phpExcel导出大量数据出现内存溢出错误的解决方法
php  /  管理员 发布于 7年前   148
phpExcel将读取的单元格信息保存在内存中,我们可以通过 来设置不同的缓存方式,已达到降低内存消耗的目的! 1、将单元格数据序列化后保存在内存中 2、将单元格序列化后再进行Gzip压缩,然后保存在内存中 3、缓存在临时的磁盘文件中,速度可能会慢一些 4、保存在php://temp 5、保存在memcache中 举例: 第4中方式: 第5种: 其它的方法 第一个方法,你可以考虑生成多个sheet的方式,不需要生成多个excel文件,根据你数据总量计算每个sheet导出多少行, 下面是PHPExcel生成多个sheet方法: 面是PHPExcel生成多个sheet方法: 第二个方法,你可以考虑ajax来分批导出,不用每次刷新页面。 数据量很大的话,建议采用第二种方法,ajax来导出数据,上面方法简单给了个流程,具体你自己补充! 122 在 123 在 原梓番博客 在 博主 在 1111 在
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
PHPExcel_Settings::setCacheStorageMethod()
PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
PHPExcel_CachedObjectStorageFactory::cache_to_memcache
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '8MB'
);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;
$cacheSettings = array( 'memcacheServer' => 'localhost',
'memcachePort' => 11211,
'cacheTime' => 600
);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1',$x);
$sheet->setCellValue('B1',$y);
export to Excel
$('#export').click(function() {
$.ajax({
url: "export.php",
data: getData(), //这个地方你也可以在php里获取,一般读数据库
success: function(response){
window.location.href = response.url;
}
})
});
复制代码 代码如下:
//export.php
$data = $_POST['data'];
$xls = new PHPExcel();
$xls->loadData($formattedData);
$xls->exportToFile('excel.xls');
$response = array(
'success' => true,
'url' => $url
);
header('Content-type: application/json');
echo json_encode($response);
?>您可能感兴趣的文章:
上一条:
PHP对MongoDB[NoSQL]数据库的操作
下一条:
Apache中php.ini的设置方法