29advancedvaluebinder.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  32. /** PHPExcel */
  33. require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  34. // Set timezone
  35. echo date('H:i:s') , " Set timezone" , EOL;
  36. date_default_timezone_set('UTC');
  37. // Set value binder
  38. echo date('H:i:s') , " Set value binder" , EOL;
  39. PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
  40. // Create new PHPExcel object
  41. echo date('H:i:s') , " Create new PHPExcel object" , EOL;
  42. $objPHPExcel = new PHPExcel();
  43. // Set document properties
  44. echo date('H:i:s') , " Set document properties" , EOL;
  45. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  46. ->setLastModifiedBy("Maarten Balliauw")
  47. ->setTitle("Office 2007 XLSX Test Document")
  48. ->setSubject("Office 2007 XLSX Test Document")
  49. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  50. ->setKeywords("office 2007 openxml php")
  51. ->setCategory("Test result file");
  52. // Set default font
  53. echo date('H:i:s') , " Set default font" , EOL;
  54. $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Arial');
  55. $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
  56. // Set column widths
  57. echo date('H:i:s') , " Set column widths" , EOL;
  58. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
  59. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(14);
  60. // Add some data, resembling some different data types
  61. echo date('H:i:s') , " Add some data" , EOL;
  62. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'String value:')
  63. ->setCellValue('B1', 'Mark Baker');
  64. $objPHPExcel->getActiveSheet()->setCellValue('A2', 'Numeric value #1:')
  65. ->setCellValue('B2', 12345);
  66. $objPHPExcel->getActiveSheet()->setCellValue('A3', 'Numeric value #2:')
  67. ->setCellValue('B3', -12.345);
  68. $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Numeric value #3:')
  69. ->setCellValue('B4', .12345);
  70. $objPHPExcel->getActiveSheet()->setCellValue('A5', 'Numeric value #4:')
  71. ->setCellValue('B5', '12345');
  72. $objPHPExcel->getActiveSheet()->setCellValue('A6', 'Numeric value #5:')
  73. ->setCellValue('B6', '1.2345');
  74. $objPHPExcel->getActiveSheet()->setCellValue('A7', 'Numeric value #6:')
  75. ->setCellValue('B7', '.12345');
  76. $objPHPExcel->getActiveSheet()->setCellValue('A8', 'Numeric value #7:')
  77. ->setCellValue('B8', '1.234e-5');
  78. $objPHPExcel->getActiveSheet()->setCellValue('A9', 'Numeric value #8:')
  79. ->setCellValue('B9', '-1.234e+5');
  80. $objPHPExcel->getActiveSheet()->setCellValue('A10', 'Boolean value:')
  81. ->setCellValue('B10', 'TRUE');
  82. $objPHPExcel->getActiveSheet()->setCellValue('A11', 'Percentage value #1:')
  83. ->setCellValue('B11', '10%');
  84. $objPHPExcel->getActiveSheet()->setCellValue('A12', 'Percentage value #2:')
  85. ->setCellValue('B12', '12.5%');
  86. $objPHPExcel->getActiveSheet()->setCellValue('A13', 'Fraction value #1:')
  87. ->setCellValue('B13', '-1/2');
  88. $objPHPExcel->getActiveSheet()->setCellValue('A14', 'Fraction value #2:')
  89. ->setCellValue('B14', '3 1/2');
  90. $objPHPExcel->getActiveSheet()->setCellValue('A15', 'Fraction value #3:')
  91. ->setCellValue('B15', '-12 3/4');
  92. $objPHPExcel->getActiveSheet()->setCellValue('A16', 'Fraction value #4:')
  93. ->setCellValue('B16', '13/4');
  94. $objPHPExcel->getActiveSheet()->setCellValue('A17', 'Currency value #1:')
  95. ->setCellValue('B17', '$12345');
  96. $objPHPExcel->getActiveSheet()->setCellValue('A18', 'Currency value #2:')
  97. ->setCellValue('B18', '$12345.67');
  98. $objPHPExcel->getActiveSheet()->setCellValue('A19', 'Currency value #3:')
  99. ->setCellValue('B19', '$12,345.67');
  100. $objPHPExcel->getActiveSheet()->setCellValue('A20', 'Date value #1:')
  101. ->setCellValue('B20', '21 December 1983');
  102. $objPHPExcel->getActiveSheet()->setCellValue('A21', 'Date value #2:')
  103. ->setCellValue('B21', '19-Dec-1960');
  104. $objPHPExcel->getActiveSheet()->setCellValue('A22', 'Date value #3:')
  105. ->setCellValue('B22', '07/12/1982');
  106. $objPHPExcel->getActiveSheet()->setCellValue('A23', 'Date value #4:')
  107. ->setCellValue('B23', '24-11-1950');
  108. $objPHPExcel->getActiveSheet()->setCellValue('A24', 'Date value #5:')
  109. ->setCellValue('B24', '17-Mar');
  110. $objPHPExcel->getActiveSheet()->setCellValue('A25', 'Time value #1:')
  111. ->setCellValue('B25', '01:30');
  112. $objPHPExcel->getActiveSheet()->setCellValue('A26', 'Time value #2:')
  113. ->setCellValue('B26', '01:30:15');
  114. $objPHPExcel->getActiveSheet()->setCellValue('A27', 'Date/Time value:')
  115. ->setCellValue('B27', '19-Dec-1960 01:30');
  116. $objPHPExcel->getActiveSheet()->setCellValue('A28', 'Formula:')
  117. ->setCellValue('B28', '=SUM(B2:B9)');
  118. // Rename worksheet
  119. echo date('H:i:s') , " Rename worksheet" , EOL;
  120. $objPHPExcel->getActiveSheet()->setTitle('Advanced value binder');
  121. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  122. $objPHPExcel->setActiveSheetIndex(0);
  123. // Save Excel 2007 file
  124. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  125. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  126. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  127. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  128. // Save Excel5 file
  129. echo date('H:i:s') , " Write to Excel5 format" , EOL;
  130. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  131. $objWriter->save(str_replace('.php', '.xls', __FILE__));
  132. echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  133. // Echo memory peak usage
  134. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  135. // Echo done
  136. echo date('H:i:s') , " Done writing file" , EOL;
  137. echo 'File has been created in ' , getcwd() , EOL;