New Worksheets

Topics: User Forum
Feb 22, 2010 at 4:01 PM

When I create a new work sheet like this:

//set the sheet name

$ws = $pe->createSheet();

Is that sheet now the ActiveSheet when I use the function getActiveSheet()?

I can't find in the documuntation how you set the active sheet.  Would I just do this $pe->getActiveSheet(3)?  Which would set the active sheet to sheet 3.

Thanks for the help.


Feb 22, 2010 at 4:21 PM

You can use:

$pe = new PHPExcel;
//this line creates the sheet, returns the sheet
//that sheet gets passed to getIndex which returns the index in _worksheetCollection
//then use the pe object to set the activesheet to the index returned by getIndex

Feb 22, 2010 at 4:51 PM
Edited Feb 22, 2010 at 4:56 PM

But how do I set the active sheet so that I can switch to a different sheet to work on in the saem workbook?

So when I am done creating the one worksheet I create the next one and then set it to the active sheet.

Feb 22, 2010 at 5:08 PM

I am doing this instead a foreach loop and won't know how many sheets I will be creating as it will all depend on the users query of the database if that helps you understand my problem.

Feb 22, 2010 at 6:25 PM
Edited Feb 22, 2010 at 6:26 PM


I wouldn't even mess with active sheet, just create a sheet, do stuff to it then add it to the workbook. See below:

$pe = new PHPExcel; foreach($users as $user){ //when you create the sheet the sheet gets added to the workbook's _worksheetCollection $wksht = new PHPExcel_Worksheet($pe,'sheet_name'); //do stuff to the worksheet here $wksht->setTitle('foo'); $pe->addSheet($wksht); } $pe->save('/some/file/path');


Apr 16, 2010 at 8:55 PM

I'm getting an error when trying to create "loose" sheets that are unattached to excel documents.


$e = new Excel();

$s = new Sheet( $e );

addStuffTo( $s );

$e->addSheet( $s );




$s = new PHPExcel_Worksheet( $excel );

$s->setCellValue( $coord, $text );
$s->getStyle( $coord )->applyFromArray( array(
  'font' => array(
    'bold' => true
  ) )

The getStyle/apply fails due to the error:

<b>NOTICE</b> [8] Undefined index:   on line 196 in file PHPExcel-1.7.1/PHPExcel.php<br />
PHP Fatal error:  Call to a member function getXSelectedCells() on a non-object in test.php

...however if I insert an $excel->addSheet( $s ), then the style applies successfully.



Apr 20, 2010 at 2:25 AM


Please stick with the createSheet() method:

$s = $excel->createSheet();

$s->setCellValue( $coord, $text );
$s->getStyle( $coord )->applyFromArray( array(
  'font' => array(
    'bold' => true
  ) )

That is the documented way of creating new sheets.