MNB PHPExcelDeveloper Guide
v2.0.5
Imports

Import Templates, Preview, and Validation

Guide users toward clean data and report row-level problems before insertion.

Updated

LevelIntermediateReading time12 minPackagemnb/mnb-phpexcel
domainImportTemplate()previewDomainImport()validateImport()duplicateRows()

What you will learn

  • Generate a validated spreadsheet template.
  • Preview and confirm headers before importing.
  • Return actionable row-level validation messages.

Generate a matching domain template#

PHP
MnbExcel::domainImportTemplate('products', [
    'include_examples' => true,
    'include_instructions' => true,
])
    ->save('product-import-template.xlsx');

Preview the uploaded workbook#

PHP
$preview = MnbExcel::previewDomainImport('products', 'products.xlsx', [
    'sheet' => 'Products',
    'limit' => 25,
]);

print_r([
    'headers' => $preview['headers'] ?? [],
    'sample' => $preview['rows'] ?? [],
    'warnings' => $preview['warnings'] ?? [],
]);

Validate custom rows before SQL#

PHP
$validation = MnbExcel::validateArray($rows, [
    'sku' => 'required|unique_in_file',
    'name' => 'required',
    'price' => 'required|numeric|min:0',
    'email' => 'nullable|email',
]);

if (($validation['valid'] ?? false) !== true) {
    print_r($validation['failed_rows'] ?? []);
}

Make errors useful to the uploader#

  • Include the physical row number.
  • Name the source header and canonical field.
  • Explain the invalid value without exposing secrets.
  • Provide a downloadable failed-row workbook or CSV.
  • Keep successful and failed counts visible in the final summary.