MNB PHPExcelDeveloper Guide
v2.0.5

Basic examples

Basic 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.

9examples in this category
21focused categories
5implementation paths
01
Create a workbook

Create an XLSX workbook from associative PHP rows and use the array keys as the header row.

EasyDirect API
MnbExcel::fromArray()withHeader()save()
create-a-workbook.php
<?php

use Mnb\PHPExcel\MnbExcel;

$rows = [
    ['ID' => 1001, 'Product' => 'Laptop', 'Price' => 1249],
    ['ID' => 1002, 'Product' => 'Monitor', 'Price' => 349],
];

MnbExcel::fromArray($rows)
    ->withHeader()
    ->save('products.xlsx');
02
Format cells (font, color, borders)

Apply header styles, range borders, alignment, fills, and number formats without converting numeric values to text.

EasyDirect API
styleHeader()rangeStyle()currencyColumns()
format-cells-font-color-borders.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->styleHeader([
        'font' => ['bold' => true, 'color' => '#FFFFFF'],
        'fill' => ['color' => '#0E6B4D'],
        'alignment' => ['horizontal' => 'center'],
    ])
    ->rangeStyle('A2:D100', [
        'borders' => ['all' => ['style' => 'thin', 'color' => '#D9E5DE']],
    ])
    ->currencyColumns(['Price'], '$')
    ->save('formatted-products.xlsx');
03
Adjust row height and column width

Set fixed dimensions for predictable layouts and combine them with bounded automatic width calculation.

EasyDirect API
columnWidths()rowHeight()autoWidth()
adjust-row-height-and-column-width.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->columnWidths([
        'A' => 14,
        'B' => 32,
        'C' => 18,
    ])
    ->rowHeight(1, 28)
    ->autoWidth(true, ['min' => 10, 'max' => 42])
    ->save('sized-report.xlsx');
04
Freeze Panes

Keep headers and identifying columns visible while users scroll through a large worksheet.

EasyDirect API
freezePanes()freezeAt()
freeze-panes.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->freezePanes(rows: 1, columns: 2)
    ->save('frozen-panes.xlsx');
05
Sort data

Sort the PHP rows before writing. This produces a workbook whose stored row order already matches the required business order.

EasyPHP preprocessing
usort()MnbExcel::fromArray()
sort-data.php
<?php

use Mnb\PHPExcel\MnbExcel;

usort($rows, static function (array $left, array $right): int {
    return [$left['Category'], -$left['Amount']]
        <=> [$right['Category'], -$right['Amount']];
});

MnbExcel::fromArray($rows)
    ->withHeader()
    ->save('sorted-orders.xlsx');
06
Apply Filters

Add an AutoFilter range and optionally preconfigure allowed values for a column.

EasyDirect API
autoFilterRange()filterValues()
apply-filters.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->autoFilterRange('A1:F500')
    ->filterValues('F', ['Open', 'Pending'])
    ->save('filtered-orders.xlsx');
07
Find & Replace

Apply replacements to selected fields in PHP, then export the transformed rows.

EasyPHP preprocessing
array_map()str_replace()MnbExcel::fromArray()
find-replace.php
<?php

use Mnb\PHPExcel\MnbExcel;

$cleanRows = array_map(static function (array $row): array {
    $row['Region'] = str_replace('North-East', 'Northeast', $row['Region']);
    $row['Notes'] = str_ireplace('n/a', '', $row['Notes']);
    return $row;
}, $rows);

MnbExcel::fromArray($cleanRows)
    ->withHeader()
    ->save('replaced-values.xlsx');
08
Conditional Formatting

Add value rules, color scales, data bars, or icon sets to make exceptions visible in Excel.

EasyDirect API
conditionalCellIs()conditionalColorScale()conditionalDataBar()
conditional-formatting.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->conditionalCellIs('F2:F500', 'greaterThan', 10000, [
        'font' => ['color' => '#9C1C1C', 'bold' => true],
        'fill' => ['color' => '#FDE8E8'],
    ])
    ->conditionalColorScale('E2:E500')
    ->conditionalDataBar('D2:D500', '#36A269')
    ->save('conditional-formatting.xlsx');
09
Data Validation (Dropdown Lists)

Create a validated dropdown list for a worksheet range and provide useful input and error messages.

MediumDirect API
validationList()dataValidation()
data-validation-dropdown-lists.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->validationList('D2:D5000', [
        'Draft',
        'Active',
        'Archived',
    ], [
        'prompt_title' => 'Status',
        'prompt' => 'Choose one status from the list.',
        'error_title' => 'Invalid status',
        'allow_blank' => false,
    ])
    ->save('status-dropdown.xlsx');