Create Excel Files With Text Links In PHP Using PHPSpreadSheet

| June 21, 2020 | 1,165 views | PhpSpreadsheet

Write texts with links on a new xlsx file, either an external link to a website, or an internal link within another sheet is possible.


  • 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, and start coding.


// 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();

// Set cell A1 with the "Hello Google !" string value
$sheet->setCellValue('A1', 'Hello Google !');

// Add a link to to cell A1

// Set cell A2 with the "Hello A1 !" string value
$sheet->setCellValue('A2', 'Hello A1 !');

// Add a link to cell A1 of the current sheet to cell A2

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

// Save the new .xlsx file


Run the following codes.

$ php create-xlsx-files-with-text-links.php


Open the generated file create-xlsx-files-with-text-links.xlsx.


Change the text links default color, and/or, underline it to make the link more noticeable.



Leave a Reply

Your email address will not be published.