Download Excel File Using PHPExcel

I use the following headers to output the excel sheet generated through PHPExcel:

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output'); 

Instead of the tab text, it is sent to the browser:

PK      &DG 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 

Can someone help me with the correct headers to download the excel file? Many thanks.

Change 1:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output'); 

Does not work

+4
source share
3 answers

Incorrect content type and file type will not help:

application/vnd.ms-excelfor .xlsfiles created using Excel5Writer

application/vnd.openxmlformats-officedocument.spreadsheetml.sheetused for .xlsxfiles created using Excel2007Writer

, gibberish ( xlsx) , , - , , , .

+8

ob_end_clean();
$objWriter->save('php://output');
exit();
+5

This problem has stolen my life! Kill him like this:

ob_get_clean();
$objWriter->save('php://output');
ob_end_flush();
0
source

All Articles