Add Header Logo In Excel Files In PHP Using PHPSpreadSheet

Posted

in

by

Learn how to add excel header logo in PHP using PHPSpreadSheet.

You can align the header logo to the left side, center, or right side. Make sure you set the correct path to your logo file, otherwise it will not appear at the header even if your codes are correct. Other logo attributes can also be set like the name, and the height.

This article will discuss the basic process of adding an excel header logo in PHP using PHPSpreadSheet. Below is the preview of the goal we will try to achieve.

Requirements:

  • Composer
  • PHP 7.2 or newer

Step 1.

Setup dependencies.

{
    "require": {
        "phpoffice/phpspreadsheet": "^1.3"
    }
}

Step 2.

Install phpspreadsheet.

$ composer install

Step 3.

Create a new PHP file (add-header-logo-in-excel-files.php), and start coding.

Step 4.

Prepare the sheet.

// Autoload dependencies
require 'vendor/autoload.php';

// Import the core class of PhpSpreadsheet
use PhpOffice\PhpSpreadsheet\Spreadsheet;

// Import the Xlsx writer class
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Create a new Spreadsheet object
$spreadsheet = new Spreadsheet();

// Retrieve the current active worksheet
$sheet = $spreadsheet->getActiveSheet();

Step 5.

Prepare the logo.

// Initiate new HeaderFooterDrawing instance
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();

// Set the name of the logo
$drawing->setName('Spreadsheet-Coding.com Logo');

// Set the path of the logo
$drawing->setPath('./spreadsheet-coding-com-logo-white-bg.png');

Step 6.

Add the logo to the header.

// Add the image to the header of the sheet
$sheet->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);

// Set the print header
$sheet->getHeaderFooter()->setOddHeader('&L&G');

Step 7.

Last step.

Write something on the sheet, and create and save the excel file.

// Write something on a cell
$sheet->setCellValue('A5', 'Hello World !');

// Write a new .xlsx file
$writer = new Xlsx($spreadsheet);

// Save the new .xlsx file
$writer->save('add-header-logo-in-excel-files.xlsx');

Complete code.

<?php

// Autoload dependencies
require 'vendor/autoload.php';

// Import the core class of PhpSpreadsheet
use PhpOffice\PhpSpreadsheet\Spreadsheet;

// Import the Xlsx writer class
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Create a new Spreadsheet object
$spreadsheet = new Spreadsheet();

// Retrieve the current active worksheet
$sheet = $spreadsheet->getActiveSheet();

// Initiate new HeaderFooterDrawing instance
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();

// Set the name of the logo
$drawing->setName('Spreadsheet-Coding.com Logo');

// Set the path of the logo
$drawing->setPath('./spreadsheet-coding-com-logo-white-bg.png');

// Add the image to the header of the sheet
$sheet->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);

// Set the print header
$sheet->getHeaderFooter()->setOddHeader('&L&G');

// Write something on a cell
$sheet->setCellValue('A5', 'Hello World !');

// Write a new .xlsx file
$writer = new Xlsx($spreadsheet);

// Save the new .xlsx file
$writer->save('add-header-logo-in-excel-files.xlsx');

Test.

Run the following codes.

$ php add-header-logo-in-excel-files.php

Result.

Open the generated file add-header-logo-in-excel-files.xlsx.

One way to check the header of an excel file is through print preview. Click Control + P and the header should now be visible.

Before.

After.

References:


Posted

in

by

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Tutorials

Web Dev Tutorials