MNB PHPExcelDeveloper Guide
v2.0.5
Getting started

Use the Main Facade and Specialized APIs

Use MnbExcel by default, then move to read sessions, workbook builders, or specialized format classes when needed.

Updated

LevelBeginnerReading time6 minPackagemnb/mnb-phpexcel

What you will learn

  • Start with the MnbExcel facade for normal application code.
  • Use specialized format classes only for format-specific operations.
  • Recognize the objects returned by each API.

Use MnbExcel as the default entry point#

PHP
use Mnb\PHPExcel\MnbExcel;

$session = MnbExcel::read('orders.xlsx');
MnbExcel::fromArray($rows)->save('orders-copy.xlsx');

The direct format class is optional and is included in the same main package.

Use MnbExcel for complete application workflows#

PHP
use Mnb\PHPExcel\MnbExcel;

MnbExcel::importProducts('products.xlsx', $pdo, 'products');
MnbExcel::emailGeneratedExcel($report, $to, $subject);
MnbExcel::runScheduler($scheduler);

The facade is installed by the main package and coordinates all supported formats and workflows.

Use ReadSession for a read pipeline#

PHP
$session = MnbExcel::read('orders.xlsx')
    ->sheet('Orders')
    ->autoDetectHeader()
    ->range(2, null, 'A', 'H')
    ->projectColumns(['Order ID', 'Amount'])
    ->streaming();

foreach ($session->chunks(1000) as $chunk) {
    // Process a consistent batch.
}

Use WorkbookBuilder for output composition#

PHP
$workbook = MnbExcel::fromWorkbookArray([
    'Data' => $rows,
    'Summary' => [],
]);

$workbook
    ->withHeader()
    ->addChart(/* ... */)
    ->encryptWithPassword($password)
    ->save('report.xlsx');