PHP - Loop Through Excel Document and Print Cells

Updated 2026-09-26: rewrote the example with PhpSpreadsheet.

With PhpSpreadsheet (composer require phpoffice/phpspreadsheet), IOFactory::load picks the reader from the file itself, so this loop works on .xls, .xlsx and .ods files:

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

$spreadsheet = IOFactory::load('example.xlsx');

foreach ($spreadsheet->getWorksheetIterator() as $sheet) {
    echo "\n\n " . $sheet->getTitle() . "\n\n";

    foreach ($sheet->getRowIterator() as $row) {
        foreach ($row->getCellIterator() as $cell) {
            echo $cell->getFormattedValue() . "\t";
        }
        echo "\n";
    }
}