PHP導出excel相對很多童鞋都碰到了,使用phpexcel類也確實方便,但導出大數據的時候就沒那麼簡單了,常常會伴隨一些超時或內存溢出的問題,下面就給大家介紹一些方法,共同學習,共同進步。 。 。
PHPExcel是一個很強大的處理Excel的PHP開源類,但是很大的一個問題就是它佔用內存太大,從1.7.3開始,它支援設定cell的快取方式,但是推薦使用目前穩定的版本1.7 .6,因為先前的版本都會不同程度的存在bug,以下是其官方文件:
PHPExcel1.7.6官方文件寫道
PHPExcel uses an average of about 1k/cell in your worksheets, cel uses an average of about 1k/cell in your worksheets, can large works cancan quicks can can quickly up available memory. Cell caching provides a mechanism that allows PHPExcel to maintain the cell objects in a smaller size of memory, on disk, or in APC, memcache or Wincache, rather than in PHP memage for Thisred or Wincache, rather than in PHP mem. large workbooks, although at a cost of speed to access cell data.
PHPExcel 平均下來使用1k/單元格的內存,因此大的文檔會導致內存消耗的也很快。單元格快取機制能夠允許PHPExcel將記憶體中的小的單元格物件快取在磁碟或 APC,memcache或Wincache中,儘管會在讀取資料上消耗一些時間,但是能夠幫助你降低記憶體的消耗。
PHPExcel1.76.官方文件寫道
By default, PHPExcel still holds all cell objects in memory, but you can specify alternatives. To enable cell caching, you must caching 集caching method that you wish to use.
預設情況下,PHPExcel依然將單元格物件保存在記憶體中,但您可以自訂。你可以使用PHPExcel_Settings::setCacheStorageMethod()方法,將快取方式當作參數傳遞給這個方法來設定快取的方式。
Php程式碼:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;
PHPExcel_Settings::setCacheStorageMethod($cacheMethodMethodMethod () will return a boolean true on success, false on failure (for example if trying to cache to APC when APC is not enabled).
setCacheStorageMethod()方法會傳回一個BOOL型變數來表示是否成功設定(例如,如果APC無法使用的時候,你設定使用APC快取,將會回傳false)
PHPExcel1.7.6官方文件寫道
A separate cache is maintained for each individual worksheet, and is autoally created when the 5. You cannot change the configuration settings once you have started to read a workbook, or have created your first worksheet.
每一個worksheet都會有一個獨立的緩存,當一個worksheet實例化時,就會根據設定或設定的快取方式創建。一旦你開始讀取一個檔案或是你已經創建了第一個worksheet,就不能在改變快取的方式了。
PHPExcel1.7.6官方文件寫道
Currently, the following caching methods are available.
碼
目前,有幾種快取方式可以使用:
PFactoryCpache
PHPExcel1.7.6官方文件寫道
The default. If you don't initialise any caching method, then this is the method that PHPExcel will use. Cell objects are maintained in PHP memory as at pres任何快取方式,PHPExcel將使用記憶體快取的方式。
================================================
Php代碼:
PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
PHPExcle1.7.6官方文件寫道
U 對 this.alat, 擲骰, memis shsing 片面 c which reduces the memory footprint with minimal performance overhead.
使用這種快取方式,單元格會以序列化的方式保存在記憶體中,這是降低記憶體使用率效能比較高的一種方案。
================================================
Php程式碼:
PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel1.7.6官方文件寫道
Like cache_inholdmem san salatorys erial cache_inas ed objects, but- gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower.
與序列化的方式類似,這種方法在序列化之後,又進行gzip壓縮之後再放入內存中,這回跟進一步降低內存的使用,但是讀取和寫入時會有一些慢。
================================================ ===========
Php代碼:
PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
PHPExcel1.7.6官方文件寫道 tempcaches 起切片🜥區only an 。
當使用cache_to_discISAM這種方式時,所有的單元格將會保存在一個臨時的磁碟檔案中,只把他們的在文件中的位置保存在PHP的內存中,這會比任何一種緩存在內存中的方式都慢,但是能顯著的降低內存的使用。臨時磁碟檔案在腳本運行結束是會自動刪除。
================================================ ===========
Php代碼:
PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
PHPExcel1.7.6官方文件寫道 o_ache ache
:// temp I/O stream, with only an index to their location maintained in PHP memory. In PHP, the php://memory wrapper stores data in the memory: php://temp behaves similarly, but uses a temporary file for storing the data when a certain memory limit is reached. The default is 1 MB, but you can change this when initialising cache_to_phpTemp.
類似cache_to_discISAM這種方式,使用cache_to_Tempcache
類似cache_to_discISAM. O流中,只把他們的位置保存在PHP的記憶體中。 PHP的php://memory包裹器將資料保存在記憶體中,php://temp的行為類似,但是當儲存的資料大小超過記憶體限制時,會將資料保存在暫存檔案中,預設的大小是1MB ,但你可以在初始化時修改它:
Php程式碼:
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = arraym( ' memoryCopiize ' ::setCacheStorageMethod($ cacheMethod, $cacheSettings);
PHPExcel1.7.6官方文件寫道
The php://temp file is automatically deleted when your script terminates.
php://temp文件結束是會自動刪除腳本。
================================================ ===========
Php代碼:
PHPExcel_CachedObjectStorageFactory::cache_to_apc;
PHPExcle1.7.6官方文件寫道 chool Anectronl. maintained in PHP memory to identify that the cell exists. By default, an APC cache timeout of 600 seconds is used, which should be enough for most applications: althoughache it is possible to be enough保存在APC中,只在記憶體中保存索引。 APC快取預設逾時時間時600秒,對絕大多數應用是足夠了,當然你也可以在初始化時進行修改:
Php程式碼:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_tog; array( 'cacheTime' => 600 );
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
PHPExcel1.7.6官方文件寫道 記憶體regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
當腳本運行結束時,所有的資料都會從APC中清楚(忽略快取時間),不能使用此機製作為持久快取。
================================================ ===========
Php程式碼:
PHPExcel_CachedObjectStorageFactory::cache_to_memcache
PHPExcel1.7.6官方文件寫道Ip. mcache with only an index maintained in PHP memory to identify that the cell exists.
By default, PHPExcel looks for a memcache server on localhost at port 11211. It also sets a memcache timeout limit of 600 seconds. If you are running memcache on a different server or port, then you can change these defaults when you initialise cache_to_memcache:
使 用cache_to_memory時,單元格物件保存在memcache中,只在記憶體中保存索引。預設情況下,PHPExcel會在localhost和連接埠11211尋找memcache服務,逾時時間600秒,如果你在其他伺服器或其他連接埠上執行memcache服務,可以在初始化時進行修改:
Php程式碼:
$cacheMethodMethod ::cache_to_memcache;
$cacheSettings = array( 'memcacheServer' => 'localhost',
'memcachePort' => 11211,
'memcachePort' );
PHPExcel_Settings::setCacheStorageMethod($cacheMethod , $cacheSettings);
從初始化設定的形式來看,MS還不支援多台memcache伺服器輪詢的方式,比較遺憾。
PHPExcel1.7.6官方文檔寫道
When your script terminates all entries will be cleared from memcache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
當腳本結束時,所有的資料都會從memcache清空(忽略快取時間),不能使用此機制進行持久性儲存。
================================================ ===========
Php代碼:
PHPExcel_CachedObjectStorageFactory::cache_to_wincache;
PHPExcel1.7.6 官方文件寫道, 經典index maintained in PHP memory to identify that the cell exists. By default, a Wincache cache timeout of 600 seconds is used, which should be enough for most applications: although it is possible to ache be enough for most applications: although it is possible to ache 0會保存在Wincache中,只在內存中保存索引,預設情況下Wincache過期時間為600秒,對絕大多數應用是足夠了,當然也可以在初始化時修改:
Php代碼:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_wincache;
$cacheSettings = array( 'cacheTime' => 600 );
PHPExcel_Settings::setCacheSpaageMethod($cacheS.
When your script terminates all entries will be cleared from Wincache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
PHPExcel還是比較強大的,最大的問題就是內存能佔用一個啎PHPExcel還是比較強大的,最大的問題就是內存能佔用一個止步量級的版本,不需要那麼多花俏的功能,只需要匯出最普通的資料的版本就好了!
以上內容是LZ在網路上找的一篇比較不錯的文章,雖沒有徹底解決我的問題,但感覺寫的不錯,謹以此作為筆記,希望對看到的童鞋能夠有所幫助!