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
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#
use Mnb\PHPExcel\MnbExcel;
$path = __DIR__ . '/storage/imports/large-orders.xlsx';
$workbook = MnbExcel::read($path);use Mnb\PHPExcel\Format\Xlsx;
$path = __DIR__ . '/storage/imports/large-orders.xlsx';
$workbook = Xlsx::read($path);$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.
Expected output
Processed 250000 rows; peak memory 38.50 MBThe number is illustrative; benchmark your real workbook, PHP build, extensions, and options.
Do not count and then read unless you need both passes#
Continue the workflow#
STRATEGYChoose a large-file strategy
Choose normal reading, native streaming, chunks, or a large import engine.
→STREAMStreaming guideConfigure forward-only reading, projections, ranges, chunks, and fallbacks.
→DATABASELarge XLSX database importCombine worksheet chunks, PDO batches, resume manifests, and failed-row output.
→Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue