MNB PHPExcelDeveloper Guide
Large files

Read Large XLSX Files in PHP with Streaming

Read large XLSX files in PHP with bounded-memory streaming, chunk callbacks, column projection, progress reporting and resumable processing.

Updated

Full packageIndividual XLSXStreamingLow memory
LevelAdvancedReading time13 minPackagemnb/mnb-phpexcel
MnbExcel::largeRead()LargeXlsxReadSession::chunk()onlyColumns()transform()memoryGuardRatio()

What you will learn

  • Configure a large-read session explicitly.
  • Process batches without retaining previous rows.
  • Apply source projection and transformations before callbacks.

Configure the large reader#

PHP
$session = MnbExcel::largeRead('orders.xlsx')
    ->sheet('Orders')
    ->withHeader()
    ->onlyColumns(['Order ID', 'Customer', 'Amount'])
    ->preserveNumericStrings()
    ->convertDates(true, 'Y-m-d')
    ->memoryGuardRatio(0.80)
    ->timeBudgetSeconds(900)
    ->progress(function (array $state): void {
        printf("Rows: %d\n", $state['rows_read'] ?? 0);
    });

Process one batch at a time#

PHP
$result = $session->chunk(1000, function (array $rows, array $state): void {
    foreach ($rows as $row) {
        // Validate or transform.
    }

    // Insert this batch and release it before the next callback.
});

Add reusable transformations#

PHP
$session
    ->transform(function (array $row): array {
        $row['Amount'] = (float) $row['Amount'];
        $row['Customer'] = trim((string) $row['Customer']);
        return $row;
    })
    ->chunk(1000, $callback);

Use native extensions for the strongest guarantee#