MNB PHPExcelDeveloper Guide

Pivot examples

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

2examples in this category
21supported categories
5implementation paths
Monolithic library

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

2
examples
2
documented methods
Example 01

Create Pivot Table

addPivotTable()
create-pivot-table.php
<?php

declare(strict_types=1);

/*
Summary:
    Generate a native pivot table from a source worksheet and define row fields and value
    aggregations.

Implementation note:
    Native pivot support covers standard worksheet pivots. OLAP and Data Model pivots require a
    trusted template.
*/

use Mnb\PHPExcel\MnbExcel;

MnbExcel::fromWorkbookArray([
    'Data' => $salesRows,
    'Pivot' => [],
])
    ->withHeader()
    ->addPivotTable(
        name: 'SalesByRegion',
        sourceSheet: 'Data',
        sourceRange: 'A1:D500',
        targetCell: 'A1',
        options: [
            'sheet' => 'Pivot',
            'rows' => ['Region'],
            'values' => [[
                'field' => 'Amount',
                'function' => 'sum',
                'name' => 'Total Sales',
            ]],
            'layout' => 'tabular',
        ]
    )
    ->save('sales-pivot.xlsx');