Untuk CI versi 1.xx seperti berikut ini contoh codingnya :
Untuk Versi 2.xx sebagai berikut codingnya :
NB:
Donwload PhpExcel : http://phpexcel.codeplex.com/
Source : http://www.linkedin.com/groups/H4nk-Groups-4550315?trk=myg_ugrp_ovr
class Table_export extends Controller {function __construct(){parent::Controller();
}function index($table_name){$query = $this->db->get($table_name);if(!$query)return false;// Starting the PHPExcel library$this->load->library('PHPExcel');$this->load->library('PHPExcel/IOFactory');$objPHPExcel = new PHPExcel();$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");$objPHPExcel->setActiveSheetIndex(0);// Field names in the first row$fields = $query->list_fields();$col = 0;foreach ($fields as $field){$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);$col++;}// Fetching the table data$row = 2;foreach($query->result() as $data){$col = 0;foreach ($fields as $field){$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);$col++;}$row++;}$objPHPExcel->setActiveSheetIndex(0);$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');// Sending headers to force the user to download the fileheader('Content-Type: application/vnd.ms-excel');header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');header('Cache-Control: max-age=0');$objWriter->save('php://mysqltoexcel');}}
Untuk Versi 2.xx sebagai berikut codingnya :
class Table_export extends CI_Controller {
function __construct() {parent::__construct();
}function index($table_name){$query = $this->db->get($table_name);if(!$query)return false;// Starting the PHPExcel library$this->load->library('PHPExcel');$this->load->library('PHPExcel/IOFactory');$objPHPExcel = new PHPExcel();$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");$objPHPExcel->setActiveSheetIndex(0);// Field names in the first row$fields = $query->list_fields();$col = 0;foreach ($fields as $field){$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);$col++;}// Fetching the table data$row = 2;foreach($query->result() as $data){$col = 0;foreach ($fields as $field){$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);$col++;}$row++;}$objPHPExcel->setActiveSheetIndex(0);$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');// Sending headers to force the user to download the fileheader('Content-Type: application/vnd.ms-excel');header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');header('Cache-Control: max-age=0');$objWriter->save('php://mysqltoexcel');}}
NB:
Donwload PhpExcel : http://phpexcel.codeplex.com/
Source : http://www.linkedin.com/groups/H4nk-Groups-4550315?trk=myg_ugrp_ovr