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