phpExcel導(dǎo)出大量數(shù)據(jù)出現(xiàn)內(nèi)存溢出錯誤的解決方法
phpExcel將讀取的單元格信息保存在內(nèi)存中,我們可以通過
PHPExcel_Settings::setCacheStorageMethod()
來設(shè)置不同的緩存方式,已達(dá)到降低內(nèi)存消耗的目的!
1、將單元格數(shù)據(jù)序列化后保存在內(nèi)存中
PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
2、將單元格序列化后再進(jìn)行Gzip壓縮,然后保存在內(nèi)存中
PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
3、緩存在臨時的磁盤文件中,速度可能會慢一些
PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
4、保存在php://temp
PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
5、保存在memcache中
PHPExcel_CachedObjectStorageFactory::cache_to_memcache
舉例:
第4中方式:
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '8MB'
);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
第5種:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;
$cacheSettings = array( 'memcacheServer' => 'localhost',
'memcachePort' => 11211,
'cacheTime' => 600
);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
其它的方法
第一個方法,你可以考慮生成多個sheet的方式,不需要生成多個excel文件,根據(jù)你數(shù)據(jù)總量計(jì)算每個sheet導(dǎo)出多少行, 下面是PHPExcel生成多個sheet方法:
面是PHPExcel生成多個sheet方法:
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1',$x);
$sheet->setCellValue('B1',$y);
第二個方法,你可以考慮ajax來分批導(dǎo)出,不用每次刷新頁面。
<a href="#" id="export">export to Excel</a>
$('#export').click(function() {
$.ajax({
url: "export.php",
data: getData(), //這個地方你也可以在php里獲取,一般讀數(shù)據(jù)庫
success: function(response){
window.location.href = response.url;
}
})
});
<?php
//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);
?>
數(shù)據(jù)量很大的話,建議采用第二種方法,ajax來導(dǎo)出數(shù)據(jù),上面方法簡單給了個流程,具體你自己補(bǔ)充!
相關(guān)文章
PHP獲取數(shù)組表示的路徑方法分析【數(shù)組轉(zhuǎn)字符串】
這篇文章主要介紹了PHP獲取數(shù)組表示的路徑,結(jié)合實(shí)例形式對比分析了數(shù)組轉(zhuǎn)字符串的實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
php中filter函數(shù)驗(yàn)證、過濾用戶輸入的數(shù)據(jù)
PHP 過濾器用于對來自非安全來源的數(shù)據(jù)(比如用戶輸入)進(jìn)行驗(yàn)證和過濾,下面為大家整理了一些,需要的朋友可以了解下2014-01-01
PHP 多進(jìn)程與信號中斷實(shí)現(xiàn)多任務(wù)常駐內(nèi)存管理實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于PHP 多進(jìn)程與信號中斷實(shí)現(xiàn)多任務(wù)常駐內(nèi)存管理的相關(guān)知識點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-10-10
php中將時間差轉(zhuǎn)換為字符串提示的實(shí)現(xiàn)代碼
通過傳入數(shù)據(jù)庫中存儲的文章發(fā)表時的UNIX時間戳,來轉(zhuǎn)化為例如 幾分鐘前,幾小時前,幾天前 這樣的提示。2011-08-08
php基于閉包實(shí)現(xiàn)函數(shù)的自調(diào)用(遞歸)實(shí)例分析
這篇文章主要介紹了php基于閉包實(shí)現(xiàn)函數(shù)的自調(diào)用,結(jié)合實(shí)例形式分析了php閉包實(shí)現(xiàn)遞歸的操作方法,需要的朋友可以參考下2016-11-11
docker?中搭建php環(huán)境經(jīng)驗(yàn)分享
這篇文章主要介紹了docker?中搭建php環(huán)境經(jīng)驗(yàn)分享的相關(guān)資料,需要的朋友可以參考下2023-09-09

