为什么PHPexcel不提供结果而是显示警告“已发送标头”?

问题描述 投票:0回答:1

我做了大量研究,并且使用PHPExcel输出无望。当我使用PHPexcel列出结果时,我得到了codeigniter中的输出,但并不总是如此。我该如何纠正这个问题?我对Excel表格编码不是很精通。这是我的查询:

 public function excel_customer_all($date, $fdate)
{
  if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && $_SESSION['role'] && $_SESSION['role_des'])
    {
    $this->load->library('excel');
    $styleArray = array(
    'font'  => array(
    'bold'  => true,
    'color' => array('rgb' => '#000000'),
    'size'  => 10,
    'name'  => 'Verdana'
    ));
    $this->excel->setActiveSheetIndex(0);
    $this->excel->getActiveSheet()->setTitle('All Customer Details');
    .....
    SETTING CELL VALUES HERE
    .....



            //set cell A1 content with some text

    ................just ignoring these parts to shorten the code.........
            //retrive contries table data

     $rs=$this->Customer_model->list_all_customer_date($date, $fdate);

    $row = 4; // 1-based index
    $col = 0;

    $no = 1;    
    foreach($rs as $key=>$value) {

                   echo $row . ", ". $col . "<br>";
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row,$no);
                    $col++;
                     $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_main_ph);
                    $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_name);
                    $col++;$col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_add);

                    $col++; $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_email);

                    $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_main_location);
                    $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->category);
                    $col++;
                   if($value->used_devices&&$value->used_device_count){
        $z=array();
         $a=explode(',',$value->used_devices);
                     $b=explode(',',$value->used_device_count);
        foreach($a as $index=>$val){
           if($b[$index]=!0)
           {
           array_push($z,$a[$index].'-'.$b[$index]);
           }
        }
        $y='';
         $y=implode("  , ",$z);
        }
        if($value->other_device&&$value->other_device_count){
        $f=array();
         $c=explode(',',$value->other_device);
                     $d=explode(',',$value->other_device_count);
        foreach($c as $index=>$val){
           if($d[$index]=!0&&$c[$index])
           {
           array_push($f,$c[$index].'-'.$d[$index]);
           }
        }
         $g=implode("  , ",$f); 
        } 
        if($y || $g)
        {
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $y.' , '.$g);
        }
                    $col++;

                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->follow_up_dt);
                    $col++;

                 $row++;$col++;
                    echo $row . ", ". $col . "<br>";
                    $col = 0;
 $no++;
}
            $this->excel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $this->excel->getActiveSheet()->getStyle('B4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $this->excel->getActiveSheet()->getStyle('C4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

          $filename=mt_rand(1,100000).'.xls'; //just some random filename
          ob_clean();
    ob_start(); 
    header('Content-type: application/vnd.ms-excel; charset=UTF-8' ); 
    //header('Content-Type: application/xlsx');
    header('Content-Disposition: attachment;filename="'.$filename.'"');
    header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($this->excel,'Excel5');
        $objWriter->save('php://output');
        }  else
    {
        redirect('Admin/index', 'refresh');
      }

}

当我用多个选定的数据测试代码时,我发现问题出现在上述函数中的爆炸部分附近。有时,错误显示“未定义的变量$ y”,有时它会显示“已发送的标题”警告消息,其中包含一些数字,例如:请参见下图:

enter image description here

无论数据库中存在的数据类型如何,都应该做什么才能获得输出。

php codeigniter phpexcel
1个回答
2
投票

为了防止错误“未定义的变量$ y”在foreach循环的每次迭代开始时初始化$ y。

foreach($rs as $key=>$value) {
    $y = '';
    ....

并检查它是否为空

if(empty($y) || $g)
© www.soinside.com 2019 - 2024. All rights reserved.