PHPExcel is a PHP Library that allows us to make a report with easy. By using PHPExcel, we can set the coloumn width, row hight, text position, making freeze panes, set the style with ease. In this post i will  combine PHPExcel and Codeigniter.

Here step by step to make a report using PHPExcel and Codeigniter:

1. Create database, here i used mysql. Example: 

CREATE TABLE IF NOT EXISTS `student` (
  `idstudent` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) DEFAULT NULL,
  `address` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`idstudent`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
INSERT INTO `theookii1`.`student` (`idstudent` ,`name` ,`address`)VALUES ('1', 'johns', 'merpati street'
), ('2', 'joko', 'pramuka street');

2. Create Model file, example student_model.php

if (!defined ('BASEPATH')) exit ('No direct access allowed');
class Student_model extends CI_Model {
function __construct(){
parent::__construct();
}
function download_to_excel()
{ $this->db->get('student');
}
}

3. Create Controller file, example student.php

if (!defined ('BASEPATH')) exit ('No direct access allowed');
class Student extends CI_Controller
{
function __construct()
{
parent::__construct();
//$this->load->library(array('table','form_validation'));
//$this->load->helper(array('form','url')); $this->load->model("Student_model"); }
function download_to_excel()
{
$this->load->library("PHPExcel");
$phpExcel = new PHPExcel();
$prestasi = $phpExcel->setActiveSheetIndex(0);
//merger
$phpExcel->getActiveSheet()->mergeCells('A1:C1');
//manage row hight
$phpExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(25);
//style alignment
$styleArray = array(
'alignment' => array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
),
);
$phpExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $phpExcel->getActiveSheet()->getStyle('A1:C1')->applyFromArray($styleArray);
//border
$styleArray1 = array(
 'borders' => array(
'allborders' => array(
 'style' => PHPExcel_Style_Border::BORDER_THIN
)
 )
);
//background
$styleArray12 = array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => 'FFEC8B',
),
),
);
//freeepane
$phpExcel->getActiveSheet()->freezePane('A3');
//coloum width
$phpExcel->getActiveSheet()->getColumnDimension('A')->setWidth(4.1);
$phpExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$phpExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$prestasi->setCellValue('A1', 'Studens Report');
$phpExcel->getActiveSheet()->getStyle('A2:C2')->applyFromArray($styleArray);
$phpExcel->getActiveSheet()->getStyle('A2:C2')->applyFromArray($styleArray1);
$phpExcel->getActiveSheet()->getStyle('A2:C2')->applyFromArray($styleArray12);
$prestasi->setCellValue('A2', 'No'); $prestasi->setCellValue('B2', 'Name');
$prestasi->setCellValue('C2', 'Address');
$data = $this->Student_model->download_to_excel();
$no=0;
$rowexcel = 2;
foreach($data->result() as $row)
{
$no++;
$rowexcel++;
$phpExcel->getActiveSheet()->getStyle('A'.$rowexcel':C'.$rowexcel)->applyFromArray($styleArray);
$phpExcel->getActiveSheet()->getStyle('A'.$rowexcel':C'.$rowexcel)->applyFromArray($styleArray1);
$prestasi->setCellValue('A'.$rowexcel, $no); $prestasi->setCellValue('B'.$rowexcel, $row->name);
$prestasi->setCellValue('C'.$rowexcel, $row->address);
}
$prestasi->setTitle('Students Report');
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"Students Report.xls\"");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, "Excel5");
$objWriter->save("php://output"); }
}
Thanks for reading how to to export data to excel file using PHPExcel and codeigniter.

Title: How to export data to excel file using PHPExcel and Codeigniter
Posted by faisal

Thanks for reading about How to export data to excel file using PHPExcel and Codeigniter