0

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');
        }

}
Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
Satyam
  • 5
  • 4
  • On my first global scan of your code I see two `{` and three `}`, that won't work. Have you done any basic error detection? See: [Error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display), – KIKO Software Jan 31 '21 at 09:07
  • sorry here in this thread `}` by typo error... help us to create dynamic tabs in spreadsheet – Satyam Jan 31 '21 at 09:11
  • Well, not anymore, after you edited the question. – KIKO Software Jan 31 '21 at 09:14
  • what is my mistake – Satyam Jan 31 '21 at 09:16
  • I don't see a real mistake. You are reusing `$result` in a confusing way, but that should still work. I can't actually test your code to see what's wrong, and you're not giving any information about the problem you're experiencing. Are there simply too many rows in your table? – KIKO Software Jan 31 '21 at 09:24
  • no, I am getting too many worksheets. I have total of 8 data in DB only. but in generated excel their lots of worksheet tabs renamed.. – Satyam Jan 31 '21 at 09:37
  • Sorry, I have no idea what to suggest. You probably need to do some basic debugging. Echo what the code does, instead of outputting the spreadsheet at the end. See, if there's anything unexpected happening. – KIKO Software Jan 31 '21 at 09:40

0 Answers0