How to insert an image in the first page header and enlarge it to fit it's content?

Topics: Developer Forum
Jun 9, 2011 at 2:40 PM
Edited Jun 9, 2011 at 2:42 PM

I have an Excel file made with PHPExcel which have an header with a left aligned logo and right aligned date & user text. For the first page, I want a similar header (same logo and same date & user text) but with some added information (title and parameters of the file centered a couples lines later).

This is what I'm doing so far:

<?php
   
    $sheet = $this->_spreadsheet->getActiveSheet(); //_spreadsheet is an instance of PHPExcel
       
    $logo = new PHPExcel_Worksheet_HeaderFooterDrawing();
    $logo->setName('Logo');
    $logo->setPath(DOCUMENT_ROOT . '/public/logo.jpg'); //Path is OK & tested under PHP
    $logo->setHeight(38); //If image is larger/smaller than that, image will be proportionally resized
    $sheet->getHeaderFooter()->addImage($logo, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT);
       
    $sheet->getHeaderFooter()->setOddHeader('&L&G&RExport date: ' . date('Y-m-d H:i:s') . "\n" . 'User: ' . $user->name);
       
    if ($grid->getTitle() != '') {
        $sheet->getHeaderFooter()->setDifferentFirst(true);

        $sheet->getHeaderFooter()->addImage($logo, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT);
        $sheet->getHeaderFooter()->setFirstHeader('&L&G&C&"-,Bold"' . "\n\n\n" . $grid->getTitle() . "\n" . $grid->getParameters() . '&RExport date: ' . date('Y-m-d H:i:s') . "\n" . 'User: ' . $user->name);
    }
   
?>



For the "regular" header, logo and text is all there so everything is OK. For the first page header I have 2 problems:

  1. The logo isn't showing on the first page header (but the text is OK).
  2. Since the centered title will be followed by some text (dynamically loaded with *getParameters*) I want the first page header to stretch to fit it's content.


How can I do this with PHPExcel?

Feb 13, 2014 at 4:42 PM
Edited Feb 13, 2014 at 11:22 PM
I'm facing the same issue. I dont know how to set two different images for the first and "regular" headers when using "getActiveSheet()->getHeaderFooter()->setDifferentFirst(true)".

This is what I have so far:
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setDifferentFirst(true);

$objPHPExcel->getActiveSheet()->getHeaderFooter()->setFirstHeader('&L&G&R&D - &T');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setFirstFooter('&C&"Arial,Normal"Page &P / &N');

$objDrawing = new PHPExcel_Worksheet_HeaderFooterDrawing();
$objDrawing->setName('Logo');
$objDrawing->setPath('img/logo.png');
$objDrawing->setResizeProportional(true);
$objDrawing->setHeight(95);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT);

$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&R&D - &T');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&C&"Arial,Normal"Page &P / &N');

$objDrawing = new PHPExcel_Worksheet_HeaderFooterDrawing();
$objDrawing->setName('Logo2');
$objDrawing->setPath('img/logo.png');
$objDrawing->setResizeProportional(true);
$objDrawing->setHeight(95);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT);
It's showing the image only in the "regular" headers. The first one is blank.

I'm using 1.7.9 version and Excel2007 writer btw.

Anyone could help me, please?
Feb 14, 2014 at 4:05 PM