I use angular 6 with PHP and I want to download an excel file using phpspreadsheet.
When I use the default code
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
the file is generated to the php folder without being downloaded and works fine. Now I would like to download it and select where to save it.
I use the code that I found in the documentation
// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
but the file is not downloaded and I get this in my browser:
At the begining of my PHP file I have
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
My service in Angular is:
export_excel(localUserid, date, projectid) {
return this.http.post(this.apiUrl, {localUserid, date, projectid},
{
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
}
I pass some values to PHP. I tried to change the headers in Angular but nothing worked. Does anyone knows how to solve it? Thank you.