使用控制器下载 Laravel Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31808416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:07:03  来源:igfitidea点击:

Laravel Excel Download using Controller

phpangularjslaravelexportlaravel-excel

提问by Nick

So I created a PHP Controller to handle exporting data which is posted by JS. The problem is I can see it creates something in the console but the file download never starts. I tried using ->store (laravel excel) and keeping it in an export folder but again when I try to use

所以我创建了一个 PHP 控制器来处理由 JS 发布的导出数据。问题是我可以看到它在控制台中创建了一些东西,但文件下载从未开始。我尝试使用 ->store (laravel excel) 并将其保存在导出文件夹中,但再次尝试使用时

return \Response::download($result);

it still won't start the download. The problem I'm having is just getting the download to start.

它仍然不会开始下载。我遇到的问题只是开始下载。

Angular Controller

角度控制器

$scope.exportMatrix = function () {
    var postData = {list: $scope.list, matrix: $scope.matrix};
    $http({
        method: 'POST',
        url: '/export',
        dataType: 'obj',
        data: postData,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function (data) {
        console.log(data);
    }).error(function (data) {
        console.log("failed");
    });
}

Route

路线

Route::post('/export', 'ExportController@export');

PHP Controller

PHP 控制器

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App;
use Excel;
use Response;
class ExportController extends Controller {

public function export()
{

    $excel = App::make('excel');

    Excel::create('Test', function($excel) {
        $excel->setTitle('new awesome title');

        $excel->sheet('Sheet', function($sheet) {
            $sheet->fromArray(array(
                array('data1', 'data2'),
                array('data3', 'data4')
            ));
        });


    })->export('xlsx');
}

回答by Nick

In the end I used FileSaver.jsto download the blob it sent back so just load up FileSaver and use it saveAs(blob).

最后,我使用FileSaver.js下载它发回的 blob,因此只需加载 FileSaver 并使用它 saveAs(blob)。

$http({
   method: 'POST',
   url: '/api/v1/download',
   dataType: 'json',
   data: {
       data:data
   },
   responseType: 'arraybuffer',
   headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
   var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});

   saveAs(blob, title + ".xlsx");
}).error(function (data) {
   console.log("failed");
});

I made sure to use

我确保使用

responseType: arraybuffer

响应类型:数组缓冲区

and saveAs type:

和另存为类型:

"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

“应用程序/vnd.openxmlformats-officedocument.spreadsheetml.sheet”

Because reasons.

因为原因。

回答by Lee Nielsen

Route::get('/export', 'ExportController@export');

Route::get('/export', 'ExportController@export');