2017-02-24 70 views
0

我有蛋糕php索引功能,它显示一些订单图。在此索引视图中,用户可以从中选择日期,并且一旦表单被提交,所选日期的订单图形将被更新。现在我试图实现另一个功能,通过向这两个日期选择添加简单的选择选项,将数据导出为ex​​cel。设置标题和下载文件后代码不会继续

问题是,当你不想导出Excel,你必须设置标题,一旦你设置标题,代码不会像我想要的那样连续。

因此,这里是我的索引功能

public function index() { 
     $orderData = $this->Order->getDashboardOrdersStatisticBetweenData(); 

     if ($this->request->is('post') || $this->request->is('put')) { 
      $dateFrom = $this->request->data['orderSumDates']['date_from']; 
      $dateTo = $this->request->data['orderSumDates']['date_to']; 
      $orderData = $this->Order->getDashboardOrdersStatisticBetweenData($dateFrom, $dateTo); 
      if ($this->request->data['orderSumDates']['export_type'] == 'export_excel') { 
       $this->generateExcelFile($orderData, $dateFrom, $dateTo); 
       die('Code never gets here, but file is downloaded'); 
      } 
     } 

     $this->set('orderStatistic', $orderData); 
    } 

这是我生成EXCEL文件功能

protected function generateExcelFile($orderData, $dateFrom, $dateTo) { 
     header('Content-type: application/vnd.ms-excel'); 
     header('Content-Disposition: attachment; filename="OrderReport'.$dateFrom.'-'.$dateTo.'.xlsx"'); 

     $objPHPExcel = new PHPExcel(); 
     $objPHPExcel->setActiveSheetIndex(0); 
     // Summary of report 
     $objPHPExcel->getActiveSheet()->SetCellValue('A5', 'Total number of orders'); 
     // Some other stuff 
     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
     $objWriter->save('php://output'); 

     header_remove('Content-type'); 
     header_remove('Content-Disposition'); 
    } 

所以,问题是,如果我选择export_excel选项,$this->generateExcelFile功能被执行和Excel文件下载,但其余的代码从来没有发生,例如这个die('Code never gets here, but file is downloaded');,我不会被执行。我已经做了一些测试,如果我注释掉$this->generateExcelFile函数的header()部分,代码通常会连续执行(die会被执行),但是excel文件没有正确生成,所以这些头文件至关重要。你能帮我解决我的问题吗?

+0

是否启用了错误?你检查错误日志吗? –

+0

没有错误...代码正在工作,问题在于写错了方式。一旦在generateExcelFile函数中设置了头文件,将不会在索引函数中继续使用 –

+0

在返回头文件之前尝试执行代码 – TedRed

回答

1

您可以保存该Excel文件,然后使用CakePHP内置函数将其发送到浏览器。

1 - 你的TMP文件夹中创建一个文件夹files

app/tmp/files 

2-节省您的功能,该文件夹中生成的文件,并返回该文件的位置

protected function generateExcelFile($orderData, $dateFrom, $dateTo) { 
    //header('Content-type: application/vnd.ms-excel'); // REMOVE THIS LINE 
    //header('Content-Disposition: attachment; filename="OrderReport' . $dateFrom . '-' . $dateTo . '.xlsx"'); // REMOVE THIS LINE 

    $objPHPExcel = new PHPExcel(); 
    $objPHPExcel->setActiveSheetIndex(0); 
    // Summary of report 
    $objPHPExcel->getActiveSheet()->SetCellValue('A5', 'Total number of orders'); 
    // Some other stuff 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

    $tmpFile = TMP . "files" . DS . sprintf("excel-file-%s.xlsx", date('Y-m-d-H-i-s')); // The tmp file 
    $objWriter->save($tmpFile); // Save on excel file 
    return $tmpFile; //send the file location 

    //$objWriter->save('php://output'); // REMOVE THIS LINE 

    //header_remove('Content-type'); // REMOVE THIS LINE 
    //header_remove('Content-Disposition'); // REMOVE THIS LINE 
} 

3-在您的删除该文件后将该文件内容发送到浏览器

public function index() { 
    $orderData = $this->Order->getDashboardOrdersStatisticBetweenData(); 

    if ($this->request->is('post') || $this->request->is('put')) { 
     $dateFrom = $this->request->data['orderSumDates']['date_from']; 
     $dateTo = $this->request->data['orderSumDates']['date_to']; 
     $orderData = $this->Order->getDashboardOrdersStatisticBetweenData($dateFrom, $dateTo); 
     if ($this->request->data['orderSumDates']['export_type'] == 'export_excel') { 
      $excelFile = $this->generateExcelFile($orderData, $dateFrom, $dateTo); 
      //Get the file content 
      $content = file_get_contents($excelFile); 
      //Delete that file 
      unlink($excelFile); 
      //Put the content on the response 
      $this->response->body($content); 
      //Force download (test.xlsx is the file name browser will recieve) 
      $this->response->download("test.xlsx"); 
      //spécify the response type 
      $this->response->type("application/vnd.ms-excel"); 
      //send the response 
      return $this->response; 
     } 
    } 

    $this->set('orderStatistic', $orderData); 
} 
+0

谢谢!最后,我没有像你那样做,但是你给了我一些很好的指导方针! ;) –