1. Pada tutorial ini, saya hanya akan menambahkan fungsi excelnya di controller Welcome.php (malas buat controller baru :p). Codenya seperti di bawah ini :
public function excel()
        {
            $this->load->library("PHPExcel");
 
            //membuat objek
            $objPHPExcel = new PHPExcel();
 
            //Sheet yang akan diolah
            $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A1', 'Hello')
                        ->setCellValue('B2', 'Ini')
                        ->setCellValue('C1', 'Excel')
                        ->setCellValue('D2', 'Pertamaku');
            //Set Title
            $objPHPExcel->getActiveSheet()->setTitle('Excel Pertama');
 
            //Save ke .xlsx, kalau ingin .xls, ubah 'Excel2007' menjadi 'Excel5'
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
 
            //Header
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
            header("Cache-Control: no-store, no-cache, must-revalidate");
            header("Cache-Control: post-check=0, pre-check=0", false);
            header("Pragma: no-cache");
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

            //Nama File
            header('Content-Disposition: attachment;filename="hasilExcel.xlsx"');

            //Download
            $objWriter->save("php://output");
 
        }

4. Selesai 😀 Untuk mengujinya, coba panggil dari browser. ketik ” http://localhost/ci3_phpexcel/index.php/welcome/excel”. File excel akan didownload otomatis dari browser anda.

Dan jika yang anda lakukan sudah benar, seharusnya beginilah hasilnya :

Jika ingin mendownload source code lengkapnya, silahkan download di :

ci3_PHPExcel.zip

Happy Coding 🙂

Leave a Reply

Your email address will not be published.