I\'m trying to download Excel files (xlsx) using PHPExcel as follows.
require_once(\"../../phpExcel/Classes/PHPExcel.php\");
require_once(\"../../phpExcel/Cl
I Put just exit; After
PHPExcel_IOFactory::createWriter($phpExcelObject, 'Excel2007')->save("php://output");
and it work's for me :)
$file_name .= "_".\Carbon\Carbon::now()->format('d_m_Y_H_i').'.xlsx';
// Prepare To Download
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename='.$file_name);
header('Cache-Control: max-age=0');
header('Pragma: public');
PHPExcel_IOFactory::createWriter($phpExcelObject, 'Excel2007')->save("php://output");
exit;
As told by questioning user Tiny on his own comment:
Finally, it worked. I just added exit at the end of my PHP script (at the end of the first code snippet in the question). Thanks very much all of you for giving me useful hints.
To give some constructive additional tips on this kind of problems:
?>
on all your PHP script files, that way you know that you're not sending any aditional invisible whitespace at the end of it.Just before the header
calls, you can check if headers has been wrongfully sent with:
if ( headers_sent() ) die("**Error: headers sent");
If you can't prevent that some function call sends undesirable strings to the browser, you can "erase" all of it using at the very beginning of your script:
ob_start();
and then, just before the first headers
call, use:
ob_clean();
Be careful that with doing so will prevent you for receiving error feedback.
And lastly, as already said, if nothing has to be executed afterwards some point on the script, call exit
or die
.
Well I had this problem, my solution was changing the header Content-type to :
header('Content-Type: application/openxmlformats-officedocument.spreadsheetml.sheet');
I had before:
header('Content-Type: application/vnd.ms-excel');
and I changed the format to Excel2007, I was using Excel5 for excel 2003 xls format.
I tried all the solutions from this page and all the other pages with the same question but not luck. So basically, I just changed the output format of my excels and that solved the problem.
Give the active sheet a title:
$sheet->setTitle('Some title string here');
This might help to someone who tries to download excel using https://github.com/Maatwebsite/Laravel-Excel package in laravel with get
route with ajax
I got response something like PK ¾@G’D²X ð [Content_Types].xml”MNÃ0…÷œ"ò%nY „švAa •(0ö¤±êØ–gúw{&i‰@ÕnbEö{ßøyìÑdÛ¸l mð¥‘×ÁX¿(ÅÛü)¿’òF¹à¡;@1_滘±Øc)j¢x/%ê…Eˆày¦ ©QÄ¿i!£ÒKµ y3ÜJ<§œZ1½0?YÙL%zV cäÖIb7؇û‰ìa/lÙ¥P1:«qáríÍjªÊj0A¯–Íuë""íàÙ(Œ ”Á€WìMä)Tjå
and much more content
i tried many of the solution but fails
finally i achieved by doing this in onclick event
$(document).on('click','#elementId',function(e){
e.preventDefault();
window.location.assign('your action url'+'?param='+ param_data);
}
This error can also be triggered by serving the file without the Content-Length headers.
Look at the example given here in php.net