Repository navigation
Expand file tree
/
Copy pathSimpleExcel.php
More file actions
235 lines (214 loc) · 7.42 KB
/
Copy pathSimpleExcel.php
File metadata and controls
235 lines (214 loc) · 7.42 KB
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
<?php
/**
* Simple Excel
*
* A PHP library with simplistic approach
* Easily parse/convert/write between Microsoft Excel XML/CSV/TSV/HTML/JSON/etc formats
*
* Copyright (c) 2011-2013 Faisalman <fyzlman@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Faisalman
* @copyright 2011-2013 (c) Faisalman
* @license http://www.opensource.org/licenses/mit-license
* @link http://github.com/faisalman/simple-excel-php
* @package SimpleExcel
* @version 0.4.0-beta
*/
namespace SimpleExcel;
use SimpleExcel\Enums\SimpleExcelException;
use SimpleExcel\Spreadsheet\Workbook;
use SimpleExcel\Spreadsheet\Worksheet;
if (!class_exists('Composer\\Autoload\\ClassLoader', false)){
// autoload all interfaces & classes
spl_autoload_register(function($class_name){
if($class_name != 'SimpleExcel') require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, substr($class_name, strlen('SimpleExcel\\'))).'.php');
});
}
/**
* SimpleExcel main class
*
* @author Faisalman
* @package SimpleExcel
*/
class SimpleExcel
{
/**
* @var IParser
*/
protected $parser;
/**
* @var string
*/
protected $parserType;
/**
* @var array
*/
protected $validParserTypes;
/**
* @var array
*/
protected $validWriterTypes;
/**
* @var IWriter
*/
protected $writer;
/**
* @var string
*/
protected $writerType;
/**
* @var Workbook
*/
public $workbook;
/**
* SimpleExcel constructor method
*
* @param string $filetype Set the filetype of the file
*/
public function __construct ($filetype = NULL) {
$this->workbook = new Workbook();
$this->validParserTypes = array('XML', 'CSV', 'TSV', 'HTML', 'JSON', 'XLSX');
$this->validWriterTypes = array('XML', 'CSV', 'TSV', 'HTML', 'JSON');
if (isset($filetype)) {
$this->setParserType($filetype);
$this->setWriterType($filetype);
}
}
/**
* Export data as file
*
* @param string $target Where to write the file
* @param string $filetype Type of the file to be written
* @param string $options Options
* @throws Exception If filetype is not supported
* @throws Exception If error writing file
*/
public function exportFile ($target, $fileType, $options = NULL) {
$this->setWriterType($fileType);
$this->writer->exportFile($target, $options);
}
/**
* Get specified worksheet
*
* @param int $index Worksheet index
* @return Worksheet
* @throws Exception If worksheet with specified index is not found
*/
public function getWorksheet ($index = 1) {
return $this->workbook->getWorksheet($index);
}
/**
* Get all worksheets
*
* @return array
*/
public function getWorksheets () {
return $this->workbook->getWorksheets();
}
/**
* Insert a worksheet
*
* @param Worksheet $worksheet Worksheet to be inserted
*/
public function insertWorksheet (Worksheet $worksheet = NULL) {
$this->workbook->insertWorksheet($worksheet);
}
/**
* Load file to parser
*
* @param string $filepath Path to file
* @param string $filetype Set the filetype of the file which will be parsed
* @param string $options Options
* @throws Exception If filetype is not supported
* @throws Exception If file being loaded doesn't exist
* @throws Exception If file extension doesn't match
* @throws Exception If error reading the file
*/
public function loadFile ($filePath, $fileType, $options = NULL) {
$this->setParserType($fileType);
$this->parser->loadFile($filePath, $options);
}
/**
* Load string to parser
*
* @param string $filepath Path to file
* @param string $filetype Set the filetype of the file which will be parsed
* @throws Exception If filetype is not supported
*/
public function loadString ($string, $fileType) {
$this->setParserType($fileType);
$this->parser->loadString($string);
}
/**
* Remove a worksheet
*
* @param int $index Worksheet index to be removed
*/
public function removeWorksheet ($index) {
$this->workbook->removeWorksheet($index);
}
/**
* Construct a SimpleExcel Parser
*
* @param string $filetype Set the filetype of the file which will be parsed (XML/CSV/TSV/HTML/JSON)
* @throws Exception If filetype is not supported
*/
protected function setParserType($filetype){
$filetype = strtoupper($filetype);
if ($filetype != $this->parserType) {
if(!in_array($filetype, $this->validParserTypes)){
throw new \Exception('Filetype '.$filetype.' is not supported', SimpleExcelException::FILETYPE_NOT_SUPPORTED);
}
$parser_class = 'SimpleExcel\\Parser\\'.$filetype.'Parser';
$this->parser = new $parser_class($this->workbook);
$this->parserType = $filetype;
}
}
/**
* Construct a SimpleExcel Writer
*
* @param string $filetype Set the filetype of the file which will be written
* @throws Exception If filetype is not supported
*/
protected function setWriterType ($filetype) {
$filetype = strtoupper($filetype);
if ($filetype != $this->writerType) {
if(!in_array($filetype, $this->validWriterTypes)) {
throw new \Exception('Filetype '.$filetype.' is not supported', SimpleExcelException::FILETYPE_NOT_SUPPORTED);
}
$writer_class = 'SimpleExcel\\Writer\\'.$filetype.'Writer';
$this->writer = new $writer_class($this->workbook);
$this->writerType = $filetype;
}
}
/**
* Get data as string
*
* @param string $filetype Document format for the string to be returned
* @param string $options Options
* @return string
* @throws Exception If filetype is not supported
*/
public function toString ($filetype, $options = NULL) {
$this->setWriterType($filetype);
return $this->writer->toString($options);
}
}