123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- if (!defined('PHPEXCEL_ROOT')) {
-
- define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
- require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
- }
- class PHPExcel_IOFactory
- {
-
- private static $searchLocations = array(
- array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
- array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
- );
-
- private static $autoResolveClasses = array(
- 'Excel2007',
- 'Excel5',
- 'Excel2003XML',
- 'OOCalc',
- 'SYLK',
- 'Gnumeric',
- 'HTML',
- 'CSV',
- );
-
- private function __construct()
- {
- }
-
- public static function getSearchLocations()
- {
- return self::$searchLocations;
- }
-
- public static function setSearchLocations($value)
- {
- if (is_array($value)) {
- self::$searchLocations = $value;
- } else {
- throw new PHPExcel_Reader_Exception('Invalid parameter passed.');
- }
- }
-
- public static function addSearchLocation($type = '', $location = '', $classname = '')
- {
- self::$searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
- }
-
- public static function createWriter(PHPExcel $phpExcel, $writerType = '')
- {
-
- $searchType = 'IWriter';
-
- foreach (self::$searchLocations as $searchLocation) {
- if ($searchLocation['type'] == $searchType) {
- $className = str_replace('{0}', $writerType, $searchLocation['class']);
- $instance = new $className($phpExcel);
- if ($instance !== null) {
- return $instance;
- }
- }
- }
-
- throw new PHPExcel_Reader_Exception("No $searchType found for type $writerType");
- }
-
- public static function createReader($readerType = '')
- {
-
- $searchType = 'IReader';
-
- foreach (self::$searchLocations as $searchLocation) {
- if ($searchLocation['type'] == $searchType) {
- $className = str_replace('{0}', $readerType, $searchLocation['class']);
- $instance = new $className();
- if ($instance !== null) {
- return $instance;
- }
- }
- }
-
- throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType");
- }
-
- public static function load($pFilename)
- {
- $reader = self::createReaderForFile($pFilename);
- return $reader->load($pFilename);
- }
-
- public static function identify($pFilename)
- {
- $reader = self::createReaderForFile($pFilename);
- $className = get_class($reader);
- $classType = explode('_', $className);
- unset($reader);
- return array_pop($classType);
- }
-
- public static function createReaderForFile($pFilename)
- {
-
- $pathinfo = pathinfo($pFilename);
- $extensionType = null;
- if (isset($pathinfo['extension'])) {
- switch (strtolower($pathinfo['extension'])) {
- case 'xlsx':
- case 'xlsm':
- case 'xltx':
- case 'xltm':
- $extensionType = 'Excel2007';
- break;
- case 'xls':
- case 'xlt':
- $extensionType = 'Excel5';
- break;
- case 'ods':
- case 'ots':
- $extensionType = 'OOCalc';
- break;
- case 'slk':
- $extensionType = 'SYLK';
- break;
- case 'xml':
- $extensionType = 'Excel2003XML';
- break;
- case 'gnumeric':
- $extensionType = 'Gnumeric';
- break;
- case 'htm':
- case 'html':
- $extensionType = 'HTML';
- break;
- case 'csv':
-
-
-
- break;
- default:
- break;
- }
- if ($extensionType !== null) {
- $reader = self::createReader($extensionType);
-
- if (isset($reader) && $reader->canRead($pFilename)) {
- return $reader;
- }
- }
- }
-
-
- foreach (self::$autoResolveClasses as $autoResolveClass) {
-
- if ($autoResolveClass !== $extensionType) {
- $reader = self::createReader($autoResolveClass);
- if ($reader->canRead($pFilename)) {
- return $reader;
- }
- }
- }
- throw new PHPExcel_Reader_Exception('Unable to identify a reader for this file');
- }
- }
|