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
21supported categories
5implementation paths
Monolithic library
Basic 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.
- 9
- examples
- 21
- documented methods
MnbExcel::fromArray()withHeader()save()<?php
declare(strict_types=1);
/*
Summary:
Create an XLSX workbook from associative PHP rows and use the array keys as the header row.
Implementation note:
The output contains one worksheet named Sheet1. Use fromWorkbookArray() when several worksheets
are required.
*/
use Mnb\PHPExcel\MnbExcel;
$rows = [
['ID' => 1_001, 'Product' => 'Laptop', 'Price' => 1_249],
['ID' => 1_002, 'Product' => 'Monitor', 'Price' => 349],
];
MnbExcel::fromArray($rows)
->withHeader()
->save('products.xlsx');styleHeader()rangeStyle()currencyColumns()<?php
declare(strict_types=1);
/*
Summary:
Apply header styles, range borders, alignment, fills, and number formats without converting
numeric values to text.
Implementation note:
Named styles are useful when the same visual treatment is applied in several places.
*/
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');columnWidths()rowHeight()autoWidth()<?php
declare(strict_types=1);
/*
Summary:
Set fixed dimensions for predictable layouts and combine them with bounded automatic width
calculation.
Implementation note:
For very large exports, fixed widths are cheaper than scanning every value for auto-width.
*/
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');freezePanes()freezeAt()<?php
declare(strict_types=1);
/*
Summary:
Keep headers and identifying columns visible while users scroll through a large worksheet.
Implementation note:
This freezes the first row and the first two columns. Use freezeAt("C2") for an explicit
top-left scroll cell.
*/
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->freezePanes(rows: 1, columns: 2)
->save('frozen-panes.xlsx');usort()MnbExcel::fromArray()<?php
declare(strict_types=1);
/*
Summary:
Sort the PHP rows before writing. This produces a workbook whose stored row order already
matches the required business order.
Implementation note:
Sorting source rows is deterministic and works in Excel, LibreOffice, and server-side readers
without relying on a UI action.
*/
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');autoFilterRange()filterValues()<?php
declare(strict_types=1);
/*
Summary:
Add an AutoFilter range and optionally preconfigure allowed values for a column.
Implementation note:
Excel opens with filter controls on the header row. Filter criteria are metadata; rows remain
present in the file.
*/
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->autoFilterRange('A1:F500')
->filterValues('F', ['Open', 'Pending'])
->save('filtered-orders.xlsx');array_map()str_replace()MnbExcel::fromArray()<?php
declare(strict_types=1);
/*
Summary:
Apply replacements to selected fields in PHP, then export the transformed rows.
Implementation note:
Limit replacements to known columns. A blind replacement across every cell can alter identifiers
and formulas unexpectedly.
*/
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');conditionalCellIs()conditionalColorScale()conditionalDataBar()<?php
declare(strict_types=1);
/*
Summary:
Add value rules, color scales, data bars, or icon sets to make exceptions visible in Excel.
Implementation note:
Conditional formatting is evaluated by the spreadsheet application when the workbook opens.
*/
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->conditionalCellIs('F2:F500', 'greaterThan', 10_000, [
'font' => ['color' => '#9C1C1C', 'bold' => true],
'fill' => ['color' => '#FDE8E8'],
])
->conditionalColorScale('E2:E500')
->conditionalDataBar('D2:D500', '#36A269')
->save('conditional-formatting.xlsx');validationList()dataValidation()<?php
declare(strict_types=1);
/*
Summary:
Create a validated dropdown list for a worksheet range and provide useful input and error
messages.
Implementation note:
For long lists, place the allowed values on a hidden template sheet and use a range-based
validation rule.
*/
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');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