I am trying to make multiple dynamic worksheets through the help PHPSpreadSheet. I am fetching the data from PHPMyAdmin and the output based I am creating the sheets. I am getting trouble with the sheet is creating multiples times. I also read other articles on this same or other platform but I couldn't achieve the result I am looking for.
Please help me in how I create a dynamic sheet.
What I am trying to do.. I am creating a dynamic sheets and each I will add foreach for sheet contains.
$stmt = $con->prepare("SELECT * FROM `table` ORDER BY `ID` ASC");
$stmt->execute();
$result = $stmt->rowCount();
if ($result > 0) {
$result = $stmt->fetchAll();
$spreadsheet = new Spreadsheet();
foreach ($result as $data => $value) {
$sheet_1 = $spreadsheet->getActiveSheet();
$sheet_1->setTitle($value['user_name']);
$sheet_1->setCellValue('A1', 'Sheet1');
$sheet_2 = $spreadsheet->createSheet();
$sheet_2->setTitle($value['user_name']);
$sheet_2->setCellValue('A1', 'Sheet2');
}
}