pillarOfLight pillarOfLight - 1 month ago 17
HTML Question

Excel html table export

So I have this code:

$output = '
<table class="sticky-enabled">
<thead><tr><th>Text0</th><th>Text1</th><th>Text2</th> </tr></thead>
<tbody id="">
<tr class="odd"><td>7</td><td>texttt</td><td>texttt</td> </tr>
</table>';



header("Content-type: application/vnd.ms-excel;");
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=file.xls");
print chr(255) . chr(254) . mb_convert_encoding($output, 'UTF-16LE', 'UTF-8');
exit;


And the thing would download an Excel file with the table.

There are 2 problems though:


  1. When I open it with Excel it'll display the file you're trying to open is in different format from the extension error

  2. When I try to save it, the default file extension is .htm instead of xls



Is there a way to manipulate the headers etc so that these 2 problems are resolved?

Answer
header('Content-type : text/csv');

and print it as a Comma separated value file. You can't just print an HTML table into an .xls or .xlsx