By using PhpSpreadsheet, it will export the html data to xlsx format. But it cannot add data to a specific worksheet in the excel sheet. How the data write to a specific worksheet? I have to write htmlstring1 to worksheet 1 and htmlstring2 to worksheet 2.
Here is the code
require "vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$htmlString1 = '<table>
<tr>
<td>Hello World</td>
</tr>
<tr>
<td>Hello World</td>
</tr>
</table>';
$htmlString2 = '<table>
<tr>
<td>Hello User</td>
</tr>
</table>';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->loadFromString($htmlString);
$myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'My Data');
// Attach the "My Data" worksheet as the first worksheet in the Spreadsheet object
$spreadsheet->addSheet($myWorkSheet, 1);
//$spreadsheet->getSheetByName('My Data');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="file.xlsx"');
$writer->save("php://output");