I am trying to extract some sheets in phpexcel as follows (see https://stackoverflow.com/a/10587576/813801 for reference).
$newObjPHPExcel = new PHPExcel();
$newObjPHPExcel->removeSheetByIndex(0); //remove first default sheet
foreach ($sheets as $sheetIndex) {
echo "trying-$sheetIndex\n";
$workSheet = $objPHPExcel->getSheet($sheetIndex);
echo "done-$sheetIndex\n";
$newObjPHPExcel->addExternalSheet($workSheet);
}
(sheets is an array of indexes which are within the bounds of the sheet. I checked with listWorksheetInfo)
If I comment out the the last line $newObjPHPExcel->addExternalSheet($workSheet);
The getSheet method works fine. Otherwise, I get an error:
Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Your requested sheet index: 2 is out of bounds. The actual number of sheets is 1.' in /Xls/PHPExcel/PHPExcel.php:577
Why should newObjPHPExcel interfere with objPHPExcel?
UPDATE: I found a workaround which seems to work. not sure why the other version did not work though.
$newObjPHPExcel = new PHPExcel();
$newObjPHPExcel->removeSheetByIndex(0); //remove first default sheet
foreach ($sheets as $sheetIndex) {
echo "trying-$sheetIndex\n";
$workSheet[] = $objPHPExcel->getSheet($sheetIndex);
echo "done-$sheetIndex\n";
}
foreach ($workSheet as $obj)
$newObjPHPExcel->addExternalSheet($obj);