47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\NamedRange;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
error_reporting(E_ALL);
|
|
set_time_limit(0);
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
|
|
require_once __DIR__ . '/../Bootstrap.php';
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->setActiveSheetIndex(0);
|
|
|
|
// Set up some basic data
|
|
$worksheet
|
|
->setCellValue('A1', 'Tax Rate:')
|
|
->setCellValue('B1', '=19%')
|
|
->setCellValue('A3', 'Net Price:')
|
|
->setCellValue('B3', 12.99)
|
|
->setCellValue('A4', 'Tax:')
|
|
->setCellValue('A5', 'Price including Tax:');
|
|
|
|
// Define named ranges
|
|
$spreadsheet->addNamedRange(new NamedRange('TAX_RATE', $worksheet, '=$B$1'));
|
|
$spreadsheet->addNamedRange(new NamedRange('PRICE', $worksheet, '=$B$3'));
|
|
|
|
// Reference that defined name in a formula
|
|
$worksheet
|
|
->setCellValue('B4', '=PRICE*TAX_RATE')
|
|
->setCellValue('B5', '=PRICE*(1+TAX_RATE)');
|
|
|
|
echo sprintf(
|
|
'With a Tax Rate of %.2f and a net price of %.2f, Tax is %.2f and the gross price is %.2f',
|
|
$worksheet->getCell('B1')->getCalculatedValue(),
|
|
$worksheet->getCell('B3')->getValue(),
|
|
$worksheet->getCell('B4')->getCalculatedValue(),
|
|
$worksheet->getCell('B5')->getCalculatedValue()
|
|
), PHP_EOL;
|
|
|
|
$outputFileName = 'SimpleNamedRange.xlsx';
|
|
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
|
$writer->save($outputFileName);
|