简体   繁体   中英

Saving a PHPSpreadSheet Object

I have a Laravel application deployed to Google App Engine. Per GAE's architecture rules, you cannot save files to a local path. I am using PHP Spreadsheet to generate Xlsx files and download them for the user. So this works locally but not in the deployed app..

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

Then I can download the file with..

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

But without being able to saves files locally on the production version of the application, how will I store my file? I have my application connected to a storage bucket where I can upload files. But I need the file path to do this, and since this file was created scriptually in the application and was never stored prior to deployment, I can't find a way to store it.

As far as I found you can't write the xlsx file directly to a disk. For example I can write a text file with the text sample data to my google cloud storage bucket..

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

But can't find a way to do it with my excel file. Also the application is deployed in the Flex environment so I can't use the file url structure gs://

Found a work around streaming the file with 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;

试试这种方式:

file_put_contents(public_path('filename.xlsx'), $writer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM