08_Conditional_formatting.php 4.9 KB

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