【问题标题】:Laravel 5 - Download Excel file of a ViewLaravel 5 - 下载视图的 Excel 文件
【发布时间】:2016-06-17 06:47:24
【问题描述】:

我正在为 Laravel 5 应用程序添加导出/下载功能。我找到了 Laravel-Excel 库 (http://www.maatwebsite.nl/laravel-excel/docs),它看起来很完美——理想情况下,我可以使用 Laravel View 或 html 并制作可下载的 Excel 文件。

到目前为止,我已经完成了创建和存储工作。我可以创建一个 Excel 文件,没关系(它是正确的数据和视图),并且在服务器上的 storage/exports/ 我看到“Report.xls” - 这是正确的保存路径。我遇到的问题是创建文件后似乎无法通过浏览器下载文件。

Laravel-Excel 库有 ->download('xls') 和 ->export('xls') 方法,但这些似乎只返回原始内容,在开发人员的控制台中可见:

在右侧,您可以看到文件已创建并存储为“Report.xls”,并且可能原始内容是正确的内容 - 但我不知道为什么它只是将原始内容吐到控制台中。我可以在 Excel 中打开 Report.xls 文件,所以我知道它没有损坏。

我也尝试使用 Laravel 5 Response::download() 函数并为下载设置标题,但它也在控制台中吐出原始内容而不是下载文件:

我使用的代码是一个 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 内存中,这可以解释为什么我在控制台中获取内容而不是文件。

【问题讨论】:

    标签: excel laravel-5 xls download laravel-excel


    【解决方案1】:

    尝试在Excel::create()之前添加以下代码:

    ob_end_clean();
    ob_start();
    

    示例代码:

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

    这对我有用。

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 2016-08-09
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      相关资源
      最近更新 更多