You've modified the question since I posted the answer to your original question.
If you need to create a new worksheet from the template after you've already loaded the original template and populated it with data, then you'll need to reload the template as a new PHPExcel object, and inject the worksheet from that into your original workbook:
$filePath = public_path('Template.xls');
$objPHPExcel = PHPExcel_IOFactory::load($filePath);
// fill with data
$template = PHPExcel_IOFactory::load($filePath);
$newSheet = clone $template->getActiveSheet();
$objPHPExcel->addExternalSheet($newSheet);
the addExternalSheet()
method ensures that all styles and structure (merged cells) are copied cleanly with the worksheet when it is added. Simply using the addSheet()
method isn't guaranteed to copy that information correctly