2017-02-24 25 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

謝謝!最後,我沒有像你那樣做,但是你給了我一些很好的指導方針! ;) –