1
|
|
|
<?php |
2
|
|
|
namespace Ellumilel\Helpers; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class PHPExcelHelper |
6
|
|
|
* @package Ellumilel\Helpers |
7
|
|
|
*/ |
8
|
|
|
class ExcelHelper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @link http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx |
12
|
|
|
* XFE1048577 |
13
|
|
|
*/ |
14
|
|
|
const EXCEL_MAX_ROW = 1048576; |
15
|
|
|
const EXCEL_MAX_RANGE = 2147483647; |
16
|
|
|
const EXCEL_MAX_COL = 16384; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $dateInput |
20
|
|
|
* |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public static function convertDateTime($dateInput) |
24
|
|
|
{ |
25
|
|
|
$epoch = 1900; |
26
|
|
|
$norm = 300; |
27
|
|
|
$year = $month = $day = $offset = $seconds = 0; |
28
|
|
|
$dateTime = $dateInput; |
29
|
|
|
if (preg_match("/(\d{4})\-(\d{2})\-(\d{2})/", $dateTime, $matches)) { |
30
|
|
|
$year = $matches[1]; |
31
|
|
|
$month = $matches[2]; |
32
|
|
|
$day = $matches[3]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $dateTime, $matches)) { |
36
|
|
|
$seconds = ($matches[1] * 60 * 60 + $matches[2] * 60 + $matches[3]) / 86400; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ("$year-$month-$day" == '1899-12-31' || "$year-$month-$day" == '1900-01-00') { |
40
|
|
|
return $seconds; |
41
|
|
|
} |
42
|
|
|
if ("$year-$month-$day" == '1900-02-29') { |
43
|
|
|
return 60 + $seconds; |
44
|
|
|
} |
45
|
|
|
$range = $year - $epoch; |
46
|
|
|
// check leapDay |
47
|
|
|
$leap = (new \DateTime($dateInput))->format('L'); |
48
|
|
|
$mDays = [31, ($leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
49
|
|
|
if (self::checkEpoch($year, $month, $day, $mDays[$month - 1])) { |
50
|
|
|
return 0; |
51
|
|
|
} |
52
|
|
|
$days = $day + ($range * 365) + array_sum(array_slice($mDays, 0, $month - 1)); |
53
|
|
|
$days += intval(($range) / 4) - intval(($range + $offset) / 100); |
54
|
|
|
$days += intval(($range + $offset + $norm) / 400) - intval($leap); |
55
|
|
|
if ($days > 59) { |
56
|
|
|
$days++; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $days + $seconds; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $year |
64
|
|
|
* @param $month |
65
|
|
|
* @param $day |
66
|
|
|
* @param $dayCheck |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
private function checkEpoch($year, $month, $day, $dayCheck) |
71
|
|
|
{ |
72
|
|
|
if (($year < 1900 || $year > 9999) || ($month < 1 || $month > 12) || $day < 1 || $day > $dayCheck) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $val |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public static function xmlspecialchars($val) |
85
|
|
|
{ |
86
|
|
|
return str_replace("'", "'", htmlspecialchars($val)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $rowNumber |
91
|
|
|
* @param int $columnNumber |
92
|
|
|
* |
93
|
|
|
* @return string Cell label/coordinates (A1, C3, AA42) |
94
|
|
|
*/ |
95
|
|
|
public static function xlsCell($rowNumber, $columnNumber) |
96
|
|
|
{ |
97
|
|
|
$n = $columnNumber; |
98
|
|
|
for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) { |
99
|
|
|
$r = chr($n % 26 + 0x41).$r; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $r.($rowNumber + 1); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @link https://msdn.microsoft.com/ru-RU/library/aa365247%28VS.85%29.aspx |
107
|
|
|
* |
108
|
|
|
* @param string $filename |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public static function checkFilename($filename) |
113
|
|
|
{ |
114
|
|
|
$invalidCharacter = array_merge( |
115
|
|
|
array_map('chr', range(0, 31)), |
116
|
|
|
['<', '>', '?', '"', ':', '|', '\\', '/', '*', '&'] |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return str_replace($invalidCharacter, '', $filename); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @todo check escaping |
124
|
|
|
* |
125
|
|
|
* @param string $cellFormat |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public static function escapeCellFormat($cellFormat) |
130
|
|
|
{ |
131
|
|
|
$ignoreUntil = ''; |
132
|
|
|
$escaped = ''; |
133
|
|
|
for ($i = 0, $ix = strlen($cellFormat); $i < $ix; $i++) { |
134
|
|
|
$c = $cellFormat[$i]; |
135
|
|
|
if ($ignoreUntil == '' && $c == '[') { |
136
|
|
|
$ignoreUntil = ']'; |
137
|
|
|
} else { |
138
|
|
|
if ($ignoreUntil == '' && $c == '"') { |
139
|
|
|
$ignoreUntil = '"'; |
140
|
|
|
} else { |
141
|
|
|
if ($ignoreUntil == $c) { |
142
|
|
|
$ignoreUntil = ''; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
if ($ignoreUntil == '' && |
147
|
|
|
($c == ' ' || $c == '-' || $c == '(' || $c == ')') && |
148
|
|
|
($i == 0 || $cellFormat[$i - 1] != '_') |
149
|
|
|
) { |
150
|
|
|
$escaped .= "\\".$c; |
151
|
|
|
} else { |
152
|
|
|
$escaped .= $c; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $escaped; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|