39dropdown.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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()
  41. ->setCreator("PHPOffice")
  42. ->setLastModifiedBy("PHPOffice")
  43. ->setTitle("PHPExcel Test Document")
  44. ->setSubject("PHPExcel Test Document")
  45. ->setDescription("Test document for PHPExcel, generated using PHP classes.")
  46. ->setKeywords("Office PHPExcel php")
  47. ->setCategory("Test result file");
  48. function transpose($value) {
  49. return array($value);
  50. }
  51. // Add some data
  52. $continentColumn = 'D';
  53. $column = 'F';
  54. // Set data for dropdowns
  55. foreach(glob('./data/continents/*') as $key => $filename) {
  56. $continent = pathinfo($filename, PATHINFO_FILENAME);
  57. echo "Loading $continent", EOL;
  58. $continent = str_replace(' ','_',$continent);
  59. $countries = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  60. $countryCount = count($countries);
  61. // Transpose $countries from a row to a column array
  62. $countries = array_map('transpose', $countries);
  63. $objPHPExcel->getActiveSheet()
  64. ->fromArray($countries, null, $column . '1');
  65. $objPHPExcel->addNamedRange(
  66. new PHPExcel_NamedRange(
  67. $continent,
  68. $objPHPExcel->getActiveSheet(), $column . '1:' . $column . $countryCount
  69. )
  70. );
  71. $objPHPExcel->getActiveSheet()
  72. ->getColumnDimension($column)
  73. ->setVisible(false);
  74. $objPHPExcel->getActiveSheet()
  75. ->setCellValue($continentColumn . ($key+1), $continent);
  76. ++$column;
  77. }
  78. // Hide the dropdown data
  79. $objPHPExcel->getActiveSheet()
  80. ->getColumnDimension($continentColumn)
  81. ->setVisible(false);
  82. $objPHPExcel->addNamedRange(
  83. new PHPExcel_NamedRange(
  84. 'Continents',
  85. $objPHPExcel->getActiveSheet(), $continentColumn . '1:' . $continentColumn . ($key+1)
  86. )
  87. );
  88. // Set selection cells
  89. $objPHPExcel->getActiveSheet()
  90. ->setCellValue('A1', 'Continent:');
  91. $objPHPExcel->getActiveSheet()
  92. ->setCellValue('B1', 'Select continent');
  93. $objPHPExcel->getActiveSheet()
  94. ->setCellValue('B3', '=' . $column . 1);
  95. $objPHPExcel->getActiveSheet()
  96. ->setCellValue('B3', 'Select country');
  97. $objPHPExcel->getActiveSheet()
  98. ->getStyle('A1:A3')
  99. ->getFont()->setBold(true);
  100. // Set linked validators
  101. $objValidation = $objPHPExcel->getActiveSheet()
  102. ->getCell('B1')
  103. ->getDataValidation();
  104. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST )
  105. ->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION )
  106. ->setAllowBlank(false)
  107. ->setShowInputMessage(true)
  108. ->setShowErrorMessage(true)
  109. ->setShowDropDown(true)
  110. ->setErrorTitle('Input error')
  111. ->setError('Continent is not in the list.')
  112. ->setPromptTitle('Pick from the list')
  113. ->setPrompt('Please pick a continent from the drop-down list.')
  114. ->setFormula1('=Continents');
  115. $objPHPExcel->getActiveSheet()
  116. ->setCellValue('A3', 'Country:');
  117. $objPHPExcel->getActiveSheet()
  118. ->getStyle('A3')
  119. ->getFont()->setBold(true);
  120. $objValidation = $objPHPExcel->getActiveSheet()
  121. ->getCell('B3')
  122. ->getDataValidation();
  123. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST )
  124. ->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION )
  125. ->setAllowBlank(false)
  126. ->setShowInputMessage(true)
  127. ->setShowErrorMessage(true)
  128. ->setShowDropDown(true)
  129. ->setErrorTitle('Input error')
  130. ->setError('Country is not in the list.')
  131. ->setPromptTitle('Pick from the list')
  132. ->setPrompt('Please pick a country from the drop-down list.')
  133. ->setFormula1('=INDIRECT($B$1)');
  134. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
  135. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
  136. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  137. $objPHPExcel->setActiveSheetIndex(0);
  138. // Save Excel 2007 file
  139. // This linked validation list method only seems to work for Excel2007, not for Excel5
  140. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  141. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  142. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  143. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  144. // Echo memory peak usage
  145. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  146. // Echo done
  147. echo date('H:i:s') , " Done writing files" , EOL;
  148. echo 'Files have been created in ' , getcwd() , EOL;