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.
01Analyze an XLSX before importingInspect workbook size, rows, columns, risk, server limits, and the recommended normal or streaming import path before processing.
MediumDirect API+
Inspect workbook size, rows, columns, risk, server limits, and the recommended normal or streaming import path before processing.
MnbExcel::analyzeXlsxForImport()MnbExcel::recommendImportMethod()MnbExcel::autoImportPlan()<?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'],
]);02Stream a large XLSX in chunksProcess a large worksheet in bounded-memory chunks and persist each chunk immediately instead of collecting the workbook in one array.
MediumDirect API+
Process a large worksheet in bounded-memory chunks and persist each chunk immediately instead of collecting the workbook in one array.
MnbExcel::largeRead()withHeader()progress()chunk()<?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);03Stream only required columnsReduce parsing and downstream work by projecting worksheet columns while preserving identifiers and converting Excel dates.
MediumDirect API+
Reduce parsing and downstream work by projecting worksheet columns while preserving identifiers and converting Excel dates.
onlyColumns()preserveNumericStrings()convertDates()chunk()<?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);
}
});04Stream a large XLSX exportGenerate a database-sized XLSX from an iterable while reporting progress and avoiding a full in-memory workbook model.
MediumDirect API+
Generate a database-sized XLSX from an iterable while reporting progress and avoiding a full in-memory workbook model.
MnbExcel::largeExport()withHeader()progress()save()<?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);05Import to SQL with progress and resumeStream, validate, batch-insert, record failed rows, checkpoint progress, and resume an interrupted large database import.
AdvancedDirect API+
Stream, validate, batch-insert, record failed rows, checkpoint progress, and resume an interrupted large database import.
MnbExcel::largeImportToSql()MnbExcel::resumeImport()MnbExcel::importStatus()<?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);
}