PHPExcel: Download the Excel file on the client side

Remove the following inclusion:

require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';

You declared the object twice. Remove one of them:

// create new PHPExcel object
$objPHPExcel = new PHPExcel();

Insert the following headers just before creating the Writer:

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

Instead of the following (which actually saves the file in the server):

$objWriter->save('results.xlsx');

Insert the following (which will create the downloadable file):

$objWriter->save("php://output");

This should solve the gibberish text. If you still get such text, insert the following code before the last line ($objWriter->save("php://output");):

ob_clean();

This worked for me. Hope it helps.


This should work, try to modify your code like this:

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=my_excel_filename.xls");
header("Pragma: no-cache");
header("Expires: 0");

flush();

require_once 'PHPExcel.php';

$objPHPExcel = new PHPExcel();

// here fill data to your Excel sheet

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

Tags:

Php

Phpexcel