mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-11-08 19:07:53 +00:00
9565757138
A follow-on activity to PR #5061. That PR added scrollbars to the dropdown lists when samples were run in a browser. Earlier changes had broken up a number of categories into multiple entries because the dropdown lists were too long and wound up truncated. With the scrollbars, it makes sense to re-consolidate many of those lists, such as ComplexNumbers, which are all essentially the same test as each other. Some break-ups are retained (e.g. Basic/1/2/3/4/5) because they differ substantially from sample to sample; that's kind of an arbitrary decision, but it will have to do for now. I also renamed some of the members so that like members sort together when listing the directory (e.g. all pivot table samples now start with `pvt`). The sample code is mostly unchanged; only the directory and/or member names are changed. The download samples were moved to their own new directory, and now represent a complete set of all the write formats that we permit. Studying their code, an improvement for `Helper\Downloader` became evident. It had been used merely to copy an existing file to be downloaded. However, by changing the first parameter of `download` to be either the name of the file directory (the existing usage) *or* a `Writer` object poised to issue `save`, you can use it to download your spreadsheet without having to worry about the headers which the browser requires. It then becomes generally useful beyond being used in samples: ```php $writer = new WriterType($spreadsheet); $downloader = new Downloader($writer, 'suggestedFileName.xlsx'); $downloader->download(); ```
117 lines
4.9 KiB
PHP
117 lines
4.9 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Color;
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
|
|
// Create new Spreadsheet object
|
|
$helper->log('Create new Spreadsheet object');
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
// Set document properties
|
|
$helper->log('Set document properties');
|
|
$spreadsheet->getProperties()->setCreator('Maarten Balliauw')
|
|
->setLastModifiedBy('Maarten Balliauw')
|
|
->setTitle('Office 2007 XLSX Test Document')
|
|
->setSubject('Office 2007 XLSX Test Document')
|
|
->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
|
|
->setKeywords('office 2007 openxml php')
|
|
->setCategory('Test result file');
|
|
|
|
// Create a first sheet, representing sales data
|
|
$helper->log('Add some data');
|
|
$spreadsheet->setActiveSheetIndex(0);
|
|
$spreadsheet->getActiveSheet()->setCellValue('A1', 'Description')
|
|
->setCellValue('B1', 'Amount');
|
|
|
|
$spreadsheet->getActiveSheet()->setCellValue('A2', 'Paycheck received')
|
|
->setCellValue('B2', 100);
|
|
|
|
$spreadsheet->getActiveSheet()->setCellValue('A3', 'Cup of coffee bought')
|
|
->setCellValue('B3', -1.5);
|
|
|
|
$spreadsheet->getActiveSheet()->setCellValue('A4', 'Cup of coffee bought')
|
|
->setCellValue('B4', -1.5);
|
|
|
|
$spreadsheet->getActiveSheet()->setCellValue('A5', 'Cup of tea bought')
|
|
->setCellValue('B5', -1.2);
|
|
|
|
$spreadsheet->getActiveSheet()->setCellValue('A6', 'Found some money')
|
|
->setCellValue('B6', 8);
|
|
|
|
$spreadsheet->getActiveSheet()->setCellValue('A7', 'Total:')
|
|
->setCellValue('B7', '=SUM(B2:B6)');
|
|
|
|
// Set column widths
|
|
$helper->log('Set column widths');
|
|
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(30);
|
|
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(12);
|
|
|
|
// Add conditional formatting
|
|
$helper->log('Add conditional formatting');
|
|
$conditional1 = new Conditional();
|
|
$conditional1->setConditionType(Conditional::CONDITION_CELLIS)
|
|
->setOperatorType(Conditional::OPERATOR_BETWEEN)
|
|
->addCondition('200')
|
|
->addCondition('400');
|
|
$conditional1->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_YELLOW);
|
|
$conditional1->getStyle()->getFont()->setBold(true);
|
|
$conditional1->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR_INTEGER);
|
|
|
|
$conditional2 = new Conditional();
|
|
$conditional2->setConditionType(Conditional::CONDITION_CELLIS)
|
|
->setOperatorType(Conditional::OPERATOR_LESSTHAN)
|
|
->addCondition('0');
|
|
$conditional2->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_RED);
|
|
$conditional2->getStyle()->getFont()->setItalic(true);
|
|
$conditional2->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR_INTEGER);
|
|
|
|
$conditional3 = new Conditional();
|
|
$conditional3->setConditionType(Conditional::CONDITION_CELLIS)
|
|
->setOperatorType(Conditional::OPERATOR_GREATERTHANOREQUAL)
|
|
->addCondition('0');
|
|
$conditional3->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_GREEN);
|
|
$conditional3->getStyle()->getFont()->setItalic(true);
|
|
$conditional3->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR_INTEGER);
|
|
|
|
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('B2')->getConditionalStyles();
|
|
$conditionalStyles[] = $conditional1;
|
|
$conditionalStyles[] = $conditional2;
|
|
$conditionalStyles[] = $conditional3;
|
|
$spreadsheet->getActiveSheet()->getStyle('B2')->setConditionalStyles($conditionalStyles);
|
|
|
|
// duplicate the conditional styles across a range of cells
|
|
$helper->log('Duplicate the conditional formatting across a range of cells');
|
|
$spreadsheet->getActiveSheet()->duplicateConditionalStyle(
|
|
$spreadsheet->getActiveSheet()->getStyle('B2')->getConditionalStyles(),
|
|
'B3:B7'
|
|
);
|
|
|
|
// Set fonts
|
|
$helper->log('Set fonts');
|
|
$spreadsheet->getActiveSheet()->getStyle('A1:B1')->getFont()->setBold(true);
|
|
//$spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
|
|
$spreadsheet->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true);
|
|
//$spreadsheet->getActiveSheet()->getStyle('B7')->getFont()->setBold(true);
|
|
// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
|
|
$helper->log('Set header/footer');
|
|
$spreadsheet->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
|
|
$spreadsheet->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $spreadsheet->getProperties()->getTitle() . '&RPage &P of &N');
|
|
|
|
// Set page orientation and size
|
|
$helper->log('Set page orientation and size');
|
|
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_PORTRAIT);
|
|
$spreadsheet->getActiveSheet()->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4);
|
|
|
|
// Rename worksheet
|
|
$helper->log('Rename worksheet');
|
|
$spreadsheet->getActiveSheet()->setTitle('Invoice');
|
|
|
|
// Save
|
|
$helper->write($spreadsheet, __FILE__);
|