Skip to content
MNB PHPExcelDeveloper Guide
AI

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
Example 02

Open XLSX with reusable reader options

Open related guide
Example 03

Read one cell with the XLSX facade

Open related guide
Example 04

Read an XLSX range directly

Open related guide
Example 05

Read typed cell details and rich text

Open related guide
Example 06

Read worksheet images and protection

Open related guide
Example 07

Check XLSX encryption before reading

Open related guide
Example 08

Inspect an XLSX workbook without rows

Open related guide
Example 09

List and strictly select worksheets

Open related guide
Example 10

Read active worksheet identity

Open related guide
Example 11

Check whether a worksheet exists

Open related guide
Example 12

Select an optional worksheet safely

Open related guide
Example 13

Use a requested sheet or the active sheet

Open related guide
Example 14

Select the active worksheet explicitly

Open related guide
Example 15

Inspect worksheet dimensions and flags

Open related guide
Example 16

Count rows across every worksheet

Open related guide
Example 17

Detect the header row automatically

Open related guide
Example 18

Choose an explicit header strategy

Open related guide
Example 19

Read selected rows and columns

Open related guide
Example 20

Check and require data rows

Open related guide
Example 21

Read first row, row states, and all rows

Open related guide
Example 22

Stream XLSX chunks with progress and row errors

Open related guide
Example 23

Read cells, ranges, and formulas from one session

Open related guide
Example 24

Read cell details, styles, and rich text

Open related guide
Example 25

Read and extract worksheet images

Open related guide
Example 26

Read worksheet metadata and protection

Open related guide
Example 27

Build structured sheet and workbook arrays

Open related guide
Example 28

Convert or save XLSX rows as JSON and XML

Open related guide