PHPExcel: Download the Excel file on the client side
When generating dynamic reports using PHPExcel, a common friction point is ensuring the resulting file downloads correctly to the client rather than saving to the server's temporary storage or causing browser errors. The core issue often stems from a misunderstanding of how the library handles internal character encoding versus how the HTTP response headers dictate file handling.
If you are working with older versions of PHPExcel (specifically around 1.7.x), the library expects the underlying data and string operations to be handled in ANSI encoding, not UTF-8. While modern PHP scripts default to UTF-8, mixing these encodings during the save operation can result in a "corrupt" file on the client side. Users might encounter garbled text (often appearing as garbage characters) or receive warnings from Excel stating that the file needs repair.
To resolve this without manually forcing every string conversion, you must align your HTTP headers with the file format you intend to serve. The standard approach for triggering a download is setting specific headers before calling the writer's save method. However, simply adding headers like Content-Disposition: attachment is not enough if the internal encoding of the generated XML or BIFF stream does not match what Excel expects for that specific version.
Here is the correct implementation pattern to ensure the file downloads as a binary attachment with the proper client-side handling:
<?php
// ... (Database fetching and PHPExcel object creation logic) ...
// Create the writer instance
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// Set headers to force download instead of inline display or server save
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
// Save directly to output stream
$objWriter->save('php://output');
?>
The critical detail here is the use of php://output. When you call $objWriter->save('results.xlsx'), PHP opens a file handle on the server filesystem, writes the binary data there, and then attempts to send that file. This often leads to the file being saved on the server with restricted permissions or failing entirely if the directory is not writable. By piping directly to php://output, you bypass the intermediate file system step, ensuring the binary stream goes straight to the HTTP response body.
The source material highlights a specific nuance: even with these headers in place, if the script itself is saved as UTF-8 and contains non-ASCII characters that are not explicitly handled during the write process, PHPExcel might generate a malformed file structure. The solution identified in community discussions for this version of the library was to ensure the environment treats the data stream as ANSI-compatible during the writing phase, or to accept that manual character set adjustments were required at the time of library development.
While migrating to PhpSpreadsheet is often recommended for new projects due to its active maintenance and native UTF-8 support, adhering to these header configurations remains necessary when maintaining legacy systems using PHPExcel. The headers ensure the browser interprets the response as a downloadable attachment (application/vnd.ms-excel) rather than attempting to render it as text or cache it incorrectly. Always verify that your server's output buffering is disabled before sending headers if you are appending data dynamically, though for direct file generation via php://output, this is typically handled automatically by the stream wrapper.