MNB PHPExcelDeveloper Guide
v2.0.5

Reading examples

Reading Excel examples, explained clearly.

Open a focused example to see the implementation path, copy-ready PHP, expected workbook behavior, and the production boundary that matters.

11examples in this category
21focused categories
5implementation paths
01
Read XLSX rows

Read one worksheet into associative PHP rows while skipping empty records and enforcing a safe row limit.

EasyDirect API
MnbExcel::read()sheet()withHeaderRow()toArray()
read-xlsx-rows.php
<?php

use Mnb\PHPExcel\MnbExcel;

$rows = MnbExcel::read(__DIR__ . '/fixtures/students.xlsx')
    ->sheet('Students')
    ->withHeaderRow()
    ->toArray([
        'skip_empty_rows' => true,
        'max_rows' => 5000,
    ]);

foreach ($rows as $row) {
    echo $row['student_id'] . ': ' . $row['name'] . PHP_EOL;
}
02
List and select worksheets

Discover worksheet names first, then select a sheet by name instead of relying on workbook order.

EasyDirect API
MnbExcel::read()sheetNames()sheet()toArray()
list-and-select-worksheets.php
<?php

use Mnb\PHPExcel\MnbExcel;

$workbook = MnbExcel::read(__DIR__ . '/fixtures/sales-workbook.xlsx');
$sheetNames = $workbook->sheetNames();

print_r($sheetNames);

$orders = $workbook
    ->sheet('Orders')
    ->withHeaderRow()
    ->toArray();
03
Detect the header row automatically

Inspect a messy worksheet, report the likely header row and confidence, then read using semantic header detection.

MediumDirect API
detectHeader()autoDetectHeader()headerAtPhysicalRow()toArray()
detect-the-header-row-automatically.php
<?php

use Mnb\PHPExcel\MnbExcel;

$session = MnbExcel::read(__DIR__ . '/fixtures/vendor-export.xlsx')
    ->sheet(1);

$detection = $session->detectHeader([
    'header_detection_rows' => 30,
]);

printf(
    "Header row: %d (confidence %.2f)\n",
    $detection->row,
    $detection->confidence
);

$rows = $session
    ->autoDetectHeader(sampleRows: 30, minimumConfidence: 0.45)
    ->toArray([
        'strict_header_detection' => true,
        'header_case' => 'snake',
    ]);
04
Read selected rows and columns

Limit the source range and project only required worksheet columns before converting rows to PHP arrays.

MediumDirect API
range()projectColumns()withHeaderRow()selectColumns()
read-selected-rows-and-columns.php
<?php

use Mnb\PHPExcel\MnbExcel;

$rows = MnbExcel::read(__DIR__ . '/fixtures/products.xlsx')
    ->sheet('Products')
    ->range(startRow: 1, endRow: 5000, startColumn: 'A', endColumn: 'F')
    ->projectColumns(['A', 'B', 'F'])
    ->withHeaderRow()
    ->toArray();
05
Read cells, formulas, dates, and metadata

Read direct cell values, ranges, calculated formulas, typed cell details, comments, hyperlinks, and worksheet metadata.

AdvancedDirect API
cell()rangeValues()calculatedCell()cellDetails()sheetMetadata()
read-cells-formulas-dates-and-metadata.php
<?php

use Mnb\PHPExcel\MnbExcel;

$sheet = MnbExcel::read(__DIR__ . '/fixtures/finance-report.xlsx')
    ->sheet('Summary');

$title = $sheet->cell('B2');
$monthlyTotals = $sheet->rangeValues('D5:D16');
$calculatedTotal = $sheet->calculatedCell('D17');
$details = $sheet->cellDetails('D17');
$metadata = $sheet->sheetMetadata();

print_r([
    'title' => $title,
    'monthly_totals' => $monthlyTotals,
    'formula' => $details->formula,
    'cached_value' => $details->cachedValue,
    'calculated_value' => $calculatedTotal,
    'comments' => $details->comments,
    'hyperlinks' => $details->hyperlinks,
    'sheet_summary' => $metadata['summary'] ?? [],
]);
06
Inspect an XLSX workbook without materializing rows

Inspect file size, encryption state, worksheet metadata, package warnings, and validation errors without converting worksheet cells into a complete PHP array.

EasyDirect API
MnbExcel::inspect()ReadSession::inspect()
inspect-an-xlsx-workbook-without-materializing-rows.php
<?php

use Mnb\PHPExcel\MnbExcel;

$inspection = MnbExcel::inspect(__DIR__ . '/fixtures/orders.xlsx');

