php表格保存按钮,通过按钮单击保存PhpSpreadSheet

首先,您需要在路由中设置端点以使用ajax(在您的情况下为axios)调用它:

Route::get('spreadsheet/download',[

'as' => 'spreadsheet.download',

'uses' => 'SpreadsheetController@download'

]);

在你的控制器中:

public function download ()

{

$fileContents = Storage::disk('local')->get($pathToTheFile);

$response = Response::make($fileContents, 200);

$response->header('Content-Type', Storage::disk('local')->mimeType($pathToTheFile));

return $response;

}

如果您没有该文件,可以将其保存到php://output:

public function download ()

{

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header('Content-Disposition: attachment; filename="file.xlsx"');

$writer->save("php://output");

}

现在你只需要调用端点/电子表格/下载来开始下载,但是正常的< a href =“/ spreadsheet / download”>下载< / a>会工作.

希望这对你有所帮助.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值