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.

213 lines
8.1 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 Germany
  122. // As it's just a simple value filter, we can use FILTERTYPE_FILTER
  123. $autoFilter->getColumn('C')
  124. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER)
  125. ->createRule()
  126. ->setRule(
  127. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  128. 'Germany'
  129. );
  130. // Filter the Date column on a filter value of the year to date
  131. $autoFilter->getColumn('D')
  132. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER)
  133. ->createRule()
  134. ->setRule(
  135. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
  136. NULL,
  137. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE
  138. )
  139. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER);
  140. // Display only sales values that are between 400 and 600
  141. $autoFilter->getColumn('E')
  142. ->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER)
  143. ->createRule()
  144. ->setRule(
  145. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL,
  146. 400
  147. )
  148. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
  149. $autoFilter->getColumn('E')
  150. ->setJoin(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND)
  151. ->createRule()
  152. ->setRule(
  153. PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL,
  154. 600
  155. )
  156. ->setRuleType(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER);
  157. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  158. $objPHPExcel->setActiveSheetIndex(0);
  159. // Save Excel 2007 file
  160. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  161. $callStartTime = microtime(true);
  162. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  163. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  164. $callEndTime = microtime(true);
  165. $callTime = $callEndTime - $callStartTime;
  166. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  167. echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  168. // Echo memory usage
  169. echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
  170. // Save Excel 95 file
  171. echo date('H:i:s') , " Write to Excel5 format" , EOL;
  172. $callStartTime = microtime(true);
  173. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  174. $objWriter->save(str_replace('.php', '.xls', __FILE__));
  175. $callEndTime = microtime(true);
  176. $callTime = $callEndTime - $callStartTime;
  177. echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  178. echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  179. // Echo memory usage
  180. echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
  181. // Echo memory peak usage
  182. echo date('H:i:s').' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).' MB'.EOL;
  183. // Echo done
  184. echo date('H:i:s').' Done writing files'.EOL;
  185. echo 'Files have been created in ' , getcwd() , EOL;