1. First download the Php Excel from the website https://phpexcel.codeplex.com/.
2. Then extract the copy and put in the application/third_party folder of codeignitor.
3. Then go to the folder application/libraries and create a file and name it Excel.php. And place the below code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PHPExcel.php"; //Change path if required.
class Excel extends PHPExcel {
public function __construct() {
parent::__construct();
}
}
4.Now create a Controller like Export.php and in its action put the code:
$this->load->library('Excel');
$query = $this->db->get('users');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
$objPHPExcel->setActiveSheetIndex(0);
$col = 0;
foreach ($header as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$objPHPExcel->getActiveSheet()->getStyle('A1:Z1')->getFont()->setBold(true);
$col++;
}
// Fetching the table data
$row = 2;
foreach($query as $data)
{
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data[$field]); //change if required.
$col++;
}
$row++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// Sending headers to force the user to download the file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.'export_'.$table_name.'.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
Comments
Post a Comment