XlsParser.php
1.76 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
<?php
/**
*/
namespace common\components\parsers;
/**
* Class XlsParser
* @package yii\multiparser
* @todo - перевести на анг. яз.
*/
class XlsParser extends TableParser
{
// экземпляр класса Spreadsheet_Excel_Reader - для чтения листов Excel
protected $reader;
// строка кодировки
protected $encoding = 'CP1251';
/**
* @var int - номер листа с которого будет происходить чтение
*/
public $active_sheet = 0;
/**
* метод устанавливает настройки конвертера и ридера
*/
public function setup()
{
parent::setup();
$this->setupReader();
}
/**
* устанавливает ридер и его параметры
*/
protected function setupReader()
{
require_once 'ExcelReader/reader.php';
$this->reader = new \Spreadsheet_Excel_Reader();
$this->reader->setOutputEncoding( $this->encoding );
$this->reader->read( $this->file_path );
}
public function read()
{
parent::read();
$this->cleanUp();
return $this->result;
}
protected function readRow( )
{
$this->row = [];
$current_sheet = $this->reader->sheets[ $this->active_sheet ];
for ( $j = 1; $j <= $current_sheet['numCols']; $j++ ) {
if ( isset( $current_sheet['cells'][ $this->current_row_number ][$j]) )
$this->row[] = $current_sheet['cells'][ $this->current_row_number ][$j];
}
}
protected function isEmptyColumn( $val ){
return $val == '';
}
protected function setResult( ){
$this->result[] = $this->row;
}
}