«
»

archived blog posts

Excel File seems corrupted…

03.02.09 | Comment?

I had this cakephp project that has an option available for the user to download the data as excel file. Now, the problem is when the user downloads the file and attempts to open it using Microsoft Excel 2007, it complains to the user that the file seems to be corrupted (or I think, it views the file being infected by a virus.) But still, you are being offered the option to:  a.) Open the file and risk yourself to open it, b.)Refrain from opening it, c.) Cancel(I think, that’s a cancel button – I don’t remember. I can’t simulate the problem on my own pc. I have MS Excel 2003.)

The cakephp layout file code that is being used is shown below:

{code type=php}

<?php
header(“Content-type: application/xls; charset=UTF-8″);
header(“Pragma:”);
header(“Cache-Control: private,max-age=3600″);
header(“Content-Disposition: filename=\”".time().”-downloadasexcel.xls\”");
echo “<html xmlns:x=\”urn:schemas-microsoft-com:office:excel\”>
<head>
<meta http-equiv=\”Content-Type\” content=\”text/csv; charset=utf-8\”>
</head>
<body>
“;

echo $content_for_layout;
echo “</body></html>”;

?>

{/code}

I think there is something wrong with my HTML code as regards to making excel acknowledge the document as HTML document in excel format. Let me know if you had the same problem as mine. Thanks. :D

UPDATE:

I realized that the problem is due to the trailing blank spaces right just after the classes… you know, after the ‘?>’ of the controller class. Now I know how this becomes dangerous to a web application. I tried to view the html code in windows and it showed a special character (a tab and a space) in the code, but when I tried to view it in Ubuntu (my working environment) it didn’t show up.

Tags: ,

Comments are closed.


«
»