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
Firstly, here is the download link to the example code as promised.
QUICK NOTES
- A copy of PHPSpreadSheet is not included in the zip file – Please download the latest version by yourself.
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.
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 | |
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
<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
<?php
// (A) PHPSPREADSHEET TO LOAD EXCEL FILE
require "vendor/autoload.php";
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("2-demo.xlsx");
// (B) COUNT NUMBER OF WORKSHEETS
$allsheets = $spreadsheet->getSheetCount();
// (C) LOOP THROUGH ALL WORKSHEETS
for ($i=0; $i<$allsheets; $i++) {
// (C1) GET WORKSHEET
$worksheet = $spreadsheet->getSheet($i);
// (C2) LOOP THROUGH ROWS OF CURRENT WORKSHEET
foreach ($worksheet->getRowIterator() as $row) {
// (C3) 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.
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
TUTORIAL VIDEO
INFOGRAPHIC CHEAT SHEET

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!
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?
Of course, PhpSpreadsheet will not evaluate the formulas. Convert the formulas to values in Excel first, then save as a separate file.