Imports examples
Imports 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.
6examples in this category
21supported categories
5implementation paths
Monolithic library
Imports 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.
- 6
- examples
- 14
- documented methods
MnbExcel::previewImport()ReadSession::previewImport()<?php
declare(strict_types=1);
/*
Summary:
Inspect missing columns, unexpected columns, duplicate candidates, empty values, and sample rows
before validation or database writes.
Implementation note:
ReadSession::previewImport() combines the read and preview steps when the source file is already
represented by a read session.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/students.csv';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$rows = MnbExcel::read($workbookPath)
->withHeaderRow()
->toArray();
$preview = MnbExcel::previewImport($rows, [
'required_columns' => ['student_id', 'name', 'email'],
'allowed_columns' => ['student_id', 'name', 'email', 'phone'],
'strict_columns' => true,
'duplicate_by' => ['student_id'],
]);
print_r([
'status' => $preview['status'],
'total_rows' => $preview['total_rows'],
'total_columns' => $preview['total_columns'],
'warnings' => $preview['warnings'],
]);MnbExcel::suggestColumnMap()<?php
declare(strict_types=1);
/*
Summary:
Build an import-screen mapping proposal from source headings, canonical database fields, and
known aliases with confidence scores.
Implementation note:
Treat the suggestion as a UI default, not an irreversible decision. Show low-confidence matches
to the user for confirmation.
*/
use Mnb\PHPExcel\MnbExcel;
$map = MnbExcel::suggestColumnMap(
['Student Number', 'Full Name', 'Email Address', 'Mobile No'],
['student_id', 'name', 'email', 'phone'],
[
'student_id' => ['student number', 'registration no'],
'email' => ['email address', 'mail'],
'phone' => ['mobile', 'mobile no', 'phone number'],
],
minConfidence: 0.60
);
print_r($map);MnbExcel::validateImport()MnbExcel::fromFailedRows()freezeHeader()autoFilter()<?php
declare(strict_types=1);
/*
Summary:
Validate imported rows with reusable rules and create a filterable XLSX containing source row
numbers and human-readable errors.
Implementation note:
Only valid rows should continue to database insertion. The failed-row workbook can be returned
to the user for correction and re-upload.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/students.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$rows = MnbExcel::read($workbookPath)
->withHeaderRow()
->toArray([
'preserve_original_row_numbers' => true,
]);
$result = MnbExcel::validateImport($rows, [
'student_id' => 'required|string|max:30',
'name' => 'required|string|max:100',
'email' => 'required|email|unique_in_file',
'marks' => 'nullable|numeric|min:0|max:100',
], [
'row_number_key' => '_mnb_original_row_number',
]);
MnbExcel::fromFailedRows($result['failed'])
->freezeHeader()
->autoFilter()
->autoWidth(['min' => 12, 'max' => 48])
->save(__DIR__ . '/output/failed-students.xlsx');MnbExcel::duplicateRows()ReadSession::duplicateRows()<?php
declare(strict_types=1);
/*
Summary:
Find repeated records using one or more business-key columns and report the source row positions
involved.
Implementation note:
Use composite columns such as ["sku", "warehouse"] when uniqueness depends on more than one
field.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/contacts.csv';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$rows = MnbExcel::read($workbookPath)
->withHeaderRow()
->toArray([
'preserve_original_row_numbers' => true,
]);
$duplicates = MnbExcel::duplicateRows($rows, ['email']);
print_r($duplicates);MnbExcel::previewDomainImport()MnbExcel::importProducts()<?php
declare(strict_types=1);
/*
Summary:
Use the built-in product schema to map aliases, normalize values, validate rows, and update
existing records by SKU.
Implementation note:
The target table and a matching unique index must already exist. Domain presets handle
spreadsheet normalization, not application-specific inventory or pricing side effects.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/products.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$pdo = new PDO('sqlite:' . __DIR__ . '/storage/catalog.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec(
'CREATE TABLE IF NOT EXISTS products (' .
'sku TEXT PRIMARY KEY, name TEXT, price REAL, quantity INTEGER, image_url TEXT' .
')'
);
$preview = MnbExcel::previewDomainImport(
'products',
$workbookPath,
['sheet' => 'Products', 'limit' => 25]
);
if (($preview['failed_rows'] ?? 0) === 0) {
$result = MnbExcel::importProducts(
$workbookPath,
$pdo,
'products',
[
'duplicate_strategy' => 'update',
'unique_by' => ['sku'],
'batch_size' => 500,
'failed_rows_csv' => __DIR__ . '/output/failed-products.csv',
]
);
print_r($result);
}MnbExcel::domainImportTemplate()freezeHeader()autoWidth()save()<?php
declare(strict_types=1);
/*
Summary:
Create a product import workbook from the same canonical field metadata used by preview and
import validation.
Implementation note:
Generated templates include headers, examples, required indicators, descriptions, comments, and
supported validation lists from the domain preset.
*/
use Mnb\PHPExcel\MnbExcel;
MnbExcel::domainImportTemplate('products', [
'title' => 'Product Catalog Import',
'instructions' => 'Keep the SKU unique. Price and quantity must be numeric.',
'sample_rows' => 2,
])
->freezeHeader()
->autoWidth(['min' => 12, 'max' => 42])
->save(__DIR__ . '/output/product-import-template.xlsx');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