How do I load an Excel Template with PHPExcel and write to its cells and also insert images to cells dynamically?
Asked
Active
Viewed 3.7k times
3 Answers
18
You can read your excel template like this with PHPExcel:
$objPHPExcel = PHPExcel_IOFactory::load("./forms/english/cash.xlsx");
and you can write to cells like this:
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', "No")
->setCellValue('B2', "Name")
->setCellValue('C2', "Email")
->setCellValue('D2', "Phone")
->setCellValue('E2', "Address");

MJ X
- 8,506
- 12
- 74
- 99
8
see the example, 30template.php in github site
https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/30template.php
load template :
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("templates/30template.xls");
see in the example write via
$objPHPExcel->getActiveSheet()->setCellValue()
to add image use PHPExcel_Worksheet_Drawing :
// Add an image to the worksheet
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('My Image');
$objDrawing->setDescription('The Image that I am inserting');
$objDrawing->setPath('./images/myImage.png');
$objDrawing->setCoordinates('B2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

Haim Evgi
- 123,187
- 45
- 217
- 223
-
-
Image Resize: $objDrawing->setResizeProportional(true); $objDrawing->setWidthAndHeight(230, 170); – user2039290 Jun 17 '15 at 09:29
1
Now you don't need load templates. Try to use the PHP Excel templator: https://github.com/alhimik1986/php-excel-templator

user3551026
- 515
- 5
- 9