For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
8.3 KiB

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. ;
  57. $startYear = $endYear = $currentYear = date('Y');
  58. $startYear--;
  59. $endYear++;
  60. $years = range($startYear,$endYear);
  61. $periods = range(1,12);
  62. $countries = array( 'United States', 'UK', 'France', 'Germany',
  63. 'Italy', 'Spain', 'Portugal', 'Japan'
  64. );
  65. $row = 2;
  66. foreach($years as $year) {
  67. foreach($periods as $period) {
  68. foreach($countries as $country) {
  69. $endDays = date('t',mktime(0,0,0,$period,1,$year));
  70. for($i = 1; $i <= $endDays; ++$i) {
  71. $eDate = PHPExcel_Shared_Date::FormattedPHPToExcel(
  72. $year,
  73. $period,
  74. $i
  75. );
  76. $value = rand(500,1000) * (1 + rand(-0.25,+0.25));
  77. $salesValue = $invoiceValue = NULL;
  78. $incomeOrExpenditure = rand(-1,1);
  79. if ($incomeOrExpenditure == -1) {
  80. $expenditure = rand(-500,-1000) * (1 + rand(-0.25,+0.25));
  81. $income = NULL;
  82. } elseif ($incomeOrExpenditure == 1) {
  83. $expenditure = rand(-500,-1000) * (1 + rand(-0.25,+0.25));
  84. $income = rand(500,1000) * (1 + rand(-0.25,+0.25));;
  85. } else {
  86. $expenditure = NULL;
  87. $income = rand(500,1000) * (1 + rand(-0.25,+0.25));;
  88. }
  89. $dataArray = array( $year,
  90. $period,
  91. $country,
  92. $eDate,
  93. $income,
  94. $expenditure,
  95. );
  96. $objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A'.$row++);
  97. }
  98. }
  99. }
  100. }
  101. $row--;
  102. // Set styling
  103. echo date('H:i:s').' Set styling'.EOL;
  104. $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(true);
  105. $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setWrapText(TRUE);
  106. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(12.5);
  107. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10.5);
  108. $objPHPExcel->getActiveSheet()->getStyle('D2:D'.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
  109. $objPHPExcel->getActiveSheet()->getStyle('E2:F'.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  110. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(14);
  111. $objPHPExcel->getActiveSheet()->freezePane('A2');
  112. // Set autofilter range
  113. echo date('H:i:s').' Set autofilter range'.EOL;
  114. // Always include the complete filter range!
  115. // Excel does support setting only the caption
  116. // row, but that's not a best practise...
  117. $objPHPExcel->getActiveSheet()->setAutoFilter($objPHPExcel->getActiveSheet()->calculateWorksheetDimension());
  118. // Set active filters
  119. $autoFilter = $objPHPExcel->getActiveSheet()->getAutoFilter();
  120. echo date('H:i:s').' Set active filters'.EOL;
  121. // Filter the Country column on a filter value of countries beginning with the letter U (or Japan)
  122. // We use * as a wildcard, so specify as U* and using a wildcard requires customFilter
  123. $autoFilter->getColumn('C')
  124. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER)
  125. ->createRule()
  126. ->setRule(
  127. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  128. 'u*'
  129. )
  130. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
  131. $autoFilter->getColumn('C')
  132. ->createRule()
  133. ->setRule(
  134. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  135. 'japan'
  136. )
  137. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
  138. // Filter the Date column on a filter value of the first day of every period of the current year
  139. // We us a dateGroup ruletype for this, although it is still a standard filter
  140. foreach($periods as $period) {
  141. $endDate = date('t',mktime(0,0,0,$period,1,$currentYear));
  142. $autoFilter->getColumn('D')
  143. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER)
  144. ->createRule()
  145. ->setRule(
  146. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  147. array(
  148. 'year' => $currentYear,
  149. 'month' => $period,
  150. 'day' => $endDate
  151. )
  152. )
  153. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP);
  154. }
  155. // Display only sales values that are blank
  156. // Standard filter, operator equals, and value of NULL
  157. $autoFilter->getColumn('E')
  158. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER)
  159. ->createRule()
  160. ->setRule(
  161. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  162. ''
  163. );
  164. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  165. $objPHPExcel->setActiveSheetIndex(0);
  166. // Save Excel 2007 file
  167. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  168. $callStartTime = microtime(true);
  169. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  170. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  171. $callEndTime = microtime(true);
  172. $callTime = $callEndTime - $callStartTime;
  173. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  174. echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  175. // Echo memory usage
  176. echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
  177. // Save Excel 95 file
  178. echo date('H:i:s') , " Write to Excel5 format" , EOL;
  179. $callStartTime = microtime(true);
  180. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  181. $objWriter->save(str_replace('.php', '.xls', __FILE__));
  182. $callEndTime = microtime(true);
  183. $callTime = $callEndTime - $callStartTime;
  184. echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  185. echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  186. // Echo memory usage
  187. echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
  188. // Echo memory peak usage
  189. echo date('H:i:s').' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).' MB'.EOL;
  190. // Echo done
  191. echo date('H:i:s').' Done writing files'.EOL;
  192. echo 'Files have been created in ' , getcwd() , EOL;