Skip to content
MNB PHPExcelDeveloper Guide
AI

Writing workbooks

Styles, Number Formats, and Layout

Apply reusable styles, column formats, widths, row heights, merged cells, filters, and frozen panes.

Level
Intermediate
Reading time
13 min
Package
mnb/mnb-phpexcel
Updated
Full packageNative XLSXStyles
namedStyle()styleHeader()rangeStyle()currencyColumns()autoWidth()freezePanes()autoFilterRange()

Define and reuse named styles#

PHP
$workbook
    ->namedStyle('title', [
        'font' => ['bold' => true, 'size' => 18, 'color' => '#FFFFFF'],
        'fill' => ['color' => '#146C5B'],
        'alignment' => ['horizontal' => 'center'],
    ])
    ->title('Quarterly Sales', ['style' => 'title']);

Style scopes intentionally#

PHP
$workbook
    ->styleHeader([
        'font' => ['bold' => true, 'color' => '#FFFFFF'],
        'fill' => ['color' => '#0E5144'],
    ])
    ->columnStyle('A', ['font' => ['bold' => true]])
    ->rowStyle(5, ['fill' => ['color' => '#FFF4E8']])
    ->cellStyle('D12', ['font' => ['color' => '#B42318']])
    ->rangeStyle('A2:F100', ['borders' => ['all' => ['style' => 'thin']]]);

Apply number formats by semantic column#

PHP
$workbook
    ->currencyColumns(['Amount'], '$')
    ->percentageColumns(['Margin'])
    ->integerColumns(['Quantity'])
    ->decimalColumns(['Weight'], 3)
    ->dateStyleColumns(['Order Date'], 'yyyy-mm-dd')
    ->datetimeStyleColumns(['Created At'], 'yyyy-mm-dd hh:mm:ss');

Improve navigation and scanning#

PHP
$workbook
    ->freezePanes(rows: 1, columns: 2)
    ->autoFilterRange('A1:H5000')
    ->columnWidths(['A' => 16, 'B' => 28])
    ->rowHeight(1, 30)
    ->autoWidth(true, ['min' => 10, 'max' => 42]);