MNB PHPExcelDeveloper Guide
Use cases

Import Excel into a Database

Validate an uploaded workbook, inspect and select its worksheet, confirm headers, preview rows, and import records through PDO with clear failure handling.

Updated

PHP 8.1+Full packagePDOValidation
LevelIntermediateReading time16 minPackagemnb/mnb-phpexcel
MnbExcel::validateUpload()MnbExcel::read()ReadSession::dryRunImportToSql()ReadSession::importToSql()

What you will learn

  • Reject unsafe or unsupported uploads before parsing.
  • Verify the worksheet and required columns before database changes.
  • Preview and then execute an import with a stable business key.

Use the production-safe recipe#

The complete implementation is maintained on a dedicated page because upload security, private storage, workbook validation, header checks, dry runs, transactions, and public error responses must stay together.

Open the production-safe upload/import recipe

Import workflow at a glance#

  1. Validate the HTTP upload

    Allow only expected extensions, enforce file and package limits, and reject invalid packages.

  2. Move to private storage

    Generate the storage name in the application and keep it outside the public web root.

  3. Inspect and select the worksheet

    List sheets, select the required sheet, and reject unexpected layouts.

  4. Confirm required headers

    Read one normalized row and compare its keys with the import contract.

  5. Dry-run the database mapping

    Review inserts, updates, invalid rows, duplicate behavior, and totals before changing data.

  6. Import transactionally

    Use batches, unique indexes, safe duplicate strategy, and server-side error logs.

Developer contract#

Upload validation result
array with a valid flag and validation context.
Dry-run result
array describing the planned import without changing rows.
Import result
array summary for inserted, updated, skipped, and failed rows.
Database boundary
PDO connection with exceptions enabled and a unique index for business keys.
Public response
A safe user message; full debug context remains in server logs.
Errors
MnbExcelException, upload validation errors, PDO exceptions, or application contract errors.
Returnsarray import summaryShapeDatabase import result summary
Expected output
Array
(
    [inserted] => 1720
    [updated] => 85
    [skipped] => 2
    [failed] => 4
)

Exact result keys depend on the selected importer options and duplicate strategy.

Continue the workflow#