PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.4.0/Tests/15datavalidation.php

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