I use the library as a means of generating Excel documents. Only I need to either save the file to the root/storage/app/public/export/file.xlsx directory, or immediately download the file in the browser. Everything is implemented in Laravel 9. How can I write the code correctly?
My code, now:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
...
$spreadsheet = new Spreadsheet();
$activeWorksheet = $spreadsheet->getActiveSheet();
$activeWorksheet->setCellValue('A1', 'Hello, World!');
$writer = new Xlsx($spreadsheet);
$writer->save("file.xlsx");
The file is saved to a folder 'public'.
If your file is saved in the public folder, you can download it easily.
A more similar question is already here. Please have a read.