PHPExcel generated excel file not working. (File format or extension is invalid )

PHPExcel generated excel file not working. (File format or extension is invalid )

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHPExcel Generated Excel File Not Working: File Format or Extension Is Invalid

When a generated file works perfectly on a local machine but fails on a server, the issue usually lies in environment differences rather than the code logic itself. The specific error message "File format or extension is invalid" combined with the fact that an "Open and Repair" operation fixes it points to a structural corruption within the binary stream sent to the browser.

The core problem here is not the PHP code generating the headers, but how the output is being transmitted. On your local PC, the file likely opens because the data is being served directly from disk or through a development server that handles php://output differently than a production web server like Apache or Nginx. The "repair" function works because it attempts to reconstruct the file by reading the XML parts individually rather than parsing the raw binary stream as a single block.

When you switch to a server environment, the most common culprit is output buffering. If any byte precedes the header() calls or gets mixed into the response body before the headers are finalized, the client (Excel) rejects the file entirely. This often happens when developers include debugging code like var_dump($objPHPExcel) or echo statements earlier in the script. Even a single BOM (Byte Order Mark) or an invisible character can break the XML signature validation that Excel performs on .xlsx files.

To resolve this, you must ensure strict output control. The provided code snippet uses php://output, which is generally safe, but it requires that no other data is written to stdout before the headers are sent. Verify that your script does not have any whitespace or BOM at the very top of the file itself.

<?php
// Ensure no whitespace before the opening PHP tag if using strict servers
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// Flush buffers to ensure data is sent immediately and cleanly
if (ob_get_level()) { ob_end_clean(); } 
$objWriter->save('php://output');

exit;

Another frequent issue in server environments involves the Cache-Control header or the interaction between web server configurations and the output stream. Some servers append a BOM to the response by default if they are not configured strictly for XML content types. Ensure your web server configuration does not inject extra headers or encoding markers that PHP isn't expecting.

Additionally, check for any logging or error reporting mechanisms that might be writing to stdout. On a local machine, echo statements often go to the browser. On a server, if you have mixed output streams (e.g., one going to the screen and one to the file), the file becomes corrupted. Disable all non-essential output during the export process.

If you are migrating from PHPExcel to a modern stack, consider switching to PhpSpreadsheet. The original PHPExcel library is no longer maintained, and while it works in legacy setups, it has known issues with newer XML standards that Excel updates enforce. PhpSpreadsheet handles the internal XML structure more robustly and is less prone to generating files that require repair on modern clients.

Finally, verify the MIME type served by the server. While the PHP headers are correct, ensure that the web server (Apache/Nginx) does not override them with its own AddType or TypesConfig directives that might serve the file as plain text or HTML. Consistency between the PHP response and the HTTP protocol is key to preventing Excel from flagging the file format as invalid.

Our network