| 1 |
<?php |
| 2 |
|
| 3 |
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 4 |
use PhpOffice\PhpSpreadsheet\Style\Color; |
| 5 |
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
| 6 |
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
| 7 |
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
| 8 |
|
| 9 |
require __DIR__ . '/../Header.php'; |
| 10 |
|
| 11 |
// Create new Spreadsheet object |
| 12 |
$helper->log('Create new Spreadsheet object'); |
| 13 |
$spreadsheet = new Spreadsheet(); |
| 14 |
|
| 15 |
// Set document properties |
| 16 |
$helper->log('Set document properties'); |
| 17 |
$spreadsheet->getProperties()->setCreator('Maarten Balliauw') |
| 18 |
->setLastModifiedBy('Maarten Balliauw') |
| 19 |
->setTitle('Office 2007 XLSX Test Document') |
| 20 |
->setSubject('Office 2007 XLSX Test Document') |
| 21 |
->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.') |
| 22 |
->setKeywords('office 2007 openxml php') |
| 23 |
->setCategory('Test result file'); |
| 24 |
|
| 25 |
// Create a first sheet, representing sales data |
| 26 |
$helper->log('Add some data'); |
| 27 |
$spreadsheet->setActiveSheetIndex(0); |
| 28 |
$spreadsheet->getActiveSheet()->setCellValue('A1', 'Description') |
| 29 |
->setCellValue('B1', 'Amount'); |
| 30 |
|
| 31 |
$spreadsheet->getActiveSheet()->setCellValue('A2', 'Paycheck received') |
| 32 |
->setCellValue('B2', 100); |
| 33 |
|
| 34 |
$spreadsheet->getActiveSheet()->setCellValue('A3', 'Cup of coffee bought') |
| 35 |
->setCellValue('B3', -1.5); |
| 36 |
|
| 37 |
$spreadsheet->getActiveSheet()->setCellValue('A4', 'Cup of coffee bought') |
| 38 |
->setCellValue('B4', -1.5); |
| 39 |
|
| 40 |
$spreadsheet->getActiveSheet()->setCellValue('A5', 'Cup of tea bought') |
| 41 |
->setCellValue('B5', -1.2); |
| 42 |
|
| 43 |
$spreadsheet->getActiveSheet()->setCellValue('A6', 'Found some money') |
| 44 |
->setCellValue('B6', 8); |
| 45 |
|
| 46 |
$spreadsheet->getActiveSheet()->setCellValue('A7', 'Total:') |
| 47 |
->setCellValue('B7', '=SUM(B2:B6)'); |
| 48 |
|
| 49 |
// Set column widths |
| 50 |
$helper->log('Set column widths'); |
| 51 |
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(30); |
| 52 |
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(12); |
| 53 |
|
| 54 |
// Add conditional formatting |
| 55 |
$helper->log('Add conditional formatting'); |
| 56 |
$conditional1 = new Conditional(); |
| 57 |
$conditional1->setConditionType(Conditional::CONDITION_CELLIS) |
| 58 |
->setOperatorType(Conditional::OPERATOR_BETWEEN) |
| 59 |
->addCondition('200') |
| 60 |
->addCondition('400'); |
| 61 |
$conditional1->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_YELLOW); |
| 62 |
$conditional1->getStyle()->getFont()->setBold(true); |
| 63 |
$conditional1->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); |
| 64 |
|
| 65 |
$conditional2 = new Conditional(); |
| 66 |
$conditional2->setConditionType(Conditional::CONDITION_CELLIS) |
| 67 |
->setOperatorType(Conditional::OPERATOR_LESSTHAN) |
| 68 |
->addCondition('0'); |
| 69 |
$conditional2->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_RED); |
| 70 |
$conditional2->getStyle()->getFont()->setItalic(true); |
| 71 |
$conditional2->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); |
| 72 |
|
| 73 |
$conditional3 = new Conditional(); |
| 74 |
$conditional3->setConditionType(Conditional::CONDITION_CELLIS) |
| 75 |
->setOperatorType(Conditional::OPERATOR_GREATERTHANOREQUAL) |
| 76 |
->addCondition('0'); |
| 77 |
$conditional3->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_GREEN); |
| 78 |
$conditional3->getStyle()->getFont()->setItalic(true); |
| 79 |
$conditional3->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); |
| 80 |
|
| 81 |
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('B2')->getConditionalStyles(); |
| 82 |
$conditionalStyles[] = $conditional1; |
| 83 |
$conditionalStyles[] = $conditional2; |
| 84 |
$conditionalStyles[] = $conditional3; |
| 85 |
$spreadsheet->getActiveSheet()->getStyle('B2')->setConditionalStyles($conditionalStyles); |
| 86 |
|
| 87 |
// duplicate the conditional styles across a range of cells |
| 88 |
$helper->log('Duplicate the conditional formatting across a range of cells'); |
| 89 |
$spreadsheet->getActiveSheet()->duplicateConditionalStyle( |
| 90 |
$spreadsheet->getActiveSheet()->getStyle('B2')->getConditionalStyles(), |
| 91 |
'B3:B7' |
| 92 |
); |
| 93 |
|
| 94 |
// Set fonts |
| 95 |
$helper->log('Set fonts'); |
| 96 |
$spreadsheet->getActiveSheet()->getStyle('A1:B1')->getFont()->setBold(true); |
| 97 |
//$spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); |
| 98 |
$spreadsheet->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true); |
| 99 |
//$spreadsheet->getActiveSheet()->getStyle('B7')->getFont()->setBold(true); |
| 100 |
// Set header and footer. When no different headers for odd/even are used, odd header is assumed. |
| 101 |
$helper->log('Set header/footer'); |
| 102 |
$spreadsheet->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D'); |
| 103 |
$spreadsheet->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $spreadsheet->getProperties()->getTitle() . '&RPage &P of &N'); |
| 104 |
|
| 105 |
// Set page orientation and size |
| 106 |
$helper->log('Set page orientation and size'); |
| 107 |
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_PORTRAIT); |
| 108 |
$spreadsheet->getActiveSheet()->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4); |
| 109 |
|
| 110 |
// Rename worksheet |
| 111 |
$helper->log('Rename worksheet'); |
| 112 |
$spreadsheet->getActiveSheet()->setTitle('Invoice'); |
| 113 |
|
| 114 |
// Save |
| 115 |
$helper->write($spreadsheet, __FILE__); |
| 116 |
|