MNB PHPExcelDeveloper Guide
Imports

Stream Large XLSX Files into a Database

Use the optional database-XLSX integration package for bounded-memory worksheet chunks, PDO batches, resume manifests, and failed rows.

Updated

LevelAdvancedReading time13 minPackagemnb/mnb-phpexcel
LargeExcelDatabaseImportEngine::importToSql()importWorkbookToSql()MnbExcel::largeImportToSql()

What you will learn

  • Install the optional integration rather than coupling generic Database to XLSX.
  • Tune worksheet chunks and SQL batches independently.
  • Resume safely with idempotent database design.

Before you start

  • Install mnb/mnb-phpexcel:^2.0.
  • Enable the intended PDO driver.
  • Create target indexes and private manifest storage.

Use the direct integration API#

PHP
use Mnb\PHPExcel\Large\LargeExcelDatabaseImportEngine;

$result = (new LargeExcelDatabaseImportEngine())->importToSql(
    'products.xlsx',
    $pdo,
    'products',
    [
        'sheet' => 'Products',
        'with_header' => true,
        'chunk_size' => 2000,
        'batch_size' => 500,
        'duplicate_strategy' => 'update',
        'unique_by' => ['sku'],
        'resume' => true,
        'manifest_path' => __DIR__ . '/storage/products-import.json',
        'failed_rows_csv' => __DIR__ . '/storage/products-failed.csv',
    ]
);

Use the application facade when installed#

PHP
$result = MnbExcel::largeImportToSql(
    'products.xlsx',
    $pdo,
    'products',
    [
        'sheet' => 'Products',
        'chunk_size' => 2000,
        'batch_size' => 500,
        'resume' => true,
    ]
);

MnbExcel belongs to mnb/mnb-phpexcel; it is not available from an isolated XLSX or Database installation.

Keep the generic package independent#