Sudah diketahui untuk Baca dan tulis dari PHP ke Excel umumnya pakai PHPExcel Library. Namun kelemahan dari Library ini adalah ketika membaca file Excel dengan jumlah baris cukup besar dan akhirnya Out Of Memory (Jika kamu belum Update PHPExcel ke Versi baru)


Spout hadir untuk menangani File Excel dengan jumlah baris besar dengan konsumsi memory yang jauh lebih rendah dari PHPExcel. Dan SPout tentu bisa jadi alternatif bagus selain library sejenis diluarsana. 

Bagi kamu yang tidak pakai Codeigniter, cukup install saja Via Composer. Buat file json

composer.json dan copy code dibawah ini

"require": {    "box/spout": "~2.0"}


lalu run dengan

php composer.phar install

Cara Install Composer di Ubuntu

Bagi yang pakai Codeigniter, biar lebih mudah...ikuti langkah berikut : 

  • Download Source Code Spout
  • Lalu ke Copy ke folder application/third_party


   Disamping adalah structure Library Spout.

 

  Agar bisa digunakan di Controller....kita buat Contohnya langsung dah....


Sponsor : 
Iphone 6S Gratis dari Thenextweb




Buat Controller misal namanya Export.php

open($file_path); //open the file                  

                echo "
";               
                $i = 0; 
                                   
                /**                  
                * Sheets Iterator. Kali aja multiple sheets                  
                **/               
                foreach ($reader->getSheetIterator() as $sheet) {

                    //Rows iterator                    
                    foreach ($sheet->getRowIterator() as $row) {

                           print_r($row); 
                         
                           ++$i;
                     }
                }

                echo "
Total Rows : ".$i."
"; $reader->close(); echo "Peak memory:", (memory_get_peak_usage(true) / 1024 / 1024), " MB" ,"
"; } catch (Exception $e) { echo $e->getMessage(); exit; } }//end of function }//end of class


Jangan Lupa buat File Excel nya sendiri...isi Rows nya sendiri...sebanyak mungkin...misal 20rb Baris.

Jika sudah Jalankan di Broswer : 

http://localhost/my-project/export/readExcelFile

atau via PHP CLI

php index.php export readExcelFile

Output diatas....adalah

Array
(
    [0] => SPP-16755
    [1] => 42198
    [2] => Mester SERVER
    [3] => Rp.9000
    [4] => Banjarmasin
)
.....
.....
.....

Total Rows : 6171 
Peak memory usage: 2 MB

Dengan jumlah rows 6171 dan 5 Colom, Memory nya 2MB . Dengan PHPExcel (saya benchmark) Memory 12MB.


Baca Juga

Membuat Article Post Dengan Rich Text Editor Summernote Di PHP