At present I'm storing data in a variable which contains HTML table. I got this idea from How to format excel file with styles, fonts, colors, tables etc with pure PHP?
$table = '<table>';
$table .= '<tr>';
$table .= '<td>1st row, 1st cell</td>';
$table .= '<td>1st row, 2nd cell</td>';
$table .= '</tr>';
$table .= '<tr>';
$table .= '<td>2nd row, 1st cell</td>';
$table .= '<td>2nd row, 2nd cell</td>';
$table .= '</tr>';
$table .= '</table>';
downloadAsExcel($table, 'somefilename');
function downloadAsExcel($html, $filename)
{
header("Content-Type: application/excel");
header("Content-Disposition: attachment; filename=".ucwords($filename)."-Data.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $html
}
This is working fine and the output is as expected. However, I was wondering if I could create multiple sheets in a single excel file using a similar approach.
It would be wonderful if it could be done as I don't want to use a library to do the job. I've looked at PHPExcel
, ExcelReader
, DocRaptor
etc and so I'm looking for answers (if at all it is possible) which don't use such libraries.
UPDATE : Almost forgot to mention - I also came across this http://webcheatsheet.com/php/create_word_excel_csv_files_with_php.php#excelcom but again this requires me to have MS Excel installed on the server, which I can't as it's a shared hosting account.
PS: I'm fine with the warning dialog that appears while opening an Excel file created by the above method.