3 Steps To Display Excel As HTML In PHP

Welcome to a quick tutorial on how to read an Excel file and display it in HTML. So you have a report or log file in the Excel file format, and want to display it on an HTML website? Sadly, that is not possible with PHP out-of-the-box.

PHP does not have native functions to read Excel files. We need to use a library such as PHPSpreadSheet to read Excel files, then output them in HTML accordingly.

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

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Excel To HTML Useful Bits & Links
Tutorial Video 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.

 

STEP 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".

 

STEP 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

 

 

STEP 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.

 

EXTRA) 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

 

TUTORIAL VIDEO

 

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 *