Cara Baca File Excel (Besar) dengan Spout di Codeigniter

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
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....
Buat Controller misal namanya Export.php






<?php
/**
* Excel dengan CI & Spout
*
*/
//load Spout Library
require_once APPPATH.'/third_party/spout/src/Spout/Autoloader/autoload.php';

//lets Use the Spout Namespaces
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;

class Export extends CI_Controller {

      public function readExcelFile() {

          try {
           
               //Lokasi file excel       
               $file_path = "C:\file_excel.xlsx";                     
               $reader = ReaderFactory::create(Type::XLSX); //set Type file xlsx
        $reader->open($file_path); //open the file          

                echo "<pre>";           
                $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 "<br> Total Rows : ".$i." <br>";              
                $reader->close();
                            

         echo "Peak memory:", (memory_get_peak_usage(true) / 1024 / 1024), " MB" ,"<br>";

      } 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.

Previous
Next Post »