Blame view

lib/XlsxParser.php 4.58 KB
8e128526   Mihail   add xlsx parser
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Tsurkanov
   * Date: 21.10.2015
   * Time: 15:44
   */
  
d0261fd1   Mihail   fixed namespace i...
9
  namespace yii\multiparser;
8e128526   Mihail   add xlsx parser
10
11
  
  
8e128526   Mihail   add xlsx parser
12
13
14
15
  
  
  /**
   * Class XlsxParser
d0261fd1   Mihail   fixed namespace i...
16
   * @package yii\multiparser
8e128526   Mihail   add xlsx parser
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
   */
  class XlsxParser extends TableParser {
  
      /**
       * @var string - путь куда будут распаковываться файлы, если не указанно - во временный каталог сервера
       */
      public $path_for_extract_files = '';
  
  
      /**
       * @var int - если указано то считывание будет производиться с этого листа, иначе со всех листов
       * при чтении со всех листов - выходной массив будет иметь номера листов первыми элементами
       */
      public $active_sheet = 0;
  
      protected $strings_arr = [];
      protected $sheets_arr = [];
  
      protected $current_node;
      protected $current_sheet;
  
      public function setup()
      {
  
          parent::setup();
  
          if ( $this->path_for_extract_files == '' ) {
              $this->path_for_extract_files = sys_get_temp_dir();
          }
      }
  
  
      public function read()
      {
8e128526   Mihail   add xlsx parser
51
  
42a252c2   Mihail   fixed issue with ...
52
          $this->extractFiles();
8e128526   Mihail   add xlsx parser
53
54
55
56
57
          $this->readSheets();
          $this->readStrings();
  
          foreach ( $this->sheets_arr  as $sheet ) {
              //проходим по всем файлам из директории /xl/worksheets/
42a252c2   Mihail   fixed issue with ...
58
  
8e128526   Mihail   add xlsx parser
59
60
61
62
63
64
              $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml';
              if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) {
  
                  $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" );
                  $this->current_node = $xml->sheetData->row;
                  $this->current_node->rewind();
8e128526   Mihail   add xlsx parser
65
  
42a252c2   Mihail   fixed issue with ...
66
                  parent::read();
8e128526   Mihail   add xlsx parser
67
68
69
70
  
              }
  
          }
42a252c2   Mihail   fixed issue with ...
71
         return $this->$result;
8e128526   Mihail   add xlsx parser
72
73
74
75
      }
  
      protected function extractFiles ()
      {
8e128526   Mihail   add xlsx parser
76
77
          $zip = new \ZipArchive;
          if ( $zip->open( $this->file->getPathname() ) === TRUE ) {
42a252c2   Mihail   fixed issue with ...
78
              $zip->extractTo( $this->path_for_extract_files );
8e128526   Mihail   add xlsx parser
79
80
81
82
83
84
85
86
87
              $zip->close();
          } else {
              throw new \Exception( 'Ошибка чтения xlsx файла' );
          }
      }
  
      protected function readSheets ()
      {
          if ( $this->active_sheet ) {
42a252c2   Mihail   fixed issue with ...
88
              $this->sheets_arr[ $this->active_sheet ] = 'Sheet' . $this->active_sheet;
8e128526   Mihail   add xlsx parser
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
              return;
          }
  
          $xml = simplexml_load_file( $this->path_for_extract_files . '/xl/workbook.xml' );
          foreach ( $xml->sheets->children() as $sheet ) {
              $sheet_name = '';
              $sheet_id = 0;
              $attr = $sheet->attributes();
              foreach ( $attr as $name => $value ) {
                  if ($name == 'name')
                      $sheet_name = (string)$value;
  
                  if ($name == 'sheetId')
                      $sheet_id = $value;
  
              }
              if ( $sheet_name && $sheet_id ) {
                  $this->sheets_arr[$sheet_name] = 'Sheet' . $sheet_id;
              }
  //
          }
      }
  
      protected function readStrings ()
      {
          $xml = simplexml_load_file( $this->path_for_extract_files . '/xl/sharedStrings.xml' );
          foreach ( $xml->children() as $item ) {
              $this->strings_arr[] = (string)$item->t;
          }
      }
  
  
8e128526   Mihail   add xlsx parser
121
122
      protected function readRow ( )
      {
8e128526   Mihail   add xlsx parser
123
          $node = $this->current_node->getChildren();
42a252c2   Mihail   fixed issue with ...
124
125
  
          foreach ( $node as $child ) {
8e128526   Mihail   add xlsx parser
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
              $attr = $child->attributes();
  
              if( isset($child->v) ) {
                  $value = (string)$child->v;
              }else{
                  $value = '';
              }
              if ( isset( $attr['t'] ) ) {
                //  $this->result_arr[$sheet][$current_row][$cell] =  $this->strings_arr[ $value ];
                  $this->row[] =  $this->strings_arr[ $value ];
              }else{
               //   $this->result_arr[$sheet][$current_row][$cell] =  $value;
                  $this->row[] =  $value;
              }
  
          }
8e128526   Mihail   add xlsx parser
142
          $this->current_node->next();
8e128526   Mihail   add xlsx parser
143
144
145
146
147
      }
  
      protected  function isEmptyRow(){
  
          $is_empty = false;
8e128526   Mihail   add xlsx parser
148
149
150
151
152
153
154
155
  
          if ( !count( $this->row ) || !$this->current_node->valid() ) {
              return true;
          }
  
          $j = 0;
          for ($i = 1; $i <= count( $this->row ); $i++) {
  
42a252c2   Mihail   fixed issue with ...
156
              if ( isset($this->row[$i - 1]) && $this->isEmptyColumn( $this->row[$i - 1] ) ) {
8e128526   Mihail   add xlsx parser
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
                  $j++;
              }
  
              if ( $j >= $this->min_column_quantity ) {
                  $is_empty = true;
                  break;
              }
          }
  
          return $is_empty;
      }
  
      protected  function isEmptyColumn( $val ){
          return $val == '';
      }
8e128526   Mihail   add xlsx parser
172
  }