15datavalidation.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (C) 2006 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version 1.6.0, 2008-02-14
  26. */
  27. /* Modified by Bertrand Zuchuat */
  28. require_once 'symfony.inc.php';
  29. // Create new PHPExcel object
  30. echo date('H:i:s') . " Create new PHPExcel object\n";
  31. $objPHPExcel = new sfPhpExcel();
  32. // Set properties
  33. echo date('H:i:s') . " Set properties\n";
  34. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
  35. $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
  36. $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
  37. $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
  38. $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
  39. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  40. $objPHPExcel->getProperties()->setCategory("Test result file");
  41. // Create a first sheet
  42. echo date('H:i:s') . " Add data\n";
  43. $objPHPExcel->setActiveSheetIndex(0);
  44. $objPHPExcel->getActiveSheet()->setCellValue('A1', "Cell B3 and B5 contain data validation...");
  45. $objPHPExcel->getActiveSheet()->setCellValue('A3', "Number:");
  46. $objPHPExcel->getActiveSheet()->setCellValue('B3', "10");
  47. $objPHPExcel->getActiveSheet()->setCellValue('A5', "List:");
  48. $objPHPExcel->getActiveSheet()->setCellValue('B5', "Item A");
  49. // Set data validation
  50. echo date('H:i:s') . " Set data validation\n";
  51. $objValidation = $objPHPExcel->getActiveSheet()->getCell('B3')->getDataValidation();
  52. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
  53. $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
  54. $objValidation->setAllowBlank(true);
  55. $objValidation->setShowInputMessage(true);
  56. $objValidation->setShowErrorMessage(true);
  57. $objValidation->setErrorTitle('Input error');
  58. $objValidation->setError('Number is not allowed!');
  59. $objValidation->setPromptTitle('Allowed input');
  60. $objValidation->setPrompt('Only numbers between 10 and 20 are allowed.');
  61. $objValidation->setFormula1(10);
  62. $objValidation->setFormula2(20);
  63. $objPHPExcel->getActiveSheet()->getCell('B3')->setDataValidation($objValidation);
  64. $objValidation = $objPHPExcel->getActiveSheet()->getCell('B5')->getDataValidation();
  65. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST );
  66. $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION );
  67. $objValidation->setAllowBlank(false);
  68. $objValidation->setShowInputMessage(true);
  69. $objValidation->setShowErrorMessage(true);
  70. $objValidation->setShowDropDown(true);
  71. $objValidation->setErrorTitle('Input error');
  72. $objValidation->setError('Value is not in list.');
  73. $objValidation->setPromptTitle('Pick from list');
  74. $objValidation->setPrompt('Please pick a value from the drop-down list.');
  75. $objValidation->setFormula1('"Item A,Item B,Item C"'); // Make sure to put the list items between " and " !!!
  76. $objPHPExcel->getActiveSheet()->getCell('B5')->setDataValidation($objValidation);
  77. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  78. $objPHPExcel->setActiveSheetIndex(0);
  79. // Save Excel 2007 file
  80. echo date('H:i:s') . " Write to Excel2007 format\n";
  81. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  82. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  83. // Echo memory peak usage
  84. echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";
  85. // Echo done
  86. echo date('H:i:s') . " Done writing file.\r\n";