我正在为Laravel 5应用程序添加一个导出/下载功能。我找到了Laravel库(http://www.maatwebsite.nl/laravel-excel/docs),它看起来很完美--理想情况下,我可以使用Laravel或html,并创建一个可下载的Excel文件。
到目前为止,我已经开始工作了。我可以创建一个Excel文件,它可以(它是正确的数据和视图),在服务器上的存储/导出/我看到"Report.xls“-这是正确的保存路径。我遇到的问题是,在创建文件之后,我似乎无法通过浏览器下载该文件。
Laravel库有->download('xls')和->export('xls')方法,但是这些方法似乎只返回原始内容,在开发人员的控制台中是可见的:
在右边,您可以看到文件是以"Report.xls“的形式创建和存储的,大概原始内容就是正确的内容--但我不知道为什么它只是将原始内容吐到控制台中。我可以在Excel中打开Report.xls文件,所以我知道它没有损坏。
我还尝试使用Laravel 5响应::download ()函数并为下载设置标题,但它也在控制台中吐出原始内容,而不是下载文件:
我使用的代码是一个针对Controller方法的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讨论了在JavaScript内存中存储AJAX结果的问题,这将解释为什么我在控制台中获取内容而不是文件。
发布于 2016-08-11 05:09:25
尝试在Excel::create()
之前添加下面的代码
ob_end_clean();
ob_start();
示例代码:
ob_end_clean();
ob_start();
Excel::create($filename, function($excel) use($records, $sheetname, $data) {}
这对我有用。
https://stackoverflow.com/questions/35780713
复制相似问题