PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpoffice/phpexcel/Examples/10autofilter-selection-display.php

https://gitlab.com/techniconline/kmc
PHP | 196 lines | 132 code | 19 blank | 45 comment | 7 complexity | cb69bcec711440f154f5b2ab6ebe8396 MD5 | raw file
  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. date_default_timezone_set('Europe/London');
  32. define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  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('PHPExcel Test Document')
  43. ->setSubject('PHPExcel Test Document')
  44. ->setDescription('Test document for PHPExcel, generated using PHP classes.')
  45. ->setKeywords('office PHPExcel php')
  46. ->setCategory('Test result file');
  47. // Create the worksheet
  48. echo date('H:i:s') . ' Add data' . EOL;
  49. $objPHPExcel->setActiveSheetIndex(0);
  50. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Financial Year')
  51. ->setCellValue('B1', 'Financial Period')
  52. ->setCellValue('C1', 'Country')
  53. ->setCellValue('D1', 'Date')
  54. ->setCellValue('E1', 'Sales Value')
  55. ->setCellValue('F1', 'Expenditure');
  56. $startYear = $endYear = $currentYear = date('Y');
  57. $startYear--;
  58. $endYear++;
  59. $years = range($startYear, $endYear);
  60. $periods = range(1, 12);
  61. $countries = array('United States', 'UK', 'France', 'Germany',
  62. 'Italy', 'Spain', 'Portugal', 'Japan'
  63. );
  64. $row = 2;
  65. foreach ($years as $year) {
  66. foreach ($periods as $period) {
  67. foreach ($countries as $country) {
  68. $endDays = date('t', mktime(0, 0, 0, $period, 1, $year));
  69. for ($i = 1; $i <= $endDays; ++$i) {
  70. $eDate = PHPExcel_Shared_Date::FormattedPHPToExcel(
  71. $year,
  72. $period,
  73. $i
  74. );
  75. $value = rand(500, 1000) * (1 + rand(-0.25, +0.25));
  76. $salesValue = $invoiceValue = NULL;
  77. $incomeOrExpenditure = rand(-1, 1);
  78. if ($incomeOrExpenditure == -1) {
  79. $expenditure = rand(-500, -1000) * (1 + rand(-0.25, +0.25));
  80. $income = NULL;
  81. } elseif ($incomeOrExpenditure == 1) {
  82. $expenditure = rand(-500, -1000) * (1 + rand(-0.25, +0.25));
  83. $income = rand(500, 1000) * (1 + rand(-0.25, +0.25));;
  84. } else {
  85. $expenditure = NULL;
  86. $income = rand(500, 1000) * (1 + rand(-0.25, +0.25));;
  87. }
  88. $dataArray = array($year,
  89. $period,
  90. $country,
  91. $eDate,
  92. $income,
  93. $expenditure,
  94. );
  95. $objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A' . $row++);
  96. }
  97. }
  98. }
  99. }
  100. $row--;
  101. // Set styling
  102. echo date('H:i:s') . ' Set styling' . EOL;
  103. $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(true);
  104. $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setWrapText(TRUE);
  105. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(12.5);
  106. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10.5);
  107. $objPHPExcel->getActiveSheet()->getStyle('D2:D' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
  108. $objPHPExcel->getActiveSheet()->getStyle('E2:F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  109. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(14);
  110. $objPHPExcel->getActiveSheet()->freezePane('A2');
  111. // Set autofilter range
  112. echo date('H:i:s') . ' Set autofilter range' . EOL;
  113. // Always include the complete filter range!
  114. // Excel does support setting only the caption
  115. // row, but that's not a best practise...
  116. $objPHPExcel->getActiveSheet()->setAutoFilter($objPHPExcel->getActiveSheet()->calculateWorksheetDimension());
  117. // Set active filters
  118. $autoFilter = $objPHPExcel->getActiveSheet()->getAutoFilter();
  119. echo date('H:i:s') . ' Set active filters' . EOL;
  120. // Filter the Country column on a filter value of countries beginning with the letter U (or Japan)
  121. // We use * as a wildcard, so specify as U* and using a wildcard requires customFilter
  122. $autoFilter->getColumn('C')
  123. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER)
  124. ->createRule()
  125. ->setRule(
  126. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  127. 'u*'
  128. )
  129. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
  130. $autoFilter->getColumn('C')
  131. ->createRule()
  132. ->setRule(
  133. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  134. 'japan'
  135. )
  136. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
  137. // Filter the Date column on a filter value of the first day of every period of the current year
  138. // We us a dateGroup ruletype for this, although it is still a standard filter
  139. foreach ($periods as $period) {
  140. $endDate = date('t', mktime(0, 0, 0, $period, 1, $currentYear));
  141. $autoFilter->getColumn('D')
  142. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER)
  143. ->createRule()
  144. ->setRule(
  145. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  146. array(
  147. 'year' => $currentYear,
  148. 'month' => $period,
  149. 'day' => $endDate
  150. )
  151. )
  152. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP);
  153. }
  154. // Display only sales values that are blank
  155. // Standard filter, operator equals, and value of NULL
  156. $autoFilter->getColumn('E')
  157. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER)
  158. ->createRule()
  159. ->setRule(
  160. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  161. ''
  162. );
  163. // Execute filtering
  164. echo date('H:i:s') . ' Execute filtering' . EOL;
  165. $autoFilter->showHideRows();
  166. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  167. $objPHPExcel->setActiveSheetIndex(0);
  168. // Display Results of filtering
  169. echo date('H:i:s') . ' Display filtered rows' . EOL;
  170. foreach ($objPHPExcel->getActiveSheet()->getRowIterator() as $row) {
  171. if ($objPHPExcel->getActiveSheet()->getRowDimension($row->getRowIndex())->getVisible()) {
  172. echo ' Row number - ', $row->getRowIndex(), ' ';
  173. echo $objPHPExcel->getActiveSheet()->getCell('C' . $row->getRowIndex())->getValue(), ' ';
  174. echo $objPHPExcel->getActiveSheet()->getCell('D' . $row->getRowIndex())->getFormattedValue(), ' ';
  175. echo EOL;
  176. }
  177. }