Search code examples
phpcodeigniterphpexcel

PHPExcel: Call to a member function setActiveSheetIndex() on null


I am using PHPExcel and CodeIgniter to generate reports on the project that I am making. I'm still new to PHPExcel. First thing I did is that I placed the PHPExcel folder and the PHPExcel.php file in the libraries folder of the application.

Now when I try to run the program, it tells me an error that "Call to a member function setActiveSheetIndex() on null".

So I tried to check if the library is really loaded on the application. So I tried this: `

if(class_exists('PHPExcel')){
        echo "yes";
    } else {
        echo "no";
    }

It alerts "yes". Which means the library is loaded. But I have no idea why the function returns null.

Here is the code in my controller:

public function excel_report1()
{
    $this->load->library('PHPExcel');

    $this->excel->setActiveSheetIndex(0);

    $this->excel->getActiveSheet()->setTitle('Users list');

    $startDate = $this->input->post('fromDate');
    $endDate = $this->input->post('toDate');
    $data = $this->KudosModel->getDateRange($startDate,$endDate);

    $styleArray = array(
    'font'  => array(
        'bold'  => true,
        'size'  => 15,
    ));

    $styleArray2 = array(
    'font'  => array(
        'size'  => 15,
    ));

    $this->excel->getActiveSheet()->mergeCells('A1:B1');
    $this->excel->getActiveSheet()->setCellValue('A1', 'TOTAL KUDOS COUNT ('.$startDate.' - '.$endDate.')');
    $this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $this->excel->getActiveSheet()->getStyle('A1')->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => 'FF0000'))));
    $this->excel->getActiveSheet()->getStyle('A1:B1')->applyFromArray($styleArray);

    $this->excel->getActiveSheet()->getStyle('A2:D2')->applyFromArray($styleArray);
    $this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
    $this->excel->getActiveSheet()->getColumnDimension('B')->setWidth(30);

    $this->excel->getActiveSheet()->setCellValue('A2', 'Month and Year');
    $this->excel->getActiveSheet()->setCellValue('B2', 'Kudos Count');



    $row = 3;
    foreach($data as $r){
        $this->excel->getActiveSheet()->fromArray(array($r->emp_id, $r->emp_stat, $r->isActive, $r->dateStarted), null, 'A'.$row);
        $this->excel->getActiveSheet()->getStyle('A'.$row.':B'.$row)->applyFromArray($styleArray2);
        $row++;
    }

    ob_end_clean();
    date_default_timezone_set("Asia/Manila");
    $timestamp=date("Y-m-d-His");
    $filename='KudosCount.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->PHPExcel, 'Excel2007');

    //$objWriter->save('php://output');
    $objWriter->save($filename);
    exit();
}

If anyone knows anything about this. Please do let me know. Thank you in advance.


Solution

  • If you want to use PHPExcel as $this->excel class property in CodeIgniter, you have to alias it on loading:

    $this->load->library('PHPExcel', NULL, 'excel');
    

    Otherwise, the library will be available as the name of the library: $this->PHPExcel.