print_r([
    'status' => $inspection['status'],
    'file' => $inspection['file'],
    'size_bytes' => $inspection['size_bytes'],
    'encrypted' => $inspection['encrypted'],
    'sheet_count' => count($inspection['sheets']),
    'warnings' => $inspection['warnings'],
    'errors' => $inspection['errors'],
]);
07
List worksheets and inspect their dimensions

List worksheet names directly, then inspect visibility, dimensions, declared last rows and columns, and physical row-tag counts.

EasyDirect API
MnbExcel::sheetNames()MnbExcel::inspect()
list-worksheets-and-inspect-their-dimensions.php
<?php

use Mnb\PHPExcel\MnbExcel;

$path = __DIR__ . '/fixtures/orders.xlsx';
$names = MnbExcel::sheetNames($path);
$inspection = MnbExcel::inspect($path);

print_r($names);

foreach ($inspection['sheets'] as $sheet) {
    printf(
        "%s: %s, dimension %s, declared row %s, row tags %s\n",
        $sheet['name'],
        $sheet['state'],
        $sheet['dimension'] ?? 'unknown',
        $sheet['declared_last_row'] ?? 'unknown',
        $sheet['row_tag_count'] ?? 'unknown'
    );
}
08
Find inspection metadata for one worksheet

Select one worksheet from the inspection result and read its dimensions, hidden rows and columns, merges, filters, and drawing flags.

MediumDirect API
MnbExcel::inspect()ReadSession::inspect()
find-inspection-metadata-for-one-worksheet.php
<?php

use Mnb\PHPExcel\MnbExcel;

$inspection = MnbExcel::inspect(__DIR__ . '/fixtures/orders.xlsx');
$orders = null;

foreach ($inspection['sheets'] as $sheet) {
    if ($sheet['name'] === 'Orders') {
        $orders = $sheet;
        break;
    }
}

if ($orders === null) {
    throw new RuntimeException('Orders worksheet was not found.');
}

print_r([
    'dimension' => $orders['dimension'],
    'declared_last_row' => $orders['declared_last_row'],
    'declared_last_column' => $orders['declared_last_column'],
    'row_tag_count' => $orders['row_tag_count'],
    'hidden_row_count' => $orders['hidden_row_count'],
    'hidden_column_count' => $orders['hidden_column_count'],
    'has_merge_cells' => $orders['has_merge_cells'],
    'has_auto_filter' => $orders['has_auto_filter'],
    'has_drawing' => $orders['has_drawing'],
]);
09
Count rows without building a complete array

Stream normalized rows through the regular reader and count them without collecting the complete workbook result in memory.

MediumDirect API
ReadSession::countRows()MnbExcel::sheetNames()
count-rows-without-building-a-complete-array.php
<?php

use Mnb\PHPExcel\MnbExcel;

$path = __DIR__ . '/fixtures/orders.xlsx';

$filledRows = MnbExcel::read($path)
    ->sheet('Orders')
    ->withHeaderRow()
    ->countRows(['skip_empty_rows' => true]);

$allSheetCounts = [];
foreach (MnbExcel::sheetNames($path) as $name) {
    $allSheetCounts[$name] = MnbExcel::read($path)
        ->sheet($name)
        ->withHeaderRow()
        ->countRows(['skip_empty_rows' => true]);
}

print_r(compact('filledRows', 'allSheetCounts'));
10
Select an optional sheet or use the active sheet

Accept a nullable worksheet input without hiding non-empty typos, then continue with the workbook active sheet when no value was supplied.

EasyDirect API
ReadSession::sheetOrActive()ReadSession::sheetIfExists()ReadSession::activeSheet()
select-an-optional-sheet-or-use-the-active-sheet.php
<?php


$requestedSheet = trim((string) ($_GET['sheet'] ?? ''));

$session = MnbExcel::read(__DIR__ . '/fixtures/report.xlsx')
    ->sheetOrActive($requestedSheet)
    ->withHeaderRow(1);

foreach ($session->rows() as $row) {
    print_r($row);
}
11
Check for data rows before collecting an array

Evaluate normalized rows after header and empty-row options, then materialize the result only when data exists.

EasyDirect API
ReadSession::isEmpty()ReadSession::hasRows()ReadSession::toArray()
check-for-data-rows-before-collecting-an-array.php
<?php


$session = MnbExcel::read(__DIR__ . '/fixtures/orders.xlsx')
    ->sheet('Orders')
    ->withHeaderRow(1)
    ->withOptions(['skip_empty_rows' => true]);

if ($session->isEmpty()) {
    echo "No data rows.\n";
    return;
}

$rows = $session->toArray();

foreach ($rows as $row) {
    print_r($row);
}