Problems with create new worksheets

Topics: Developer Forum
Jan 14, 2011 at 3:03 PM


echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();
$objPHPExcel->getSheet(0)->setTitle('Revenue');
$objPHPExcel->createSheet(1);
$objPHPExcel->getActiveSheet()->setTitle('Registrations');
$objPHPExcel->createSheet(2);
$objPHPExcel->getActiveSheet()->setTitle('Spoon');

and at the end i have an excel file with 3 worksheets - first one called spoon, then worksheet, worksheet 1

PHPExcel is always overwriting the first sheet

Tried this - which i found elsewhere here

$objPHPExcel = new PHPExcel();
$objPHPExcel->getSheet(0)->setTitle('Revenue');

$newSheet = new PHPExcel_Worksheet();
$newSheet->setTitle('Samplel2');
$objPHPExcel->addSheet($newSheet,1);

that just dies with a fatal error

PHP Fatal error:  Call to a member function getSheetByName() on a non-object in /var/www/html/dailyreport/classes/PHPExcel/Worksheet.php on line 657

 

So how do i create and use a new worksheet, without overwriting the old one ??

 

Coordinator
Jan 14, 2011 at 3:13 PM
Edited Jan 14, 2011 at 3:15 PM

The createSheet() method doesn't change the active sheet, you need to do that manually.

echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();
$objPHPExcel->getSheet(0)->setTitle('Revenue');
$objPHPExcel->createSheet(1);
$objPHPExcel->setActiveSheetIndex(1)
$objPHPExcel->getActiveSheet()->setTitle('Registrations');
$objPHPExcel->createSheet(2);
$objPHPExcel->setActiveSheetIndex(2)
$objPHPExcel->getActiveSheet()->setTitle('Spoon');

or use the getSheet() method to reference a specified worksheet (the index of the sheet you've just added) rather than getActiveSheet() when setting the title

echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();
$objPHPExcel->getSheet(0)->setTitle('Revenue');
$objPHPExcel->createSheet(1);
$objPHPExcel->getSheet(1)->setTitle('Registrations');
$objPHPExcel->createSheet(2);
$objPHPExcel->getSheet(2)->setTitle('Spoon');
Jan 14, 2011 at 3:20 PM

ahhhhhhh thanks a lot