I'm serving HTML to an Excel file by doing:
header('Content-type: application/excel');
header('Content-Disposition: attachment; filename=testfile.xls');
$html = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">
<head>
<!--[if gte mso 9]>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Sheet1</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo/>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->
</head>
<body>
<table>
<thead><tr><th>Header</th></tr></thead>
<tbody><tr><td>Values</td></tr></tbody>
</table>
</body>
</html>";
echo $html;
This creates an Excel file with 1 worksheet but I need a second worksheet. So what I did was to duplicate
<x:ExcelWorksheet>
<x:Name>Sheet2</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo/>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
and just used a different name. This worked as expected but now I don't know how to put data into this second worksheet. Is there a way for me to specify which worksheet the <table>
is supposed to go into?
Notes
I searched around for a bit and found this and this.
The solutions on the first link uses XML instead of HTML and I would have been okay with it but I couldn't get multi-line to display properly because I can't set the row to auto fit the height based on the content. It only shows the first line.
The second link talks about MHTML (Multipart HTML) but I could not understand it.