Display Excel As HTML In PHP – A Simple Example

Welcome to a quick tutorial on how to read an Excel file and display it in HTML. Have a report or log file in the Excel file format? Want to display it on an HTML website?

PHP cannot read Excel files natively. We need to use a library such as PHPSpreadSheet to read and output Excel files in HTML.

Yes, and it is actually not that difficult – Read on for an example!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Excel To HTML Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

  • A copy of PHPSpreadSheet is not included in the zip file – Please download the latest version by yourself.

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

EXCEL TO HTML

All right, let us now get into the example of outputting an Excel file in HTML.

 

1) DOWNLOAD PHPSPREADSHEET

As in the introduction above, PHP does not support the Excel file format natively. We need to use the PHPSpreadSheet library to open and read Excel files. You can do a git pull, but I can’t figure out their folder structure… The easier way is to:

  • Download and install a package manager called Composer.
  • Open the command prompt (or terminal).
  • Navigate to your project folder and run composer require phpoffice/phpspreadsheet.

That’s all, Composer will automatically download the latest version of PHPSpreadSheet into the vendor/ folder, and we can easily use it with require "vendor/autoload.php".

 

2) DUMMY EXCEL FILE

For this example, we will use the following simple dummy Excel:

Name Email
Joa Doe joa@doe.com
Job Doe job@doe.com
Joe Doe joe@doe.com
Jon Doe jon@doe.com
Joy Doe joy@doe.com

 

 

3) READ EXCEL & OUTPUT HTML

excel-html.php
<table><?php
  // (A) PHPSPREADSHEET TO LOAD EXCEL FILE
  require "vendor/autoload.php";
  $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  $spreadsheet = $reader->load("demo.xlsx");
  $worksheet = $spreadsheet->getActiveSheet();
  foreach ($worksheet->getRowIterator() as $row) {
 
  // (B) READ CELLS
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false);
 
  // (C) OUTPUT HTML
  echo "<tr>";
  foreach ($cellIterator as $cell) { echo "<td>". $cell->getValue() ."</td>"; }
  echo "</tr>";
}
?></table>

Lastly, all that’s left is to use the library to read the Excel file and output it in HTML.

 

4) READING MULTIPLE WORKSHEETS

excel-worksheet.php
<?php
// (A) PHPSPREADSHEET TO LOAD EXCEL FILE
require "vendor/autoload.php";
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("demo.xlsx");
 
// (B) COUNT NUMBER OF WORKSHEETS
$allsheets = $spreadsheet->getSheetCount();
 
// (C) LOOP THROUGH ALL WORKSHEETS
for ($i=0; $i<$allsheets; $i++) {
  $worksheet = $spreadsheet->getSheet($i);
  foreach ($worksheet->getRowIterator() as $row) {
    // (D) READ CELLS
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);
    foreach ($cellIterator as $cell) { echo $cell->getValue() ."<br>"; }
  }
}

Lastly, this is kind of an extra – Yes, we can also loop through multiple worksheets. Read their documentation if you want to learn more. Links below.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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