tags:

views:

50

answers:

2

hi

please check my coding for create the excel file and download.

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");;
        header("Content-Disposition: attachment;filename=export.xls ");
        header("Content-Transfer-Encoding: binary ");

        echo "<table style='border: 1px solid red;border-color:red' cellspacing='0'>";
        echo "<tr>";
        echo "<th style='background-color:yellow;border:1px solid red'>Link id</th><th style='background-color:yellow;border:1px solid red'>Priority</th><th style='background-color:yellow;border:1px solid red'>Year</th><th style='background-color:yellow;border:1px solid red'>Make</th><th style='background-color:yellow;border:1px solid red'>Model</th><th style='background-color:yellow;border:1px solid red'>Input URL</th><th style='background-color:yellow;border:1px solid red'>Changes Found</th><th style='background-color:yellow;border:1px solid red'>Output filename</th><th style='background-color:yellow;border:1px solid red'>No of Changes</th>";
        echo "</tr>";
        echo "</table>";

thanks in advance

+2  A: 

Use an external library like PHP Excel to create excel file and then send it to download. Is the easiest way and you don't have to reinvent the wheel. I've used this library and is very good. Alternately you think can use COM functions to do the same thing, although I never used them.

Elzo Valugi
+1 for the only really proper solution (although I can see how the simplicity of building a table is tempting.)
Pekka
One of these days, I'll create an HTML reader for PHPExcel that can be used to convert HTML to XLS/XLSX in much the same way Excel can do it. That will allow users to build their HTML tables and export it as a real Excel file
Mark Baker
@Elzo - did you mean for your link to PHP Excel to point to the PHP COM pages?
Mark Baker
@Mark Baker. thanks. It was an error, I had both links with the same number.
Elzo Valugi
+1  A: 

If you write an excelfile with an <table>, excel will send a warning to the user, trying to open the excel-file, that the file doesn't seem to be a regular file, and if they want to open it anyway.

http://i.imgur.com/bx8ZY.png <- Image of the warning

NoLifeKing
Good point and great screenshot, must keep this in mind for future questions, +1 ` `
Pekka