PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Examples/32chartreadwrite.php

https://github.com/iGroup/PHPExcel
PHP | 131 lines | 81 code | 20 blank | 30 comment | 18 complexity | 63009468965d8757bac28c1278f1e9b1 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /** Error reporting */
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', TRUE);
  5. ini_set('display_startup_errors', TRUE);
  6. date_default_timezone_set('Europe/London');
  7. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  8. date_default_timezone_set('Europe/London');
  9. /**
  10. * PHPExcel
  11. *
  12. * Copyright (C) 2006 - 2012 PHPExcel
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. * @category PHPExcel
  29. * @package PHPExcel
  30. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  31. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  32. * @version ##VERSION##, ##DATE##
  33. */
  34. /** Include path **/
  35. set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
  36. /** PHPExcel_IOFactory */
  37. include 'PHPExcel/IOFactory.php';
  38. $inputFileType = 'Excel2007';
  39. $inputFileNames = 'templates/32readwrite*[0-9].xlsx';
  40. if ((isset($argc)) && ($argc > 1)) {
  41. $inputFileNames = array();
  42. for($i = 1; $i < $argc; ++$i) {
  43. $inputFileNames[] = __DIR__ . '/templates/' . $argv[$i];
  44. }
  45. } else {
  46. $inputFileNames = glob($inputFileNames);
  47. }
  48. foreach($inputFileNames as $inputFileName) {
  49. $inputFileNameShort = basename($inputFileName);
  50. if (!file_exists($inputFileName)) {
  51. echo date('H:i:s') , " File " , $inputFileNameShort , ' does not exist' , EOL;
  52. continue;
  53. }
  54. echo date('H:i:s') , " Load Test from $inputFileType file " , $inputFileNameShort , EOL;
  55. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  56. $objReader->setIncludeCharts(TRUE);
  57. $objPHPExcel = $objReader->load($inputFileName);
  58. echo date('H:i:s') , " Iterate worksheets looking at the charts" , EOL;
  59. foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  60. $sheetName = $worksheet->getTitle();
  61. echo 'Worksheet: ' , $sheetName , EOL;
  62. $chartNames = $worksheet->getChartNames();
  63. if(empty($chartNames)) {
  64. echo ' There are no charts in this worksheet' , EOL;
  65. } else {
  66. natsort($chartNames);
  67. foreach($chartNames as $i => $chartName) {
  68. $chart = $worksheet->getChartByName($chartName);
  69. if (!is_null($chart->getTitle())) {
  70. $caption = '"' . implode(' ',$chart->getTitle()->getCaption()) . '"';
  71. } else {
  72. $caption = 'Untitled';
  73. }
  74. echo ' ' , $chartName , ' - ' , $caption , EOL;
  75. echo str_repeat(' ',strlen($chartName)+3);
  76. $groupCount = $chart->getPlotArea()->getPlotGroupCount();
  77. if ($groupCount == 1) {
  78. $chartType = $chart->getPlotArea()->getPlotGroupByIndex(0)->getPlotType();
  79. echo ' ' , $chartType , EOL;
  80. } else {
  81. $chartTypes = array();
  82. for($i = 0; $i < $groupCount; ++$i) {
  83. $chartTypes[] = $chart->getPlotArea()->getPlotGroupByIndex($i)->getPlotType();
  84. }
  85. $chartTypes = array_unique($chartTypes);
  86. if (count($chartTypes) == 1) {
  87. $chartType = 'Multiple Plot ' . array_pop($chartTypes);
  88. echo ' ' , $chartType , EOL;
  89. } elseif (count($chartTypes) == 0) {
  90. echo ' *** Type not yet implemented' , EOL;
  91. } else {
  92. echo ' Combination Chart' , EOL;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. $outputFileName = basename($inputFileName);
  99. echo date('H:i:s') , " Write Tests to Excel2007 file " , EOL;
  100. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  101. $objWriter->setIncludeCharts(TRUE);
  102. $objWriter->save($outputFileName);
  103. echo date('H:i:s') , " File written to " , $outputFileName , EOL;
  104. $objPHPExcel->disconnectWorksheets();
  105. unset($objPHPExcel);
  106. }
  107. // Echo memory peak usage
  108. echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  109. // Echo done
  110. echo date('H:i:s') , " Done writing files" , EOL;
  111. echo 'Files have been created in ' , getcwd() , EOL;