How to fill a Excel template

Topics: Developer Forum
Sep 28, 2010 at 4:50 PM

Im newbe here, i have seen some example. one create a excel file, another read from excel file...

I have excel templates with images and columns tittles, is there a way that i can open these templates and fill them with the data generated with the scripts.

Sep 28, 2010 at 10:51 PM
// Read the template file
$inputFileType = 'Excel5';
$inputFileName = 'template.xls';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);

// Add your new data to the template

$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');
// Write out as the new file
$outputFileType = 'Excel5';
$outputFileName = 'myInvoice.xls';
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
Sep 29, 2010 at 11:42 PM

I've added a 30template.php example to the /Tests directory that demonstrates writing to a template spreadsheet

Sep 30, 2010 at 7:29 PM


It was really usefull

Nov 4, 2010 at 11:50 PM

First-timer here.  I needed some type of Excel import/export tool, and I think PHPExcel is going to be an incredible resource.  I am very impressed with the documentation I have read so far with the feature set; I couldn't find a needed feature that was not supported (even conditional formatting - really?!?  Just incredible).  This forum posed a scenario that I would definitely use - I have an Excel template saved that I would like to write to dynamically based on the object that the user wants to update.  I downloaded version 1.7.4 but did not see the 30template.php file referenced above.  How can I get a copy of that file?  If it is just a file with the code referenced above, please disregard and I will just use that code.

Looking forward to learning PHPExcel!  Hoping that my VBA and PHP knowledge will make it easy on me...

Nov 5, 2010 at 12:52 AM

The 30template.php is in the latest SVN code, which can be accessed through the "Source Code" tab her on

Apr 17, 2014 at 5:21 AM
Edited Apr 17, 2014 at 5:35 AM
Hi, I'm Ecuadorian, I do not speak very good English, but I hope you understand me and can help me; I'm starting to use PHPExcel and I loaded the files in my web root, but to run some examples including 30template.php gives me the following error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 24576 bytes) in C:\AppServ\www\Classes\PHPExcel\Writer\Excel5\Worksheet.php on line 1538

please if someone could help me, I would be very grateful.