Delete 1st sheet / create new sheets in a loop

Topics: Developer Forum, User Forum
May 13, 2011 at 9:23 AM

Hello,

I am trying to use PHPExcel (which is super great btw.) to generate, of course, an Excel file.

I have a loop which for each user in the db creates a new sheet.

So first I create the object

$objPHPExcel = new PHPExcel();

and blank sheet appears.

After this a do a loop which creates the other sheets:

foreach ($users as $user) {
    $objWorksheet1 = $objPHPExcel->createSheet();
    $objWorksheet1->setTitle($user->name);
    ...
}

What can I do to remove the first sheet?

Is there a way to create the PHPExcel object without creating the initial sheet?

Any hints / ideas would be appreciated!

 

Thank you in advance,

V

May 16, 2011 at 9:16 PM

$objPHPExcel->removeSheetByIndex($sheetIndex);

 

 

May 18, 2011 at 2:22 PM

Thank you, it works!

Jan 24, 2013 at 4:00 PM
Edited Jan 24, 2013 at 4:00 PM

I was having this problem, too, and confirmed that joanmvf's suggestion worked.

I'll add the suggestion to add this at the beginning of your routine (before the foreach loop) with the following:

 

$objPHPExcel->removeSheetByIndex(0);

That way, you're guaranteed to be deleting an empty sheet and you don't have to track how many sheets you created.