MNB PHPExcelDeveloper Guide
v2.0.5

Large files examples

Large files 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.

5examples in this category
21focused categories
5implementation paths
01
Analyze an XLSX before importing

Inspect workbook size, rows, columns, risk, server limits, and the recommended normal or streaming import path before processing.

MediumDirect API
MnbExcel::analyzeXlsxForImport()MnbExcel::recommendImportMethod()MnbExcel::autoImportPlan()
analyze-an-xlsx-before-importing.php
<?php

use Mnb\PHPExcel\MnbExcel;

$plan = MnbExcel::autoImportPlan(
    __DIR__ . '/fixtures/large-orders.xlsx',
    [
        'server' => 'shared',
        'memory_limit' => '256M',
        'max_execution_time' => 30,
    ],
    [
        'accurate_row_count' => true,
        'scan_features' => true,
        'time_budget_seconds' => 20,
    ]
);

print_r([
    'method' => $plan['selected_method'],
    'chunk_size' => $plan['chunk_size'],
    'route' => $plan['route'],
    'rows' => $plan['profile']['total_rows'],
    'risk' => $plan['profile']['risk'],
]);
02
Stream a large XLSX in chunks

Process a large worksheet in bounded-memory chunks and persist each chunk immediately instead of collecting the workbook in one array.

MediumDirect API
MnbExcel::largeRead()withHeader()progress()chunk()
stream-a-large-xlsx-in-chunks.php
<?php

use Mnb\PHPExcel\MnbExcel;

$result = MnbExcel::largeRead(__DIR__ . '/fixtures/large-orders.xlsx')
    ->sheet('Orders')
    ->withHeader()
    ->timeBudgetSeconds(25)
    ->memoryGuardRatio(0.80)
    ->progress(static function (array $state): void {
        echo 'Rows delivered: ' . ($state['rows_delivered'] ?? 0) . PHP_EOL;
    })
    ->chunk(1000, static function (array $rows, array $state): void {
        // Validate and insert this chunk here.
        printf("Chunk %d contains %d rows\n", $state['chunks_delivered'], count($rows));
    });

print_r($result);
03
Stream only required columns

Reduce parsing and downstream work by projecting worksheet columns while preserving identifiers and converting Excel dates.

MediumDirect API
onlyColumns()preserveNumericStrings()convertDates()chunk()
stream-only-required-columns.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::largeRead(__DIR__ . '/fixtures/large-payments.xlsx')
    ->withHeader()
    ->onlyColumns(['A', 'D', 'G'])
    ->preserveNumericStrings()
    ->convertDates(true, 'Y-m-d')
    ->chunk(2000, static function (array $rows): void {
        foreach ($rows as $row) {
            // Only the projected fields are present.
            processPaymentRow($row);
        }
    });
04
Stream a large XLSX export

Generate a database-sized XLSX from an iterable while reporting progress and avoiding a full in-memory workbook model.

MediumDirect API
MnbExcel::largeExport()withHeader()progress()save()
stream-a-large-xlsx-export.php
<?php

use Mnb\PHPExcel\MnbExcel;

function orderRows(): Generator
{
    for ($id = 1; $id <= 250000; $id++) {
        yield [
            'order_id' => $id,
            'status' => $id % 2 === 0 ? 'paid' : 'pending',
            'amount' => round($id * 1.25, 2),
        ];
    }
}

$result = MnbExcel::largeExport(orderRows())
    ->sheetName('Orders')
    ->withHeader()
    ->freezeHeader()
    ->autoFilter()
    ->progress(static function (array $state): void {
        echo 'Rows written: ' . ($state['rows_exported'] ?? 0) . PHP_EOL;
    }, everyRows: 5000)
    ->save(__DIR__ . '/output/large-orders.xlsx');

print_r($result);
05
Import to SQL with progress and resume

Stream, validate, batch-insert, record failed rows, checkpoint progress, and resume an interrupted large database import.

AdvancedDirect API
MnbExcel::largeImportToSql()MnbExcel::resumeImport()MnbExcel::importStatus()
import-to-sql-with-progress-and-resume.php
<?php

use Mnb\PHPExcel\MnbExcel;

$pdo = new PDO('sqlite:' . __DIR__ . '/storage/imports.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('CREATE TABLE IF NOT EXISTS payments (email TEXT, amount REAL)');

$manifest = __DIR__ . '/storage/payment-import.json';

$result = MnbExcel::largeImportToSql(
    __DIR__ . '/fixtures/large-payments.xlsx',
    $pdo,
    'payments',
    [
        'with_header' => true,
        'chunk_size' => 1000,
        'batch_size' => 250,
        'manifest_path' => $manifest,
        'failed_rows_csv' => __DIR__ . '/storage/payment-errors.csv',
        'resume' => true,
        'time_budget_seconds' => 25,
        'rules' => [
            'email' => 'required|email',
            'amount' => 'required|numeric|min:0',
        ],
        'progress' => static function (array $state): void {
            echo 'Rows scanned: ' . ($state['rows_scanned'] ?? 0) . PHP_EOL;
        },
    ]
);

if (($result['status'] ?? '') === 'paused') {
    $result = MnbExcel::resumeImport($manifest, $pdo);
}