Hello,
Recently I am using phpexcel and afer many tries I wasn't able to find out how to
make a document with a image in its header.
Here is a simplified code that I am using. Can someone helps me with this issue?!

<?php
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objDrawing = new PHPExcel_Worksheet_HeaderFooterDrawing();
$objDrawing->setPath('header.jpg');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_RIGHT);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader("&C&G");
$objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit('A1',  'CODICE PRATICA');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter      = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('test.xlsx');
?>
<?php
require_once 'PHPExcel-1.7.7/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Mangel Murti");
$objPHPExcel->getProperties()->setLastModifiedBy("Mangel Murti");
$objPHPExcel->getProperties()->setTitle("Testing");
$objPHPExcel->getProperties()->setSubject("Testiin Me");
$objPHPExcel->getProperties()->setDescription("what to see what to get.");

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
$objPHPExcel->getActiveSheet()->setTitle('MyTest');

$gdImage = imagecreatefromjpeg('header.jpg');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');
$objDrawing->setDescription('TEST');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(100);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('test.xlsx');

?>

Thank you Mangel,
I solved my problem with a tricky procedure.
Before adding the excel content I use phpexel load,
to load a template created in MS Office that has the header image as I wanted,
after inserting the rows inside the loaded template I save it where I wanted to :)

hey buddy, did you fixed this problem? How?

Hey, If I can find some help in retreiving images to Excel. I have done so far, Error is: Cannot find image

<?php
//call the autoload
include 'sqlconnect.php';
require 'vendor/autoload.php';
//load phpspreadsheet class using namespaces
use PhpOffice\PhpSpreadsheet\Spreadsheet;
//call xlsx writer class to make an xlsx file
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing as drawing; // Instead PHPExcel_Worksheet_Drawing

//make a new spreadsheet object
$spreadsheet = new Spreadsheet();
//get current active sheet (first sheet)
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Eight-Months');

$query = "select cloneno,name from eighttotwelve where month=8";
$result = $conn->prepare($query);
$result->execute();
$i = 1;

//set the value of cell a1 to "Hello World!"
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

  $logo= $row["name"];
  $logopath = 'upload/'.$logo;
  // $logo = get_home_path() . str_replace(home_url() . 'upload/','', $logo);
  $sheet->setCellValue('A'.$i, $row['cloneno']);
  $sheet->setCellValue('B'.$i, $row['name']);
  // $sheet->setCellValue('C'.$i, 'upload/'.$row['name']);

  $objDrawing = new drawing();
        $objDrawing->setName('Logo ' . $i);
        $objDrawing->setDescription('Logo ' . $i);
        $objDrawing->setPath($logopath);
        $objDrawing->setResizeProportional(true);
        $objDrawing->setWidth(40);
        $objDrawing->setCoordinates('C' . $i);
        $objDrawing->setWorksheet($sheet);

  $i++;
}
//make an xlsx writer object using above spreadsheet
$writer = new Xlsx($spreadsheet);
//write the file in current directory
$writer->save('hello1.xlsx');
//redirect to the file
echo "<meta http-equiv='refresh' content='0;url=hello world.xlsx'/>";

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.