【问题标题】:Saving a PHPSpreadSheet Object保存 PHPSpreadSheet 对象
【发布时间】:2018-10-11 17:50:46
【问题描述】:

我有一个 Laravel 应用程序部署到 Google App Engine。根据 GAE 的架构规则,您不能将文件保存到本地路径。我正在使用PHP Spreadsheet 生成 Xlsx 文件并为用户下载它们。所以这在本地有效,但在部署的应用程序中无效..

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('storage/SomeExcelFile.xlsx');

然后我可以用..下载文件。

return Storage::disk('public')->download('SomeExcelFile.xlsx');

但是无法在应用程序的生产版本上本地保存文件,我将如何存储我的文件? 我将我的应用程序连接到可以上传文件的存储桶。但是我需要文件路径来执行此操作,并且由于该文件是在应用程序中以脚本方式创建的,并且在部署之前从未存储过,因此我找不到存储它的方法。

据我所知,您无法将 xlsx 文件直接写入磁盘。例如,我可以将带有文本sample data 的文本文件写入我的谷歌云存储桶..

Storage::disk('gcs')->put('String.txt', 'sample data');

但是找不到使用我的 excel 文件的方法。另外应用部署在Flex环境中,所以不能使用文件url结构gs://

【问题讨论】:

  • 您没有保存任何文件,例如会话或日志?您可以在不创建新文件的情况下打开/写入文件吗?也许您可以使用现有文件作为解决方法。

标签: php laravel google-app-engine google-cloud-storage phpspreadsheet


【解决方案1】:

找到了解决使用Symfony 流式传输文件的方法

$response = new StreamedResponse(
    function () use ($writer) {
        $writer->save('php://output');
    }
);
$response->headers->set('Content-Type', 'application/vnd.ms-excel');
$response->headers->set('Content-Disposition', 'attachment;filename="Services Reformatted.xlsx"');
$response->headers->set('Cache-Control', 'max-age=0');
return $response;

【讨论】:

    【解决方案2】:

    试试这个方法:

    file_put_content('your/project/path/public/filename.xlsx',$writer);

    【讨论】:

    • 你的意思是file_put_contents?仅在本地工作实际上并不能正确写入文件,会损坏它。
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 2018-12-02
    • 2018-10-23
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    相关资源
    最近更新 更多