开发者

PHPExcel in CakePHP: error--excel file incompatible or corrupted

I'm using Cake; as I open the excel file in the browser with a function generate:

I'm getting this error from Microsoft Excel:

Excel cannot open the fi开发者_如何转开发le 'Groups list .xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

I've tried removing the spaces in the filename after downloading it from the browser and opened it again, it shows the same error as above. Anyone experienced this and solved it? or any clue to what's going on?

Basically the excel file is opened but empty due to the error above. I've read a similar problem which says removing space at the end of ?> tag in one of the component files. But I don't know which component file..?

P.S. I'm using Microsoft Excel 2010, could this be the reason? Does PHPExcel currently work for a--"Microsoft Excel 2010" ?


The PHPExcel Excel2007 Writer should generate valid xlsx files that can be read by Excel 2007, Excel 2010 and by Excel 2003 with the compatibility pack. I actually do most of my testing with Excel2010 and Excel2003.

First thing to check is if there are any spurious characters being echoed into the file by opening it in a text editor. If there are, then those texts can be a guide to locate the problem. In particular, watch out for spaces or line breaks as the first characters in the file.

An xlsx file is a zipped collection of xml files, so if there are no obvious text strings (beside the PK signature) visible in a text editor, then try unzipping it (using whatever unzipper is appropriate for your operating platform). That may give other error messages which could help diagnose the problem.

If you're running on a Windows platform, then there have been some buggy versions of the ZipArchive extension (php_zip.dll) that can cause this error. The latest SVN code allows you to use PCLZip as an alternative to ZipArchive.

If you're still having problems even then, try posting on the PHPExcel discussion forums with all the details.


Set up empty layout with proper headers for creating/downloading your excel file

Here is example with PDF file

<?php 
 header("Content-type: application/pdf"); 
 echo $content_for_layout; 
?> 


Did you try this?

<?php
$file="test.xls";
$test="<table border=1><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo $test;
?>

Thanks


I force my users to download Excel sheets using the following code in my Controller (I use PHPExcel to generate reports on my CakePHP applications):

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.'myreportname.xls"');
header('Cache-Control: max-age=0');

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

$this->render(false);
  • $objPHPExcel | This is my sheet, a PHPExcel() Object

  • $this->render(false); | To avoid anything to be outputed to client browser, making sure you are not screwing the Excel file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