Files
PhpSpreadsheet/samples/PivotTable/04_Read_PivotTable.php
T
saifulislamferoz 6afe310f8a Add PivotTable samples
Adds runnable samples under samples/PivotTable demonstrating the pivot table
feature, discovered automatically by the sample browser:

- 01_Create_PivotTable: build a basic pivot (row/column/value fields) from a
  source range and save it as Xlsx.
- 02_PivotTable_Page_Filter: add a page (report filter) field.
- 03_PivotTable_Grouping: group a numeric field into ranges and a date field
  by quarter.
- 04_Read_PivotTable: write a pivot, load it back, and inspect the pivot table
  object model (source, row/column/page fields, value aggregation).

Directory is named PivotTable to match the PascalCase convention used by the
other sample categories (Table, Chart, ...).
2026-08-03 11:52:58 +06:00

91 lines
2.9 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PivotTable\PivotField;
use PhpOffice\PhpSpreadsheet\Worksheet\PivotTable\PivotTableBuilder;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
require __DIR__ . '/../Header.php';
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
// First build a small workbook that contains a pivot table, so this sample is
// self-contained. (In practice you would just load an existing .xlsx file.)
$helper->log('Create a workbook containing a pivot table');
$spreadsheet = new Spreadsheet();
$dataSheet = $spreadsheet->getActiveSheet();
$dataSheet->setTitle('Data');
$dataSheet->fromArray(
[
['Region', 'Product', 'Amount'],
['East', 'Widget', 100],
['West', 'Widget', 150],
['East', 'Gadget', 200],
['West', 'Gadget', 250],
],
null,
'A1'
);
$pivotSheet = $spreadsheet->createSheet();
$pivotSheet->setTitle('PivotTable');
$builder = new PivotTableBuilder($dataSheet, 'A1:C5');
$builder
->addRowField('Region')
->addColumnField('Product')
->addDataField('Amount', PivotField::SUBTOTAL_SUM);
$builder->build($pivotSheet, 'A3', 'SalesByRegion');
$tempFile = $helper->getTemporaryFilename('xlsx');
(new XlsxWriter($spreadsheet))->save($tempFile);
$spreadsheet->disconnectWorksheets();
// Now read the file back and inspect its pivot tables.
$helper->log('Load the workbook and inspect its pivot tables');
$loaded = (new XlsxReader())->load($tempFile);
foreach ($loaded->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getPivotTableCollection() as $pivotTable) {
$helper->log(sprintf(
'Pivot table "%s" on sheet "%s" at %s',
$pivotTable->getName(),
$worksheet->getTitle(),
$pivotTable->getLocation()
));
$cache = $pivotTable->getCacheDefinition();
if ($cache !== null) {
$helper->log(sprintf(
' Source: %s!%s',
(string) $cache->getSourceWorksheet(),
(string) $cache->getSourceRange()
));
}
$helper->log(' Row fields: ' . implode(', ', names($pivotTable->getRowFields())));
$helper->log(' Column fields: ' . implode(', ', names($pivotTable->getColumnFields())));
$helper->log(' Page fields: ' . implode(', ', names($pivotTable->getPageFields())));
foreach ($pivotTable->getDataFields() as $dataField) {
$helper->log(sprintf(
' Value field: %s (%s)',
$dataField->getName(),
(string) $dataField->getSubtotal()
));
}
}
}
$loaded->disconnectWorksheets();
@unlink($tempFile);
/**
* @param PivotField[] $fields
*
* @return string[]
*/
function names(array $fields): array
{
return array_map(static fn (PivotField $field): string => $field->getName(), $fields);
}