jeudi 3 mars 2016

Laravel 5 - Download Excel file of a View

I'm working on adding an export/downloading feature to a Laravel 5 application. I found the Laravel-Excel library (http://ift.tt/1n2QXpH), 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://ift.tt/1QW3jp7" 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!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1QOSeQu
via IFTTT

Aucun commentaire:

Enregistrer un commentaire