I'm creating an API with slimframework and I have a route where I'm trying to export data to an excel file and download it instead of saving it but when I try to do it it outputs text like this ��ࡱ�;�
, the thing is that I have three ways of getting data (different sql queries) and only one fails, could anyone explain me why?
This is one of the arrays that works perfectly:
Array
(
[0] => Array
(
[id_asistente] => 1
[nombre] => adfadsfas
[inst_procedencia] => sadfsa
[perfil_academico] => asdfasdf
[estado] => asdfsaf
[correo] => mail@gmail.com
[cursos_inscritos] => 1
)
[1] => Array
(
[id_asistente] => 2
[nombre] => asdfsafsaf
[inst_procedencia] => asdfasfsa
[perfil_academico] => asdfasdfsdf
[estado] => sdfdsafadfs
[correo] => mailll@gmail.com
[cursos_inscritos] => 1
)
[2] => Array
(
[id_asistente] => 3
[nombre] => asdfasfsfa
[inst_procedencia] => asdffds
[perfil_academico] => asdfasdfdsaf
[estado] => asdfasdf
[correo] => edu@gmail.com
[cursos_inscritos] => 0
)
)
And this is the array that, I don't know why, it doesn't work:
Array
(
[0] => Array
(
[id_asistente] => 2
[nombre] => asdasdasdad
[inst_procedencia] => Asda
[perfil_academico] => asdasd
[estado] => asdass
[correo] => mailll@gmail.com
[asistencia] => 1
)
[1] => Array
(
[id_asistente] => 4
[nombre] => Aasdasdad
[inst_procedencia] => asdsa
[perfil_academico] => asdasd
[estado] => asdasdas
[correo] => asdasdsa@adasdsa.com
[asistencia] => 1
)
[2] => Array
(
[id_asistente] => 6
[nombre] => asdasdas
[inst_procedencia] => asdasd
[perfil_academico] => asdasd
[estado] => asddas
[correo] => asdasdsa@gmail.com
[asistencia] => 1
)
)
And this is the function where I try to download the file:
function exportToExcel($data, $filename) {
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($data, NULL, 'A1');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
}
To be honest I don't know if it has something to do with the array but I can't think of another reason, any help would be appreciated.