XlsxParser.php
2.31 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
<?php
/**
* Created by PhpStorm.
* User: Tsurkanov
* Date: 21.10.2015
* Time: 15:44
*/
namespace yii\multiparser;
use common\components\CustomVarDamp;
class XlsxParser extends Parser {
public $path_for_extract_files = '';
public function setup()
{
parent::setup();
if ( $this->path_for_extract_files == '' ) {
$this->path_for_extract_files = sys_get_temp_dir();
}
}
public function read()
{
//$this->extractFiles();
CustomVarDamp::dumpAndDie($this->parse());
// return $this->parse();
}
protected function extractFiles ()
{
$zip = new \ZipArchive;
if ( $zip->open( $this->file->getPathname() ) === TRUE ) {
$zip->extractTo( $this->path_for_extract_files );
$zip->close();
} else {
throw new \Exception('Ошибка чтения xlsx файла');
}
}
protected function parse ()
{
$result = [];
// while ($sheet = @readdir($this->file)) {
foreach ( glob($this->path_for_extract_files . '/xl/worksheets/*.xml' ) as $sheet ) {
//проходим по всем файлам из директории /xl/worksheets/
//CustomVarDamp::dumpAndDie($sheet);
if ($sheet != "." && $sheet != ".." && $sheet != '_rels') {
$xml = simplexml_load_file( $sheet );
//по каждой строке
$row = 0;
foreach ( $xml->sheetData->row as $item ) {
$result[$sheet][$row] = array();
//по каждой ячейке строки
$cell = 0;
foreach ( $item as $child ) {
// $attr = $child->attributes();
if( isset($child->v) ) {
$value = (string)$child->v;
}else{
$value = false;
}
// $result[$sheet][$row][$cell] = isset($attr['t']) ? $sharedStringsArr[$value] : $value;
$result[$sheet][$row][$cell] = $value;
$cell++;
}
$row++;
}
}
}
return $result;
}
}