insert image with template excel

Topics: Developer Forum, User Forum
Sep 26, 2012 at 4:32 AM

$objTpl = PHPExcel_IOFactory::load("template.xlsx"); -->using template excel 2007

// Instantiate the module
$objTpl = new PHPExcel();
// Add a drawing to the worksheet
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('PHPExcel logo');
$objDrawing->setDescription('PHPExcel logo');
$objDrawing->setCoordinates('C33'); -->want to insert image in C33

$objTpl->getActiveSheet()->setCellValue('C12', stripslashes($data[0]['TGL_USUL']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('A15', stripslashes($data[0]['LATAR_BELAKANG']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('A25', stripslashes($data[0]['PERBAIKAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H15', stripslashes($data[0]['MANFAAT']));//mengambil nik karyawan pada index 2

//prepare download
$filename=mt_rand(1,100000).'.xlsx'; //just some random filename
header('Content-Type: application/');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
exit; //done.. exiting
I tried to insert images and text into a excel template that I had, text and images are from PHP.
when replace text values​​, excel template format as the original template,
but when inserting images, excel template into chaos, the format is not the same.
this is why?
please help


Sep 26, 2012 at 7:33 AM

What do you mean by "not the same". Are you losing cell styling or data? Are column widths changing?


Sep 26, 2012 at 8:25 AM
Edited Sep 26, 2012 at 8:33 AM

Dear Mr.Mark..

I mean in this case,I losing styling cell of template and column changing,

and the name of Worksheet changing to..

if i don't insert image the style of format template not losing and name of worksheet not change..

is cause of this?


Sep 26, 2012 at 8:57 AM

in the first two lines, you open your template ($objTpl), then you completely discard it, and overwrite it with a new PHPExcel object, obviously the styling info of your template is not being used in the rest of your code!

Sep 26, 2012 at 10:59 AM

so how should I fix it sir?

Sep 26, 2012 at 11:52 AM

yes,, i has done..

image can show,, and format keep the same template..

thanks for helping me Mr.Mark and Mr.Borft


Sep 26, 2012 at 11:54 AM

Well spotted borft