Export HTML to Excel

Export HTML to Excel

Export to Excel Today I learned a cool trick of how to export data that I have arranged in tables to an excel file quick and easy. Below is a sample of some code. To make it work copy the following code into its own php file. You will notice that in the code there are to function calls to the header. The first is the Content-Type which gives the browser the information needed to use excel to open the document. The second is the Content-disposition. This allows the user the option to save the document as an excel file, with a default or custom filename.

<?php
header("Content-Type: application/vnd.ms-excel; name='excel'");
header("Content-disposition:  attachment; filename=test.xls");
?>
<table border="1">
 <tr>
   <td>test</td>
   <td>=3+4</td>
 </tr>
 <tr>
   <td>test</td>
   <td>test2</td>
 </tr>
 <tr>
   <td>test</td>
   <td>test2</td>
 </tr>
</table>