For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
9.1 KiB

  1. <?php
  2. /** PHPExcel root directory */
  3. if (!defined('PHPEXCEL_ROOT')) {
  4. /**
  5. * @ignore
  6. */
  7. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
  8. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  9. }
  10. /**
  11. * PHPExcel_IOFactory
  12. *
  13. * Copyright (c) 2006 - 2015 PHPExcel
  14. *
  15. * This library is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2.1 of the License, or (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with this library; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * @category PHPExcel
  30. * @package PHPExcel
  31. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  32. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  33. * @version ##VERSION##, ##DATE##
  34. */
  35. class PHPExcel_IOFactory
  36. {
  37. /**
  38. * Search locations
  39. *
  40. * @var array
  41. * @access private
  42. * @static
  43. */
  44. private static $searchLocations = array(
  45. array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
  46. array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
  47. );
  48. /**
  49. * Autoresolve classes
  50. *
  51. * @var array
  52. * @access private
  53. * @static
  54. */
  55. private static $autoResolveClasses = array(
  56. 'Excel2007',
  57. 'Excel5',
  58. 'Excel2003XML',
  59. 'OOCalc',
  60. 'SYLK',
  61. 'Gnumeric',
  62. 'HTML',
  63. 'CSV',
  64. );
  65. /**
  66. * Private constructor for PHPExcel_IOFactory
  67. */
  68. private function __construct()
  69. {
  70. }
  71. /**
  72. * Get search locations
  73. *
  74. * @static
  75. * @access public
  76. * @return array
  77. */
  78. public static function getSearchLocations()
  79. {
  80. return self::$searchLocations;
  81. }
  82. /**
  83. * Set search locations
  84. *
  85. * @static
  86. * @access public
  87. * @param array $value
  88. * @throws PHPExcel_Reader_Exception
  89. */
  90. public static function setSearchLocations($value)
  91. {
  92. if (is_array($value)) {
  93. self::$searchLocations = $value;
  94. } else {
  95. throw new PHPExcel_Reader_Exception('Invalid parameter passed.');
  96. }
  97. }
  98. /**
  99. * Add search location
  100. *
  101. * @static
  102. * @access public
  103. * @param string $type Example: IWriter
  104. * @param string $location Example: PHPExcel/Writer/{0}.php
  105. * @param string $classname Example: PHPExcel_Writer_[0]
  106. */
  107. public static function addSearchLocation($type = '', $location = '', $classname = '')
  108. {
  109. self::$searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
  110. }
  111. /**
  112. * Create PHPExcel_Writer_IWriter
  113. *
  114. * @static
  115. * @access public
  116. * @param PHPExcel $phpExcel
  117. * @param string $writerType Example: Excel2007
  118. * @return PHPExcel_Writer_IWriter
  119. * @throws PHPExcel_Reader_Exception
  120. */
  121. public static function createWriter(PHPExcel $phpExcel, $writerType = '')
  122. {
  123. // Search type
  124. $searchType = 'IWriter';
  125. // Include class
  126. foreach (self::$searchLocations as $searchLocation) {
  127. if ($searchLocation['type'] == $searchType) {
  128. $className = str_replace('{0}', $writerType, $searchLocation['class']);
  129. $instance = new $className($phpExcel);
  130. if ($instance !== null) {
  131. return $instance;
  132. }
  133. }
  134. }
  135. // Nothing found...
  136. throw new PHPExcel_Reader_Exception("No $searchType found for type $writerType");
  137. }
  138. /**
  139. * Create PHPExcel_Reader_IReader
  140. *
  141. * @static
  142. * @access public
  143. * @param string $readerType Example: Excel2007
  144. * @return PHPExcel_Reader_IReader
  145. * @throws PHPExcel_Reader_Exception
  146. */
  147. public static function createReader($readerType = '')
  148. {
  149. // Search type
  150. $searchType = 'IReader';
  151. // Include class
  152. foreach (self::$searchLocations as $searchLocation) {
  153. if ($searchLocation['type'] == $searchType) {
  154. $className = str_replace('{0}', $readerType, $searchLocation['class']);
  155. $instance = new $className();
  156. if ($instance !== null) {
  157. return $instance;
  158. }
  159. }
  160. }
  161. // Nothing found...
  162. throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType");
  163. }
  164. /**
  165. * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  166. *
  167. * @static
  168. * @access public
  169. * @param string $pFilename The name of the spreadsheet file
  170. * @return PHPExcel
  171. * @throws PHPExcel_Reader_Exception
  172. */
  173. public static function load($pFilename)
  174. {
  175. $reader = self::createReaderForFile($pFilename);
  176. return $reader->load($pFilename);
  177. }
  178. /**
  179. * Identify file type using automatic PHPExcel_Reader_IReader resolution
  180. *
  181. * @static
  182. * @access public
  183. * @param string $pFilename The name of the spreadsheet file to identify
  184. * @return string
  185. * @throws PHPExcel_Reader_Exception
  186. */
  187. public static function identify($pFilename)
  188. {
  189. $reader = self::createReaderForFile($pFilename);
  190. $className = get_class($reader);
  191. $classType = explode('_', $className);
  192. unset($reader);
  193. return array_pop($classType);
  194. }
  195. /**
  196. * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
  197. *
  198. * @static
  199. * @access public
  200. * @param string $pFilename The name of the spreadsheet file
  201. * @return PHPExcel_Reader_IReader
  202. * @throws PHPExcel_Reader_Exception
  203. */
  204. public static function createReaderForFile($pFilename)
  205. {
  206. // First, lucky guess by inspecting file extension
  207. $pathinfo = pathinfo($pFilename);
  208. $extensionType = null;
  209. if (isset($pathinfo['extension'])) {
  210. switch (strtolower($pathinfo['extension'])) {
  211. case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet
  212. case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
  213. case 'xltx': // Excel (OfficeOpenXML) Template
  214. case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded)
  215. $extensionType = 'Excel2007';
  216. break;
  217. case 'xls': // Excel (BIFF) Spreadsheet
  218. case 'xlt': // Excel (BIFF) Template
  219. $extensionType = 'Excel5';
  220. break;
  221. case 'ods': // Open/Libre Offic Calc
  222. case 'ots': // Open/Libre Offic Calc Template
  223. $extensionType = 'OOCalc';
  224. break;
  225. case 'slk':
  226. $extensionType = 'SYLK';
  227. break;
  228. case 'xml': // Excel 2003 SpreadSheetML
  229. $extensionType = 'Excel2003XML';
  230. break;
  231. case 'gnumeric':
  232. $extensionType = 'Gnumeric';
  233. break;
  234. case 'htm':
  235. case 'html':
  236. $extensionType = 'HTML';
  237. break;
  238. case 'csv':
  239. // Do nothing
  240. // We must not try to use CSV reader since it loads
  241. // all files including Excel files etc.
  242. break;
  243. default:
  244. break;
  245. }
  246. if ($extensionType !== null) {
  247. $reader = self::createReader($extensionType);
  248. // Let's see if we are lucky
  249. if (isset($reader) && $reader->canRead($pFilename)) {
  250. return $reader;
  251. }
  252. }
  253. }
  254. // If we reach here then "lucky guess" didn't give any result
  255. // Try walking through all the options in self::$autoResolveClasses
  256. foreach (self::$autoResolveClasses as $autoResolveClass) {
  257. // Ignore our original guess, we know that won't work
  258. if ($autoResolveClass !== $extensionType) {
  259. $reader = self::createReader($autoResolveClass);
  260. if ($reader->canRead($pFilename)) {
  261. return $reader;
  262. }
  263. }
  264. }
  265. throw new PHPExcel_Reader_Exception('Unable to identify a reader for this file');
  266. }
  267. }