版本:1.7.6+
在不進行特殊設置的情況下,phpExcel將讀取的單元格信息保存在內存中,我們可以通過
PHPExcel_Settings::setCacheStorageMethod()
來設置不同的緩存方式,已達到降低內存消耗的目的!
1、將單元格數據序列化后保存在內存中
PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
2、將單元格序列化后再進行Gzip壓縮,然后保存在內存中
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
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache; $cacheSettings = array( 'memcacheServer' => 'localhost', 'memcachePort' => 11211, 'cacheTime' => 600 ); PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
注意是加在new PHPExcel() 前面:如下
1 require_once APPPATH .'third_party/PHPExcel/PHPExcel.php'; 2 3 $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp; 4 $cacheSettings = array('memoryCacheSize'=>'16MB'); 5 PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); 6 7 $objPHPExcel = new PHPExcel();