Skip to content
MNB PHPExcelDeveloper Guide
AI

Reading workbooks

Inspect Workbooks and Count Rows

Inspect XLSX package metadata and worksheet dimensions, then stream row counts without building complete workbook arrays.

Level
Intermediate
Reading time
8 min
Package
mnb/mnb-phpexcel
Updated
MnbExcel::inspect()MnbExcel::sheetNames()ReadSession::inspect()ReadSession::countRows()

Inspect the workbook package#

inspect-workbook.php
use Mnb\PHPExcel\MnbExcel;

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

print_r([
    'status' => $inspection['status'],
    'file' => $inspection['file'],
    'size_bytes' => $inspection['size_bytes'],
    'encrypted' => $inspection['encrypted'],
    'sheets' => $inspection['sheets'],
    'warnings' => $inspection['warnings'],
    'errors' => $inspection['errors'],
]);

inspect() validates required XLSX package parts and reads workbook relationships and worksheet metadata. It does not convert worksheet cells into a complete PHP row array.

List worksheets directly#

PHP
$sheetNames = MnbExcel::sheetNames('orders.xlsx');

foreach ($sheetNames as $name) {
    echo $name . PHP_EOL;
}

Use sheetNames() when names are all the application needs. Use inspect() when visibility, dimensions, physical row tags, hidden rows or columns, merge cells, filters, and warnings also matter.

Find metadata for one worksheet#

PHP
$inspection = MnbExcel::inspect('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([
    'state' => $orders['state'],
    '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'],
    'has_merge_cells' => $orders['has_merge_cells'],
    'has_auto_filter' => $orders['has_auto_filter'],
]);

Count normalized data rows#

PHP
$session = MnbExcel::read('orders.xlsx')
    ->sheet('Orders')
    ->withHeaderRow();

$filledRows = $session->countRows([
    'skip_empty_rows' => true,
]);

printf("Orders data rows: %d\n", $filledRows);

countRows() uses the same reader options as rows(). It streams and counts yielded records without collecting the complete result into toArray(). Header handling, empty-row behavior, ranges, limits, and selected columns therefore remain consistent with the normal read path.

Count every worksheet explicitly#

PHP
$counts = [];

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

print_r($counts);