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.
01Create a workbookCreate an XLSX workbook from associative PHP rows and use the array keys as the header row.
EasyDirect API+
Create an XLSX workbook from associative PHP rows and use the array keys as the header row.
MnbExcel::fromArray()withHeader()save()<?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');02Format cells (font, color, borders)Apply header styles, range borders, alignment, fills, and number formats without converting numeric values to text.
EasyDirect API+
Apply header styles, range borders, alignment, fills, and number formats without converting numeric values to text.
styleHeader()rangeStyle()currencyColumns()<?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');03Adjust row height and column widthSet fixed dimensions for predictable layouts and combine them with bounded automatic width calculation.
EasyDirect API+
Set fixed dimensions for predictable layouts and combine them with bounded automatic width calculation.
columnWidths()rowHeight()autoWidth()<?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');04Freeze PanesKeep headers and identifying columns visible while users scroll through a large worksheet.
EasyDirect API+
Keep headers and identifying columns visible while users scroll through a large worksheet.
freezePanes()freezeAt()<?php
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->freezePanes(rows: 1, columns: 2)
->save('frozen-panes.xlsx');05Sort dataSort the PHP rows before writing. This produces a workbook whose stored row order already matches the required business order.
EasyPHP preprocessing+
Sort the PHP rows before writing. This produces a workbook whose stored row order already matches the required business order.
usort()MnbExcel::fromArray()<?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');06Apply FiltersAdd an AutoFilter range and optionally preconfigure allowed values for a column.
EasyDirect API+
Add an AutoFilter range and optionally preconfigure allowed values for a column.
autoFilterRange()filterValues()<?php
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->autoFilterRange('A1:F500')
->filterValues('F', ['Open', 'Pending'])
->save('filtered-orders.xlsx');07Find & ReplaceApply replacements to selected fields in PHP, then export the transformed rows.
EasyPHP preprocessing+
Apply replacements to selected fields in PHP, then export the transformed rows.
array_map()str_replace()MnbExcel::fromArray()<?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');08Conditional FormattingAdd value rules, color scales, data bars, or icon sets to make exceptions visible in Excel.
EasyDirect API+
Add value rules, color scales, data bars, or icon sets to make exceptions visible in Excel.
conditionalCellIs()conditionalColorScale()conditionalDataBar()<?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');09Data Validation (Dropdown Lists)Create a validated dropdown list for a worksheet range and provide useful input and error messages.
MediumDirect API+
Create a validated dropdown list for a worksheet range and provide useful input and error messages.
validationList()dataValidation()<?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');