Skip to content

Instantly share code, notes, and snippets.

@William-ST
Last active August 29, 2015 14:24
Show Gist options
  • Save William-ST/d9ab8a8f312126443469 to your computer and use it in GitHub Desktop.
Save William-ST/d9ab8a8f312126443469 to your computer and use it in GitHub Desktop.
//in third_party librrary PhpExcel
//in library Excel.php:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PHPExcel.php";
class Excel extends PHPExcel {
public function __construct() {
parent::__construct();
}
}
//in controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('Excel');
}
public function index(){
$this->load->view('ReportExcel');
}
public function GenerateExcel(){
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('Prueba reporte excel');
$count = 1;
$columns = array('A','B');
$datos = array(
array('firstName' => "william1", 'lastName' => "Sulca Talavera William 1"),
array('firstName' => "william2", 'lastName' => "Sulca Talavera William 2"),
array('firstName' => "william3", 'lastName' => "Sulca Talavera William 3"),
array('firstName' => "william4", 'lastName' => "Sulca Talavera William 4"),
array('firstName' => "william5", 'lastName' => "Sulca Talavera William 5")
);
foreach ($datos as $key => $value) {
$this->excel->getActiveSheet()->setCellValue($columns[0].$count, $value['firstName']);
$this->excel->getActiveSheet()->setCellValue($columns[1].$count, $value['lastName']);
$this->excel->getActiveSheet()->getStyle($columns[0].$count)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle($columns[1].$count)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$count++;
}
$sheet = $this->excel->getActiveSheet();
$sheet->getColumnDimension('A')->setAutoSize(true);
$sheet->getColumnDimension('B')->setAutoSize(true);
$this->excel->getActiveSheet()->getStyle('A')->getFont()->setSize(11);
$this->excel->getActiveSheet()->getStyle('B')->getFont()->setSize(11);
//$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
//$this->excel->getActiveSheet()->mergeCells('A1:D1');
$filename='reporte01.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment