No Description

08conditionalformatting.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (C) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** Error reporting */
  28. error_reporting(E_ALL);
  29. ini_set('display_errors', TRUE);
  30. ini_set('display_startup_errors', TRUE);
  31. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  32. date_default_timezone_set('Europe/London');
  33. /** Include PHPExcel */
  34. require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  35. // Create new PHPExcel object
  36. echo date('H:i:s') , " Create new PHPExcel object" , EOL;
  37. $objPHPExcel = new PHPExcel();
  38. // Set document properties
  39. echo date('H:i:s') , " Set document properties" , EOL;
  40. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  41. ->setLastModifiedBy("Maarten Balliauw")
  42. ->setTitle("Office 2007 XLSX Test Document")
  43. ->setSubject("Office 2007 XLSX Test Document")
  44. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  45. ->setKeywords("office 2007 openxml php")
  46. ->setCategory("Test result file");
  47. // Create a first sheet, representing sales data
  48. echo date('H:i:s') , " Add some data" , EOL;
  49. $objPHPExcel->setActiveSheetIndex(0);
  50. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Description')
  51. ->setCellValue('B1', 'Amount');
  52. $objPHPExcel->getActiveSheet()->setCellValue('A2', 'Paycheck received')
  53. ->setCellValue('B2', 100);
  54. $objPHPExcel->getActiveSheet()->setCellValue('A3', 'Cup of coffee bought')
  55. ->setCellValue('B3', -1.5);
  56. $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Cup of coffee bought')
  57. ->setCellValue('B4', -1.5);
  58. $objPHPExcel->getActiveSheet()->setCellValue('A5', 'Cup of tea bought')
  59. ->setCellValue('B5', -1.2);
  60. $objPHPExcel->getActiveSheet()->setCellValue('A6', 'Found some money')
  61. ->setCellValue('B6', 8);
  62. $objPHPExcel->getActiveSheet()->setCellValue('A7', 'Total:')
  63. ->setCellValue('B7', '=SUM(B2:B6)');
  64. // Set column widths
  65. echo date('H:i:s') , " Set column widths" , EOL;
  66. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  67. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
  68. // Add conditional formatting
  69. echo date('H:i:s') , " Add conditional formatting" , EOL;
  70. $objConditional1 = new PHPExcel_Style_Conditional();
  71. $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS)
  72. ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_BETWEEN)
  73. ->addCondition('200')
  74. ->addCondition('400');
  75. $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_YELLOW);
  76. $objConditional1->getStyle()->getFont()->setBold(true);
  77. $objConditional1->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  78. $objConditional2 = new PHPExcel_Style_Conditional();
  79. $objConditional2->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS)
  80. ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN)
  81. ->addCondition('0');
  82. $objConditional2->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
  83. $objConditional2->getStyle()->getFont()->setItalic(true);
  84. $objConditional2->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  85. $objConditional3 = new PHPExcel_Style_Conditional();
  86. $objConditional3->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS)
  87. ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL)
  88. ->addCondition('0');
  89. $objConditional3->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
  90. $objConditional3->getStyle()->getFont()->setItalic(true);
  91. $objConditional3->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  92. $conditionalStyles = $objPHPExcel->getActiveSheet()->getStyle('B2')->getConditionalStyles();
  93. array_push($conditionalStyles, $objConditional1);
  94. array_push($conditionalStyles, $objConditional2);
  95. array_push($conditionalStyles, $objConditional3);
  96. $objPHPExcel->getActiveSheet()->getStyle('B2')->setConditionalStyles($conditionalStyles);
  97. // duplicate the conditional styles across a range of cells
  98. echo date('H:i:s') , " Duplicate the conditional formatting across a range of cells" , EOL;
  99. $objPHPExcel->getActiveSheet()->duplicateConditionalStyle(
  100. $objPHPExcel->getActiveSheet()->getStyle('B2')->getConditionalStyles(),
  101. 'B3:B7'
  102. );
  103. // Set fonts
  104. echo date('H:i:s') , " Set fonts" , EOL;
  105. $objPHPExcel->getActiveSheet()->getStyle('A1:B1')->getFont()->setBold(true);
  106. //$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
  107. $objPHPExcel->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true);
  108. //$objPHPExcel->getActiveSheet()->getStyle('B7')->getFont()->setBold(true);
  109. // Set header and footer. When no different headers for odd/even are used, odd header is assumed.
  110. echo date('H:i:s') , " Set header/footer" , EOL;
  111. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
  112. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
  113. // Set page orientation and size
  114. echo date('H:i:s') , " Set page orientation and size" , EOL;
  115. $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
  116. $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
  117. // Rename worksheet
  118. echo date('H:i:s') , " Rename worksheet" , EOL;
  119. $objPHPExcel->getActiveSheet()->setTitle('Invoice');
  120. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  121. $objPHPExcel->setActiveSheetIndex(0);
  122. // Save Excel 2007 file
  123. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  124. $callStartTime = microtime(true);
  125. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  126. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  127. $callEndTime = microtime(true);
  128. $callTime = $callEndTime - $callStartTime;
  129. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  130. echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  131. // Save Excel5 file
  132. echo date('H:i:s') , " Write to Excel5 format" , EOL;
  133. $callStartTime = microtime(true);
  134. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  135. $objWriter->save(str_replace('.php', '.xls', __FILE__));
  136. $callEndTime = microtime(true);
  137. $callTime = $callEndTime - $callStartTime;
  138. echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  139. echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  140. // Echo memory usage
  141. echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
  142. // Echo memory peak usage
  143. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  144. // Echo done
  145. echo date('H:i:s') , " Done writing file" , EOL;
  146. echo 'File has been created in ' , getcwd() , EOL;