The following PhpSpreadsheet code is working properly on localhost but not on live server.
THE ISSUE IS : save('php://output'); is not downloading the xlsx file.
PhpSpreadsheet Version : 1.29.0
PHP version is: localhost : 8.1.6 live server : 8.0.28
<?php
require('PhpSpreadsheet/vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Border;
// WORKSHEET 1
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('STACKOVERFLOW');
$spreadsheet->getDefaultStyle()->getFont()->setName('Calibri')->setSize(9);
$sheet->getColumnDimension('A')->setWidth(40);
$sheet->getColumnDimension('B')->setWidth(40);
$sheet->getColumnDimension('C')->setWidth(20);
$sheet->getColumnDimension('D')->setWidth(20);
$sheet->getColumnDimension('E')->setWidth(20);
$sheet->getColumnDimension('F')->setWidth(20);
$sheet->getColumnDimension('G')->setWidth(40);
$sheet->setCellValue('A1', 'UNREAL');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="spreadsheet.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
I changed : $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); to Html and filename="spreadsheet.xlsx" to spreadsheet.html
which is working fine ( file spreadsheet.html got downloaded )
but I want xlsx format.
This is a known issue with PhpSpreadsheet on PHP 8.0.
The comments on the issue on Github list some workarounds, but since PHP 8.0 is rapidly approaching end of life anyway, you'd be better off upgrading your production environment to match your development environment.