addsheet and load external excel

Topics: User Forum
Apr 19, 2015 at 7:58 AM
Edited Apr 19, 2015 at 8:01 AM
good morning,
sorry for my english, but i am italian...
I have to make a series of sheet with, within each, an external excel (matrice.xls).
what can i do?
this is my code:

require_once '../phpexcel/Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();
$inputFileName = 'matrice.xls';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

///// 1° SHEET
// load matrix
$objPHPExcel = $objReader->load($inputFileName);

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('K9', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('K10', 'world!');
// rename il sheet
$objPHPExcel->getSheet(0)->setTitle('SHEET 1');

///// 2° SHEET
// Create a new worksheet at index 1
$objPHPExcel->createSheet(1);
$objPHPExcel->setActiveSheetIndex(1);
// load matrix
$objPHPExcel = $objReader->load($inputFileName);
// write
$objPHPExcel->getActiveSheet()->SetCellValue('K9', '2Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('K10', '2world!');

// rename il sheet
$objPHPExcel->getSheet()->setTitle('SHEET 2');

// Salvo il file Excel 2007
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('myfile.xlsx');

this is error log:
[19-Apr-2015 08:26:24 Europe/Rome] PHP Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1.' in /home/msericecp/public_html/new/ms/phpexcel/Classes/PHPExcel.php:688
Stack trace:
0 /home/msericecp/public_html/new/ms/turni/test.php(39): PHPExcel->setActiveSheetIndex(1)
1 {main}
thrown in /home/msericecp/public_html/new/ms/phpexcel/Classes/PHPExcel.php on line 688


tanks!