Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 15, 2023 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/88cfb3c5236611bddc29249018721297 to your computer and use it in GitHub Desktop.
Save code-boxx/88cfb3c5236611bddc29249018721297 to your computer and use it in GitHub Desktop.
PHP Show Excel File In HTML Table

PHP DISPLAY EXCEL IN HTML TABLE

NOTES

  1. A copy of PHPSpreadsheet is not included, download your own - composer require phpoffice/phpspreadsheet
  2. GIST does not allow Excel files. Convert 2-demo.csv to 2-demo.xlsx on your own, or generate your own dummy data.
  3. Access 3-excel-html.php in the browser for the example.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1) Download and install Composer - https://getcomposer.org/
2) Open command line, navigate to your project folder - cd YOUR-HTTP-FOLDER
3) Run "composer require phpoffice/phpspreadsheet"
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
/* COSMETICS - NOT IMPORTANT */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
table { border-collapse: collapse; }
table tr { background: #f2f2f2; }
table tr:nth-child(even) { background: #fff; }
td {
padding: 15px;
border: 1px solid #e5e5e5;
}
<!DOCTYPE html>
<html>
<head>
<title>Excel To HTML Using PHPSpreadSheet</title>
<meta charset="utf-8">
<link rel="stylesheet" href="3-excel-html.css">
</head>
<body>
<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>
</body>
</html>
<?php
// (A) PHPSPREADSHEET TO LOAD EXCEL FILE
require "vendor/autoload.php";
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("x.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>"; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment