copy sheet,paste in the same file and edit new sheet

Topics: User Forum
Feb 27, 2014 at 12:48 PM
Edited Feb 27, 2014 at 12:49 PM
Hello, i'm using phpexcel but i'm new with this library. i'm try to do this: i need to open a excel file wich contents several sheets, copy the last sheet and create a new sheet with the data of the copied sheet and edit the new last sheet.

i tried to do the copy of the new sheet with the next code:

$objPHPExcel = new PHPExcel();
$objPHPExcel = PHPExcel_IOFactory::load($filepath);
$sheetName = $objPHPExcel->getActiveSheet()->getTitle();
$newSheet = $objPHPExcel->getSheetByName($sheetName);
$objPHPExcel->addSheet($newSheet->setTitle('aaaa'));
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

but response this error:

Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Workbook already contains a worksheet named 'aaaa'. Rename this worksheet first.' in C:\localhost\wamp\www\excel\PHPExcel\Classes\PHPExcel.php on line 267

and this exception:

PHPExcel_Exception: Workbook already contains a worksheet named 'aaaa'. Rename this worksheet first. in C:\localhost\wamp\www\excel\PHPExcel\Classes\PHPExcel.php on line 267