Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. //in third_party librrary PhpExcel
  2. //in library Excel.php:
  3. <?php
  4. if (!defined('BASEPATH')) exit('No direct script access allowed');
  5.  
  6. require_once APPPATH."/third_party/PHPExcel.php";
  7.  
  8. class Excel extends PHPExcel {
  9. public function __construct() {
  10. parent::__construct();
  11. }
  12. }
  13. //in controller:
  14. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  15.  
  16. class Welcome extends CI_Controller {
  17.  
  18. public function __construct() {
  19. parent::__construct();
  20. $this->load->library('Excel');
  21. }
  22.  
  23. public function index(){
  24. $this->load->view('ReportExcel');
  25. }
  26.  
  27. public function GenerateExcel(){
  28. $this->excel->setActiveSheetIndex(0);
  29. $this->excel->getActiveSheet()->setTitle('Prueba reporte excel');
  30. $count = 1;
  31. $columns = array('A','B');
  32. $datos = array(
  33. array('firstName' => "william1", 'lastName' => "Sulca Talavera William 1"),
  34. array('firstName' => "william2", 'lastName' => "Sulca Talavera William 2"),
  35. array('firstName' => "william3", 'lastName' => "Sulca Talavera William 3"),
  36. array('firstName' => "william4", 'lastName' => "Sulca Talavera William 4"),
  37. array('firstName' => "william5", 'lastName' => "Sulca Talavera William 5")
  38. );
  39.  
  40. foreach ($datos as $key => $value) {
  41. $this->excel->getActiveSheet()->setCellValue($columns[0].$count, $value['firstName']);
  42. $this->excel->getActiveSheet()->setCellValue($columns[1].$count, $value['lastName']);
  43. $this->excel->getActiveSheet()->getStyle($columns[0].$count)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  44. $this->excel->getActiveSheet()->getStyle($columns[1].$count)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  45. $count++;
  46. }
  47.  
  48. $sheet = $this->excel->getActiveSheet();
  49. $sheet->getColumnDimension('A')->setAutoSize(true);
  50. $sheet->getColumnDimension('B')->setAutoSize(true);
  51. $this->excel->getActiveSheet()->getStyle('A')->getFont()->setSize(11);
  52. $this->excel->getActiveSheet()->getStyle('B')->getFont()->setSize(11);
  53. //$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
  54. //$this->excel->getActiveSheet()->mergeCells('A1:D1');
  55. $filename='reporte01.xls';
  56. header('Content-Type: application/vnd.ms-excel');
  57. header('Content-Disposition: attachment;filename="'.$filename.'"');
  58. header('Cache-Control: max-age=0');
  59. $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
  60. $objWriter->save('php://output');
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement