通过按钮单击保存PhpSpreadSheet

栏目: PHP · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/45737485/saving-a-phpspreadsheet-through-button-click
我正在尝试让我的 Laravel 应用程序下载一个带有 phpSpreadSheet

延续PhpExcel的excel文件.但到目前为止,我没有任何运气.我首先尝试通过onClick进行Axios调用,但由于不允许JS保存,因此无效.之后我尝试将按钮附加到Laravel动作,这只是打开一个空页面.

我不知道这里是否有人能够帮助我,但我会保持充满希望

首先,您需要在路由中设置端点以使用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>会工作.

希望这对你有所帮助.

翻译自:https://stackoverflow.com/questions/45737485/saving-a-phpspreadsheet-through-button-click


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Defensive Design for the Web

Defensive Design for the Web

37signals、Matthew Linderman、Jason Fried / New Riders / 2004-3-2 / GBP 18.99

Let's admit it: Things will go wrong online. No matter how carefully you design a site, no matter how much testing you do, customers still encounter problems. So how do you handle these inevitable bre......一起来看看 《Defensive Design for the Web》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具