在不进行特殊设置的情况下,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

        <span style="color: #800080">$cacheMethod</span> = PHPExcel_CachedObjectStorageFactory::<span style="color: #000000">cache_to_memcache;  
        </span><span style="color: #800080">$cacheSettings</span> = <span style="color: #0000ff">array</span>( 'memcacheServer'  => 'localhost',  
                        'memcachePort'    => 11211,  
                        'cacheTime'       => 600<span style="color: #000000">  
                      );  
        PHPExcel_Settings</span>::setCacheStorageMethod(<span style="color: #800080">$cacheMethod</span>, <span style="color: #800080">$cacheSettings</span>);

?

注意是加在new PHPExcel() 前面:如下

<span style="color: #008080">1</span> <span style="color: #0000ff">require_once</span> APPPATH .'third_party/PHPExcel/PHPExcel.php'<span style="color: #000000">;
</span><span style="color: #008080">2</span>         
<span style="color: #008080">3</span>         <span style="color: #800080">$cacheMethod</span> = PHPExcel_CachedObjectStorageFactory::<span style="color: #000000">cache_to_phpTemp;
</span><span style="color: #008080">4</span>         <span style="color: #800080">$cacheSettings</span> = <span style="color: #0000ff">array</span>('memoryCacheSize'=>'16MB'<span style="color: #000000">);
</span><span style="color: #008080">5</span>         PHPExcel_Settings::setCacheStorageMethod(<span style="color: #800080">$cacheMethod</span>, <span style="color: #800080">$cacheSettings</span><span style="color: #000000">);
</span><span style="color: #008080">6</span>         
<span style="color: #008080">7</span>         <span style="color: #800080">$objPHPExcel</span> = <span style="color: #0000ff">new</span> PHPExcel();