简体   繁体   中英

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

What I'd like to achieve:

Programmatically insert an image (from a file) to the header section of an Excel template file.

What I have tried so far:

I have based my work on information from the examples in the docs, and combined example 4 and example 30. Both scripts work excellent separately. However, if I load a template (blank xls file) and insert an image to the header, the image does not appear when I open the workbook.

So far I have tried Excel 2007 and Excel 95, with no success.

I have also tried to insert the exact same image into cell A1 in the workbook, which works perfectly each time.

My code:

<?php

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

date_default_timezone_set('Europe/London');

require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("templates/30template.xls");

// Create new picture object and insert picture
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Image');
$objDrawing->setDescription('Image');
$objDrawing->setPath('./images/phpexcel_logo.gif');
$objDrawing->setHeight(50);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

// Add a drawing to the header
$objDrawing = new PHPExcel_Worksheet_HeaderFooterDrawing();
$objDrawing->setName('Image');
$objDrawing->setPath('./images/phpexcel_logo.gif');
$objDrawing->setHeight(36);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT);

// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

// Save Excel 95 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM