2017-03-06 661 views
0

我正在使用PHPExcel和CodeIgniter来生成我正在制作的项目的报告。我还是PHPExcel的新手。我做的第一件事就是将PHPExcel文件夹和PHPExcel.php文件放入应用程序的libraries文件夹中。PHPExcel:调用成员函数setActiveSheetIndex()null

现在,当我尝试运行该程序时,它告诉我一个错误,即“调用null上的成员函数setActiveSheetIndex()”。

所以我试图检查库是否真的加载在应用程序上。所以我试过这个:'

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

它提醒“是”。这意味着库被加载。但我不知道为什么函数返回null。

这里是我的控制器代码:

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(); 
} 

如果有人知道这事。请让我知道。先谢谢你。

回答

1

如果你想使用PHPExcel作为笨$this->excel类属性,你有别名它加载:

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

否则,该库将作为该库的名称:$this->PHPExcel

+0

所以如果我不使用你说的话。我应该这样做,像$ this-> PHPExcel-> setActiveSheetIndex(0)?现在错误消失了。但现在它说现场不能到达。 – Azis

+0

传递给PHPExcel_IOFactory :: createWriter()的参数1必须是PHPExcel的实例,null给出 – Azis

+0

感谢您帮助bro。这解决了我的问题。祝你有美好的一天 – Azis