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
21supported categories
5implementation paths
Monolithic library
Large files examples with copy-ready, production-minded PHP.
Each example identifies the exact methods it uses and can be opened independently from the right-side index.
- 5
- examples
- 15
- documented methods
MnbExcel::analyzeXlsxForImport()MnbExcel::recommendImportMethod()MnbExcel::autoImportPlan()<?php
declare(strict_types=1);
/*
Summary:
Inspect workbook size, rows, columns, risk, server limits, and the recommended normal or
streaming import path before processing.
Implementation note:
Preflight unknown uploads instead of guessing from file size alone. The recommendation accounts
for workbook dimensions, feature complexity, and the current server profile.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/large-orders.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$plan = MnbExcel::autoImportPlan(
$workbookPath,
[
'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'],
]);MnbExcel::largeRead()withHeader()progress()chunk()<?php
declare(strict_types=1);
/*
Summary:
Process a large worksheet in bounded-memory chunks and persist each chunk immediately instead of
collecting the workbook in one array.
Implementation note:
Use a zero time budget in CLI workers. HTTP imports should stop safely before the server timeout
and continue through a manifest or queue.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/large-orders.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$result = MnbExcel::largeRead($workbookPath)
->sheet('Orders')
->withHeader()
->timeBudgetSeconds(25)
->memoryGuardRatio(0.80)
->progress(static function (array $state): void {
echo 'Rows delivered: ' . ($state['rows_delivered'] ?? 0) . PHP_EOL;
})
->chunk(1_000, 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);onlyColumns()preserveNumericStrings()convertDates()chunk()<?php
declare(strict_types=1);
/*
Summary:
Reduce parsing and downstream work by projecting worksheet columns while preserving identifiers
and converting Excel dates.
Implementation note:
onlyColumns() accepts worksheet letters or numeric indexes in the streaming XLSX reader.
Projection is applied while parsing, not after full row materialization.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/large-payments.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
MnbExcel::largeRead($workbookPath)
->withHeader()
->onlyColumns(['A', 'D', 'G'])
->preserveNumericStrings()
->convertDates(true, 'Y-m-d')
->chunk(2_000, static function (array $rows): void {
foreach ($rows as $row) {
// Only the projected fields are present.
processPaymentRow($row);
}
});MnbExcel::largeExport()withHeader()progress()save()<?php
declare(strict_types=1);
/*
Summary:
Generate a database-sized XLSX from an iterable while reporting progress and avoiding a full
in-memory workbook model.
Implementation note:
The large writer uses a separate streaming engine. Rich charts, pivots, arbitrary styling, and
template object editing belong to normal workbook mode.
*/
use Mnb\PHPExcel\MnbExcel;
function orderRows(): Generator
{
for ($id = 1; $id <= 250_000; $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: 5_000)
->save(__DIR__ . '/output/large-orders.xlsx');
print_r($result);MnbExcel::largeImportToSql()MnbExcel::resumeImport()MnbExcel::importStatus()<?php
declare(strict_types=1);
/*
Summary:
Stream, validate, batch-insert, record failed rows, checkpoint progress, and resume an
interrupted large database import.
Implementation note:
Create database indexes before importing. Resume relies on the manifest and source file
remaining available and unchanged.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/large-payments.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$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(
$workbookPath,
$pdo,
'payments',
[
'with_header' => true,
'chunk_size' => 1_000,
'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);
}No matching example found.Try a method name, task, or result.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue