I am using php and I want to export two HTML
tables to excel
file with two sheets
each has one table.
I followed documentation but it only create one sheet with one table.
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
$firstHtmlString = '<table>
<tr>
<td>Hello World</td>
</tr>
</table>';
$secondHtmlString = '<table>
<tr>
<td>Hello World</td>
</tr>
</table>';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->loadFromString($firstHtmlString);
$reader->setSheetIndex(1);
$spreadhseet = $reader->loadFromString($secondHtmlString, $spreadsheet);
$filename='Users.xlsx';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
return $writer->save('php://output');
?>
any help what went wrong?
That's worked version: