I am trying to download "Xlsx" file using PhpSpreadSheet and I am sure code is working fine but all I get is these strange characters shows up in my console
I tried nearly all the solutions here in stackoverflow but all give me the same result below in the image
solutions I have tried: solution more solutions
my code
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'ID');
$sheet->setCellValue('B1', 'Name');
$sheet->setCellValue('C1', 'Name2');
$sheet->setCellValue('D1', 'Name3');
$sheet->setCellValue('E1', 'Type');
// Write an .xlsx file
$date = date('d-m-y-'.substr((string)microtime(), 1, 8));
$date = str_replace(".", "", $date);
$filename = "export_".$date.".xlsx";
try {
$writer = new Xlsx($spreadsheet);
$writer->save($filename);
$content = file_get_contents($filename);
} catch(Exception $e) {
exit($e->getMessage());
}
header("Content-Disposition: attachment; filename=".$filename);
unlink($filename);