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
addPivotTable()<?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');addPivotTable()addChart()<?php
declare(strict_types=1);
/*
Summary:
Create a pivot summary and then add a chart that points to the pivot output range.
Implementation note:
Confirm the expected pivot output range when row-field cardinality can change.
*/
use Mnb\PHPExcel\MnbExcel;
MnbExcel::fromWorkbookArray([
'Data' => $salesRows,
'Pivot' => [],
])
->withHeader()
->addPivotTable('SalesPivot', 'Data', 'A1:D500', 'A1', [
'sheet' => 'Pivot',
'rows' => ['Region'],
'values' => [['field' => 'Amount', 'function' => 'sum']],
])
->addChart('column', 'Sales by Region', [[
'name' => 'Sales',
'categories' => 'Pivot!$A$2:$A$8',
'values' => 'Pivot!$B$2:$B$8',
]], [
'sheet' => 'Pivot',
'from' => 'D2',
'to' => 'L18',
])
->save('pivot-chart.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