PHPExcel Rendering Corrupt Excels

Topics: Developer Forum
Jul 23, 2014 at 1:43 PM
There are no possible white spaces when the file is being rendered.
<?php
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

//Functions for rendering data

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="SeedApplicants.xls"');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>

I am trying to render blank excel for testing purpose, but still its rendering me a excel which is not readable by MSExcel (2007 & 2013). Tested in Ubuntu and Windows. I checked the file by opening it in notepad, I didn't find any php errors or some echo statements.

Can anyone help me in this? It was working fine, but all of sudden it stopped. I am using 1.7.8v of phpexcel and 5.3 of PHP.
Coordinator
Jul 23, 2014 at 1:57 PM
As always, open the file in a text editor and look for any leading/trailing whitespace characters, any BOM header in the first 3 bytes of the file, or any obvious plaintext error messages
Jul 23, 2014 at 3:25 PM
Yes, I have checked everything, There are absolutely no leading or trailing whitespace characters. And no BOM header and error messages.
Coordinator
Jul 23, 2014 at 5:38 PM
Have you tried writing to a file on the server, then doing a manual file transfer to your local PC to see if that will load in MS Excel correctly?
Jul 24, 2014 at 1:26 PM
Yes , i tried that. It is rendering a blank excel file.
Jul 25, 2014 at 10:09 AM
when i remove
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="SeedApplicants.xlsx"');

and change
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
to
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

and change
$objWriter->save('php://output');
to
$objWriter->save(str_replace('.php', '.xlsx', FILE));

The file rendered works just fine.
Coordinator
Jul 25, 2014 at 11:53 AM
So if, instead of using the IO factory, you did
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 

$objWriter->save(str_replace('.php', '.xls', FILE)); 
does it work?
Jul 25, 2014 at 2:28 PM
Thats right, the file saved on my local machine and worked fine. But since i want the client to be able to download it for himself, i add the headers and everything goes wrong.
Coordinator
Jul 25, 2014 at 3:12 PM
So the PHPExcel save() works without problem for both Excel and Excel2007: which narrows it down to something in the IOFactory, or in your script:

If you try doing
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save(str_replace('.php', '.xls', FILE)); 
does that create a valid Excel file on the server?
Jul 28, 2014 at 6:53 AM
Edited Jul 28, 2014 at 7:06 AM
The file got created and was a valid one.
Aug 2, 2014 at 1:38 AM
i got the same problem on Ubuntu Server with php 5.5.3.

but i think it is server related, cuz i didn't change anything and a few days ago i could open xls files on LibreOffice 4.3.0 and now i can't.