2

I'm working on adding an export/downloading feature to a Laravel 5 application. I found the Laravel-Excel library (http://www.maatwebsite.nl/laravel-excel/docs), which seems perfect -- ideally, I will be able to take a Laravel View or html and make a downloadable Excel file.

So far, I've gotten the creation and storage to work. I can create an Excel file, and it's okay (it's the right data and View), and on the server in storage/exports/ I see "Report.xls" - which is right save path. The issue I'm having is I cannot seem to download the file through the browser after creating it.

The Laravel-Excel library has ->download('xls') and ->export('xls') methods, but these only seems to return the raw content, which is visible in the developer's console:

enter image description here

On the right, you can see the file was created and stored as "Report.xls", and presumably the raw content is the right content - but I don't know why it's just spitting raw content into the console. I can open the Report.xls file in Excel, so I know it's not corrupted.

I also tried to use the Laravel 5 Response::download() function and set headers for the download, but it also spit out raw content in the console instead of downloading the file:

enter image description here

The code I'm using is an AJAX call that hits a Controller method, and the controller method just triggers a Service method called "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'" ]); } 

The View being made into the Excel file is a simple table - the same table as in the screenshots. Here's the template/HTML of the View:

<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> 

Does anyone see what I might be missing to download an XLS file? Thanks for your time!

EDIT After doing some more research, it seems that my original workflow for downloading -- using an AJAX call to trigger -- is part of the problem. For example Download a file from Servlet using Ajax talks about AJAX storing results in JavaScript memory, which would explain why I get content in my console and not a file.

1 Answer 1

3

Try to add code below before Excel::create():

ob_end_clean(); ob_start(); 

Example code:

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

This works for me.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.