Add Images to a Excel File Using PHP

Adding Image to the Excel in phpexcel in php

Specifying coordinates for the image might help, as per the examples and the documentation

$objDrawing->setCoordinates('A3');

Note that an image isn't in a cell/column/row, but overlaid over the main sheet at the same position as that cell/column/row

PHPSpreadsheet - How do i place a image from link into my excel file?

I got a solution for you. But unfortunately i couldn't find a way to use a url but an image localy. take a look.

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('images/paid.png'); // put your path and image here
$drawing->setCoordinates('B15');
$drawing->setOffsetX(110);
$drawing->setRotation(25);
$drawing->getShadow()->setVisible(true);
$drawing->getShadow()->setDirection(45);
$drawing->setWorksheet($spreadsheet->getActiveSheet());

$writer = new Xlsx($spreadsheet);
$writer->save('image.xlsx');

It's working just fine, enjoy :)

enter image description here

enter image description here

PHPExcel: How do I insert an image to header/footer, using a template?

So I was able to replicate your issue and the only way I got it working was like this:

Change your template file to a 2007 style file and load:

$objPHPExcel = PHPExcel_IOFactory::load('template.xlsx');

Then I added the following to the header code:

$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&');

I don't know why it doesn't automatically add that header info during the writer. But that comes from the big comment in the HeaderFooter file on GitHub - * &G - code for "picture as background". The &L is code for the left section which seemed to be where you wanted it.

The docs or code also mention that if nothing is given for evenHeader then setOddHeader is assumed to be used everywhere and the result file seems to reflect that, it is on every page.

This only works when saving to 2007 style file, not sure if that is a bug in the library or what because saving through Excel works fine for the older format.

Let me know if that doesn't work for you and I can send you the working example I have.



Related Topics



Leave a reply



Submit