dongye7231 2017-03-06 15:09
浏览 381
已采纳

PHPExcel:在null上调用成员函数setActiveSheetIndex()

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.

  • 写回答

1条回答 默认 最新

  • dpwbc42604 2017-03-06 15:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?