2016-03-03 138 views
1

我正在爲Laravel 5應用程序添加導出/下載功能。我找到了Laravel-Excel庫(http://www.maatwebsite.nl/laravel-excel/docs),這看起來很完美 - 理想情況下,我將能夠採用Laravel View或HTML並製作可下載的Excel文件。Laravel 5 - 下載視圖的Excel文件

到目前爲止,我已經獲得了創建和存儲工作。我可以創建一個Excel文件,並且沒關係(這是正確的數據和視圖),並且在存儲/出口/服務器上,我看到「Report.xls」 - 這是正確的保存路徑。我遇到的問題是,我似乎無法在創建後通過瀏覽器下載文件。

的Laravel-Excel的圖書館 - >下載(「XLS」)和 - >導出(「XLS」)的方法,但這些似乎只返回原始內容,這是開發商的控制檯可見:

enter image description here

在右邊,你可以看到文件的創建和「的report.xls」存儲,想必原始內容是正確的內容 - 但我不知道爲什麼它只是吐痰原始內容進入安慰。我可以在Excel中打開Report.xls文件,所以我知道它沒有損壞。

我也嘗試使用Laravel 5響應::下載()函數,以及下載設置頭,但它也吐出控制檯原始內容,而不是下載的文件:

enter image description here

我正在使用的代碼是一個AJAX調用擊中控制器的方法,並在控制器方法只是觸發所謂的「downloadReportFile()」的服務方法:

public function downloadReportFile($requestData, $fileType) { 

    $configJSON = \Report::loadConfigFile($requestData['reportName']); 
    $today = Carbon::today()->format('Y-m-d'); 

    $FileViewdata = array(
     'config' => $configJSON, 
     'html' => $requestData['report_html'] 
    ); 


    \Excel::create("Report", function($excel) use ($FileViewdata) { 

     $excel->setTitle($FileViewdata['config']['title']); 
     $excel->setDescription($FileViewdata['config']['description']); 


     $excel->sheet("page 1", function($sheet) { 
      $sheet->loadView("reports.sample"); 
     }); 

    })->store('xls', storage_path('exports')); // ->export('xls'); 


    $report_excelFilepath = storage_path('exports') . "/Report.xls"; 


    return response()->download($report_excelFilepath, "Report.xls", [ 
     'Content-Type' => 'application/vnd.ms-excel', 
     'Content-Disposition' => "attachment; filename='Report.xls'" 
    ]); 

} 

此視圖製作成Excel文件是簡單的桌子 - 截圖中的同一張表。這裏是視圖的模板/ HTML:

<div class="container full-width-container"> 
    <link href="http://192.168.33.10/css/reports.css" rel="stylesheet"> 

<div id="results" class="row"> 
    <div class="col-xs-12"> 

     <h2 class="report-title">Active Products </h2> 
     <br> 

     <div class="row"> 
      <div class="col-xs-12"> 

       <div class="table-responsive report-component" name="table" template="table"> 
        <table id="report-table" class="table table-striped table-bordered table-hover"> 

         <thead> 
          <tr> 
           <td class="report-table-th">ENGINE</td> 
           <td class="report-table-th">PRODUCT COUNT</td> 
           <td class="report-table-th">PRODUCT PRICE</td> 
          </tr> 
         </thead> 
         <tbody> 
          <tr class="results-cell"> 

           <td class="report-table-cell"> 
             <p>Meows</p> 
           </td> 


           <td class="report-table-cell"> 
             <p>1,234</p> 
           </td> 


           <td class="report-table-cell"> 
             <p>781230.00</p> 
           </td> 

          </tr> 
         </tbody> 
         <tbody> 
          <tr class="results-cell"> 

           <td class="report-table-cell"> 
             <p>Paws</p> 
           </td> 


           <td class="report-table-cell"> 
             <p>10,777</p> 
           </td> 


           <td class="report-table-cell"> 
             <p>3919823.00</p> 
           </td> 

          </tr> 
         </tbody> 
         <tbody> 
          <tr class="results-cell"> 

           <td class="report-table-cell"> 
             <p>Meow Mobile</p> 
           </td> 

           <td class="report-table-cell"> 
             <p>177</p> 
           </td> 

           <td class="report-table-cell"> 
             <p>334323.00</p> 
           </td> 

          </tr> 
         </tbody> 
         <tbody> 
          <tr class="results-cell"> 

           <td class="report-table-cell"> 
             <p>Tails Cloud</p> 
           </td> 


           <td class="report-table-cell"> 
             <p>335</p> 
           </td> 


           <td class="report-table-cell"> 
             <p>378918923.00</p> 
           </td> 

          </tr> 
         </tbody> 
        </table> 
       </div> 




      </div> 
     </div> 

    </div> 
</div> 

有沒有人看到我可能缺少下載一個XLS文件? 感謝您的時間!

編輯 做一些進一步的研究之後,似乎我的原始工作下載 - 使用AJAX調用觸發 - 是問題的一部分。例如Download a file from Servlet using Ajax談論AJAX在JavaScript內存中存儲結果,這可以解釋爲什麼我在控制檯中獲取內容而不是文件。

回答

2

嘗試Excel::create()前添加如下代碼:

ob_end_clean(); 
ob_start(); 

示例代碼:

ob_end_clean(); 
ob_start(); 
Excel::create($filename, function($excel) use($records, $sheetname, $data) {} 

這對我的作品。