IOFactory.php 9.5 KB

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