1: <?php
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: if (!defined('PHPEXCEL_ROOT')) {
31: 32: 33:
34: define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35: require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
36: }
37:
38: 39: 40: 41: 42: 43: 44:
45: class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
46: {
47: 48: 49: 50: 51: 52:
53: private $_inputEncoding = 'UTF-8';
54:
55: 56: 57: 58: 59: 60:
61: private $_delimiter = ',';
62:
63: 64: 65: 66: 67: 68:
69: private $_enclosure = '"';
70:
71: 72: 73: 74: 75: 76:
77: private $_lineEnding = PHP_EOL;
78:
79: 80: 81: 82: 83: 84:
85: private $_sheetIndex = 0;
86:
87: 88: 89: 90: 91: 92:
93: private $_contiguous = false;
94:
95: 96: 97: 98: 99:
100: private $_contiguousRow = -1;
101:
102:
103: 104: 105:
106: public function __construct() {
107: $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
108: }
109:
110: 111: 112: 113: 114:
115: protected function _isValidFormat()
116: {
117: return TRUE;
118: }
119:
120: 121: 122: 123: 124:
125: public function setInputEncoding($pValue = 'UTF-8')
126: {
127: $this->_inputEncoding = $pValue;
128: return $this;
129: }
130:
131: 132: 133: 134: 135:
136: public function getInputEncoding()
137: {
138: return $this->_inputEncoding;
139: }
140:
141: 142: 143: 144:
145: protected function _skipBOM()
146: {
147: rewind($this->_fileHandle);
148:
149: switch ($this->_inputEncoding) {
150: case 'UTF-8':
151: fgets($this->_fileHandle, 4) == "\xEF\xBB\xBF" ?
152: fseek($this->_fileHandle, 3) : fseek($this->_fileHandle, 0);
153: break;
154: case 'UTF-16LE':
155: fgets($this->_fileHandle, 3) == "\xFF\xFE" ?
156: fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
157: break;
158: case 'UTF-16BE':
159: fgets($this->_fileHandle, 3) == "\xFE\xFF" ?
160: fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
161: break;
162: case 'UTF-32LE':
163: fgets($this->_fileHandle, 5) == "\xFF\xFE\x00\x00" ?
164: fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
165: break;
166: case 'UTF-32BE':
167: fgets($this->_fileHandle, 5) == "\x00\x00\xFE\xFF" ?
168: fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
169: break;
170: default:
171: break;
172: }
173: }
174:
175: 176: 177: 178: 179: 180:
181: public function listWorksheetInfo($pFilename)
182: {
183:
184: $this->_openFile($pFilename);
185: if (!$this->_isValidFormat()) {
186: fclose ($this->_fileHandle);
187: throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
188: }
189: $fileHandle = $this->_fileHandle;
190:
191:
192: $this->_skipBOM();
193:
194: $escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure );
195:
196: $worksheetInfo = array();
197: $worksheetInfo[0]['worksheetName'] = 'Worksheet';
198: $worksheetInfo[0]['lastColumnLetter'] = 'A';
199: $worksheetInfo[0]['lastColumnIndex'] = 0;
200: $worksheetInfo[0]['totalRows'] = 0;
201: $worksheetInfo[0]['totalColumns'] = 0;
202:
203:
204: while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
205: $worksheetInfo[0]['totalRows']++;
206: $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
207: }
208:
209: $worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
210: $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
211:
212:
213: fclose($fileHandle);
214:
215: return $worksheetInfo;
216: }
217:
218: 219: 220: 221: 222: 223: 224:
225: public function load($pFilename)
226: {
227:
228: $objPHPExcel = new PHPExcel();
229:
230:
231: return $this->loadIntoExisting($pFilename, $objPHPExcel);
232: }
233:
234: 235: 236: 237: 238: 239: 240: 241:
242: public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
243: {
244: $lineEnding = ini_get('auto_detect_line_endings');
245: ini_set('auto_detect_line_endings', true);
246:
247:
248: $this->_openFile($pFilename);
249: if (!$this->_isValidFormat()) {
250: fclose ($this->_fileHandle);
251: throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
252: }
253: $fileHandle = $this->_fileHandle;
254:
255:
256: $this->_skipBOM();
257:
258:
259: while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
260: $objPHPExcel->createSheet();
261: }
262: $sheet = $objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
263:
264: $escapeEnclosures = array( "\\" . $this->_enclosure,
265: $this->_enclosure . $this->_enclosure
266: );
267:
268:
269: $currentRow = 1;
270: if ($this->_contiguous) {
271: $currentRow = ($this->_contiguousRow == -1) ? $sheet->getHighestRow(): $this->_contiguousRow;
272: }
273:
274:
275: while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
276: $columnLetter = 'A';
277: foreach($rowData as $rowDatum) {
278: if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
279:
280: $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
281:
282:
283: if ($this->_inputEncoding !== 'UTF-8') {
284: $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
285: }
286:
287:
288: $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
289: }
290: ++$columnLetter;
291: }
292: ++$currentRow;
293: }
294:
295:
296: fclose($fileHandle);
297:
298: if ($this->_contiguous) {
299: $this->_contiguousRow = $currentRow;
300: }
301:
302: ini_set('auto_detect_line_endings', $lineEnding);
303:
304:
305: return $objPHPExcel;
306: }
307:
308: 309: 310: 311: 312:
313: public function getDelimiter() {
314: return $this->_delimiter;
315: }
316:
317: 318: 319: 320: 321: 322:
323: public function setDelimiter($pValue = ',') {
324: $this->_delimiter = $pValue;
325: return $this;
326: }
327:
328: 329: 330: 331: 332:
333: public function getEnclosure() {
334: return $this->_enclosure;
335: }
336:
337: 338: 339: 340: 341: 342:
343: public function setEnclosure($pValue = '"') {
344: if ($pValue == '') {
345: $pValue = '"';
346: }
347: $this->_enclosure = $pValue;
348: return $this;
349: }
350:
351: 352: 353: 354: 355:
356: public function getLineEnding() {
357: return $this->_lineEnding;
358: }
359:
360: 361: 362: 363: 364: 365:
366: public function setLineEnding($pValue = PHP_EOL) {
367: $this->_lineEnding = $pValue;
368: return $this;
369: }
370:
371: 372: 373: 374: 375:
376: public function getSheetIndex() {
377: return $this->_sheetIndex;
378: }
379:
380: 381: 382: 383: 384: 385:
386: public function setSheetIndex($pValue = 0) {
387: $this->_sheetIndex = $pValue;
388: return $this;
389: }
390:
391: 392: 393: 394: 395:
396: public function setContiguous($contiguous = FALSE)
397: {
398: $this->_contiguous = (bool) $contiguous;
399: if (!$contiguous) {
400: $this->_contiguousRow = -1;
401: }
402:
403: return $this;
404: }
405:
406: 407: 408: 409: 410:
411: public function getContiguous() {
412: return $this->_contiguous;
413: }
414:
415: }
416: