我正在为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”)的方法,但这些似乎只返回原始内容,这是开发商的控制台可见:
在右边,你可以看到文件的创建和“的report.xls”存储,想必原始内容是正确的内容 - 但我不知道为什么它只是吐痰原始内容进入安慰。我可以在Excel中打开Report.xls文件,所以我知道它没有损坏。
我也尝试使用Laravel 5响应::下载()函数,以及下载设置头,但它也吐出控制台原始内容,而不是下载的文件:
我正在使用的代码是一个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内存中存储结果,这可以解释为什么我在控制台中获取内容而不是文件。