I have a function in Laravel that allows the user to export reports from their application. So far exporting .csv is working fine, though when giving them the option to export out to .xlsx it throws an error about the extension not being valid.
Below is the function that is called for exporting.
public function export($fileName, $data, $fileType) {
$xls = new PHPExcel();
$xls->setActiveSheetIndex(0);
if (count($data) > 0) {
$acols = array_keys($data[0]->toArray());
$xls->getActiveSheet()->fromArray($acols, NULL, 'A1');
$xls->getActiveSheet()->fromArray($data->toArray(), NULL, 'A2');
switch ($fileType) {
case 'csv':
$objWriter = new PHPExcel_Writer_CSV($xls);
break;
case 'xlsx':
$objWriter = new PHPExcel_Writer_Excel2007($xls);
$objWriter->setOffice2003Compatibility(true);
break;
case 'html':
$objWriter = new PHPExcel_Writer_HTML($xls);
return $objWriter->generateSheetData();
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=$fileName");
header("Content-Transfer-Encoding: binary ");
$objWriter->save('php://output');
return;
}
return Redirect::back();
}