5
* Copyright (c) 2006 - 2011 PHPExcel
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.
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.
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
23
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
24
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25
* @version 1.7.6, 2011-02-27
29
/** PHPExcel root directory */
30
if (!defined('PHPEXCEL_ROOT')) {
34
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
35
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
43
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
45
class PHPExcel_IOFactory
54
private static $_searchLocations = array(
55
array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
56
array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
66
private static $_autoResolveClasses = array(
77
* Private constructor for PHPExcel_IOFactory
79
private function __construct() { }
82
* Get search locations
88
public static function getSearchLocations() {
89
return self::$_searchLocations;
90
} // function getSearchLocations()
93
* Set search locations
100
public static function setSearchLocations($value) {
101
if (is_array($value)) {
102
self::$_searchLocations = $value;
104
throw new Exception('Invalid parameter passed.');
106
} // function setSearchLocations()
109
* Add search location
113
* @param string $type Example: IWriter
114
* @param string $location Example: PHPExcel/Writer/{0}.php
115
* @param string $classname Example: PHPExcel_Writer_{0}
117
public static function addSearchLocation($type = '', $location = '', $classname = '') {
118
self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
119
} // function addSearchLocation()
122
* Create PHPExcel_Writer_IWriter
126
* @param PHPExcel $phpExcel
127
* @param string $writerType Example: Excel2007
128
* @return PHPExcel_Writer_IWriter
131
public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
133
$searchType = 'IWriter';
136
foreach (self::$_searchLocations as $searchLocation) {
137
if ($searchLocation['type'] == $searchType) {
138
$className = str_replace('{0}', $writerType, $searchLocation['class']);
139
$classFile = str_replace('{0}', $writerType, $searchLocation['path']);
141
$instance = new $className($phpExcel);
142
if (!is_null($instance)) {
149
throw new Exception("No $searchType found for type $writerType");
150
} // function createWriter()
153
* Create PHPExcel_Reader_IReader
157
* @param string $readerType Example: Excel2007
158
* @return PHPExcel_Reader_IReader
161
public static function createReader($readerType = '') {
163
$searchType = 'IReader';
166
foreach (self::$_searchLocations as $searchLocation) {
167
if ($searchLocation['type'] == $searchType) {
168
$className = str_replace('{0}', $readerType, $searchLocation['class']);
169
$classFile = str_replace('{0}', $readerType, $searchLocation['path']);
171
$instance = new $className();
172
if (!is_null($instance)) {
179
throw new Exception("No $searchType found for type $readerType");
180
} // function createReader()
183
* Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
187
* @param string $pFileName
191
public static function load($pFilename) {
192
$reader = self::createReaderForFile($pFilename);
193
return $reader->load($pFilename);
197
* Identify file type using automatic PHPExcel_Reader_IReader resolution
201
* @param string $pFileName
205
public static function identify($pFilename) {
206
$reader = self::createReaderForFile($pFilename);
207
$className = get_class($reader);
208
$classType = explode('_',$className);
210
return array_pop($classType);
211
} // function identify()
214
* Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
218
* @param string $pFileName
219
* @return PHPExcel_Reader_IReader
222
public static function createReaderForFile($pFilename) {
224
// First, lucky guess by inspecting file extension
225
$pathinfo = pathinfo($pFilename);
227
if (isset($pathinfo['extension'])) {
228
switch (strtolower($pathinfo['extension'])) {
230
$reader = self::createReader('Excel2007');
233
$reader = self::createReader('Excel5');
236
$reader = self::createReader('OOCalc');
239
$reader = self::createReader('SYLK');
242
$reader = self::createReader('Excel2003XML');
245
$reader = self::createReader('Gnumeric');
249
// We must not try to use CSV reader since it loads
250
// all files including Excel files etc.
256
// Let's see if we are lucky
257
if (isset($reader) && $reader->canRead($pFilename)) {
263
// If we reach here then "lucky guess" didn't give any result
265
// Try loading using self::$_autoResolveClasses
266
foreach (self::$_autoResolveClasses as $autoResolveClass) {
267
$reader = self::createReader($autoResolveClass);
268
if ($reader->canRead($pFilename)) {
273
} // function createReaderForFile()