MNB PHPExcelDeveloper Guide

XLSX · Reading

XLSX reading examples.

Package-local, copy-ready examples organized like the classes and methods reference. Open one example at a time, search by method, and jump from the right-side index.

Packagemnb/mnb-phpexcel-xlsx:^2.0Reference28 examples · 78 methods
XLSX individual library

Reading examples with copy-ready, production-minded PHP.

Every snippet uses mnb/mnb-phpexcel-xlsx or its declared Core dependency. Each copied snippet includes strict typing, a concise summary, implementation guidance, readable limits, and a safe fixture check.

28
examples
78
documented methods
Example 01

Read XLSX rows with headers

Open related guide
Xlsx::read()sheet()withHeaderRow()toArray()
read-xlsx-rows-with-headers.php
<?php

declare(strict_types=1);

/*
Summary:
    Open an XLSX workbook, select a worksheet, map row one to associative keys, and collect bounded
    rows.

Implementation note:
    Use toArray() for small and normal worksheets. For an unknown or very large file, inspect or
    stream before collecting every row.
*/

use Mnb\PHPExcel\Format\Xlsx;

$workbookPath = __DIR__ . '/fixtures/students.xlsx';

if (!is_file($workbookPath)) {
    throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}

$rows = Xlsx::read($workbookPath)
    ->sheet('Students')
    ->withHeaderRow(1)
    ->toArray([
        'skip_empty_rows' => true,
        'max_rows' => 5_000,
    ]);

foreach ($rows as $row) {
    $studentId = trim((string) ($row['student_id'] ?? ''));
    $name = trim((string) ($row['name'] ?? ''));

    if ($studentId === '' && $name === '') {
        continue;
    }

    printf('%s: %s%s', $studentId, $name, PHP_EOL);
}
Expected output
S001: Asha Patel
S002: Daniel Wong