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 an Excel file, and want to display it on an HTML webpage? PHP does not have native functions to read Excel files, but we can use the PHPSpreadSheet library to read Excel files and output them in HTML accordingly.

// composer require phpoffice/phpspreadsheet
require "vendor/autoload.php";
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
foreach ($reader->load("my.xlsx")->getActiveSheet()->getRowIterator() as $row) {
  foreach ($row->getCellIterator() as $cell) {
    echo $cell->getValue();
  }
}

That covers the “quick fix” for reading the rows and cells – Read on for the detailed example!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP DISPLAY EXCEL AS HTML

Let us now get into the example of reading an Excel file in PHP, and outputting it in HTML.

 

TUTORIAL VIDEO

 

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 direct pull from GitHub, but their folder structure is a mystery and I can’t figure out how it works either… So the easier way is to:

  • Download and install a package manager called Composer.
  • Open the command prompt (or terminal), and navigate to your project folder cd YOUR-HTTP-FOLDER.
  • 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

3-excel-html.php
<table><?php
// (A) PHPSPREADSHEET TO LOAD EXCEL FILE
require "vendor/autoload.php";
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("2-demo.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
 
// (B) LOOP THROUGH ROWS OF CURRENT WORKSHEET
foreach ($worksheet->getRowIterator() as $row) {
  // (B1) READ CELLS
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false);
 
  // (B2) 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

4-excel-worksheet.php
<?php
// (A) PHPSPREADSHEET TO LOAD EXCEL FILE
require "vendor/autoload.php";
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("2-demo.xlsx");
 
// (B) LOOP THROUGH ALL WORKSHEETS
for ($i=0; $i<$spreadsheet->getSheetCount(); $i++) {
  // (B1) GET WORKSHEET
  $worksheet = $spreadsheet->getSheet($i);
 
  // (B2) LOOP THROUGH ROWS & CELLS OF WORKSHEET
  foreach ($worksheet->getRowIterator() as $row) {
    $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.

 

 

EXTRAS

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

 

CELLS WITH FORMULA

Take note that PHPSpreadsheet will not evaluate formulas. Just do a “convert formula to calculated values” in Excel, and save it as a separate spreadsheet first.

 

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!

3 thoughts on “3 Steps To Display Excel As HTML In PHP”

  1. hi, i’m harry. Thanks in advance for the tutorial. this is very helpful, but I find it difficult, when there is a column that contains a calculation formula such as SUM, it does not display the result, but displays the formula. How do you deal with this?

    1. Of course, PhpSpreadsheet will not evaluate the formulas. Convert the formulas to values in Excel first, then save as a separate file.

Leave a Comment

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