Load one template on several sheet

Topics: Developer Forum, User Forum
Jul 27, 2014 at 8:17 AM
Hi,
I am trying to load a template and copy or load the same template on several sheet, i have been able to load it on the first sheet but the second sheet loads without the template, can anyone assist pls.

sample of my code

$inputFileType = 'Excel2007';
$inputFileName = 'Invoice.xlsx';

$outputFileType = 'Excel2007';
$outputFileName = 'myInvoice.xlsX';

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->setActiveSheetIndex(0);

// Add your new data to the template
$objPHPExcel->getActiveSheet()->insertNewRowBefore(4,1);
$objPHPExcel->getActiveSheet()->setCellValue('A3', 'ISBN 962-571-8926');
$objPHPExcel->getActiveSheet()->setCellValue('B3', 'Excel for dummies');
$objPHPExcel->getActiveSheet()->setCellValue('C3', 17.99);
$objPHPExcel->getActiveSheet()->setCellValue('D3', 2);
$objPHPExcel->getActiveSheet()->setCellValue('E3', '=C4D4');

// Write out as the new file
$objPHPExcel->getActiveSheet()->setTitle('commission');


$objPHPExcel->createSheet();
$objPHPExcel->load($inputFileName);

$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A3', 'ISBN 962-571-8926');
$objPHPExcel->getActiveSheet()->setCellValue('B3', 'Excel for dummies');
$objPHPExcel->getActiveSheet()->setCellValue('C3', 17.99);
$objPHPExcel->getActiveSheet()->setCellValue('D3', 2);
$objPHPExcel->getActiveSheet()->setCellValue('E3', '=C4
D4');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
$objPHPExcel->getActiveSheet()->setTitle('NEXT');

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$outputFileName.'"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
Coordinator
Jul 27, 2014 at 2:26 PM
You might want to try cloning the sheet before you populate it
$clonedSheet = clone $objPHPExcel->getActiveSheet();
then use
$objPHPExcel->addSheet($clonedSheet);
instead of
$objPHPExcel->createSheet(); 
 $objPHPExcel->load($inputFileName); 
Jul 27, 2014 at 6:57 PM
Thanks for your reply, but I have used the clone sheet function that works well, but I would still like to kno just incase I have a case wherein I would like to have one excel file with different template , because then the clone feature wouldn't help.
Coordinator
Jul 27, 2014 at 7:12 PM
If you're working with different templates; then you also have the option of the addExternalSheet() method.
Jul 27, 2014 at 9:12 PM
Thanks ill try that method