MNB PHPExcelDeveloper Guide
Use cases

Process Large Excel Files

Read large XLSX workbooks with forward-only row streaming, selected columns, bounded chunks, progress reporting, and controlled memory use.

Updated

PHP 8.1+Full packageIndividual XLSXLow memory
LevelIntermediateReading time13 minPackagemnb/mnb-phpexcel or mnb/mnb-phpexcel-xlsx
ReadSession::rows()ReadSession::selectColumns()ReadSession::chunk()MnbExcel::largeRead()

What you will learn

  • Avoid collecting a large workbook into one PHP array.
  • Project only the columns required by the application.
  • Process bounded chunks and measure the real memory profile.

Prefer streaming for large input#

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

$path = __DIR__ . '/storage/imports/large-orders.xlsx';
$workbook = MnbExcel::read($path);
process-large-orders.php
$session = ($workbook->sheetIfExists('Orders') ?? $workbook->activeSheet())
    ->withHeaderRow(1)
    ->selectColumns(['Order ID', 'Customer', 'Amount'])
    ->withOptions([
        'skip_empty_rows' => true,
        'max_rows' => 1_000_000,
    ]);

$processed = 0;
$batch = [];

foreach ($session->rows() as $row) {
    $batch[] = $row;
    $processed++;

    if (count($batch) === 1000) {
        processBatch($batch);
        $batch = [];
    }
}

if ($batch !== []) {
    processBatch($batch);
}

printf(
    "Processed %d rows; peak memory %.2f MB\n",
    $processed,
    memory_get_peak_usage(true) / 1048576
);

Developer contract#

Returns from rows()
Generator
Data retained by example
At most one 1,000-row application batch plus reader state.
Column behavior
Only selected columns are normalized and passed to the application workflow.
Progress
Track processed rows; calculate a percentage only when a trustworthy total is available.
Memory measurement
memory_get_peak_usage(true) in a dedicated PHP process.
Production limit
Set file-size, ZIP, worksheet, row, string, time, and application batch limits.
ReturnsGenerator rows + application batchesShapeOne progress line after processing
Expected output
Processed 250000 rows; peak memory 38.50 MB

The number is illustrative; benchmark your real workbook, PHP build, extensions, and options.

Do not count and then read unless you need both passes#

Understand fluent and terminal methods →

Continue the workflow#