MNB PHPExcelDeveloper Guide
v2.0.5
XLSX module

XLSX Quick Start

Read associative rows, produce structured arrays, and write a new XLSX file with the focused module API.

Updated

LevelBeginnerReading time8 minPackagemnb/mnb-phpexcel
MnbExcel::read()withHeaderRow()toStructuredArray()Xlsx::write()

What you will learn

  • Read a named worksheet as associative records.
  • Return workbook-aware structured data without writing an output file.
  • Generate a simple XLSX export from associative rows.

Read rows with headers#

read-orders.php
<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Mnb\PHPExcel\MnbExcel;
use Mnb\PHPExcel\Format\Xlsx;

$rows = MnbExcel::read(__DIR__ . '/orders.xlsx')
    ->sheet('Orders')
    ->withHeaderRow(1)
    ->rows();

foreach ($rows as $row) {
    printf("%s: %s\n", $row['Order ID'], $row['Amount']);
}

Return structured workbook output#

structured-response.php
$structured = MnbExcel::read('orders.xlsx')
    ->withHeaderRow(1)
    ->toStructuredArray([
        'include_workbook' => true,
        'include_sheets' => true,
        'preserve_original_row_numbers' => true,
    ]);

header('Content-Type: application/json');
echo json_encode($structured, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

Use toStructuredWorkbookArray() for all sheets or toStructuredSheetArray() for the selected sheet. These are terminal methods returning native PHP arrays; inspect $structured['summary'] and $structured['rows'] rather than calling session methods on the result. JSON and XML helpers are also available through toStructuredJson() and toStructuredXml().

Write a basic workbook#

write-orders.php
use Mnb\PHPExcel\MnbExcel;
use Mnb\PHPExcel\Format\Xlsx;

$rows = [
    ['order_id' => 'SO-1001', 'customer' => 'Acme', 'amount' => 1250.50],
    ['order_id' => 'SO-1002', 'customer' => 'Northwind', 'amount' => 840.00],
];

Xlsx::write($rows, __DIR__ . '/orders-export.xlsx', [
    'sheet_name' => 'Orders',
    'with_header' => true,
]);

Add typed reader options#

PHP
use Mnb\PHPExcel\MnbExcel;
use Mnb\PHPExcel\Format\Xlsx;
use Mnb\PHPExcel\Reader\Options\ReaderOptions;

$options = ReaderOptions::defaults()
    ->withColumns(['A', 'C', 'F'])
    ->withFormulaMode('both');

$rows = MnbExcel::read('finance.xlsx', $options)
    ->sheet('Summary')
    ->streaming()
    ->rows();