mirror of
https://github.com/shuchkin/simplexlsxgen.git
synced 2023-08-10 21:12:59 +03:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
5a4c04025a | |||
982de475c9 | |||
e92d3a5284 | |||
ca5da99160 | |||
8a38813b12 | |||
72b9f420d7 | |||
08e2ae6892 | |||
6a098dd7e4 |
21
README.md
21
README.md
@ -1,4 +1,4 @@
|
||||
# SimpleXLSXGen class 0.9.12 (Official)
|
||||
# SimpleXLSXGen class 0.9.16 (Official)
|
||||
[<img src="https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.herokuapp.com%2Fshuchkin" />](https://www.patreon.com/shuchkin) [<img src="https://img.shields.io/github/license/shuchkin/simplexlsxgen" />](https://github.com/shuchkin/simplexlsxgen/blob/master/license.md) [<img src="https://img.shields.io/github/stars/shuchkin/simplexlsxgen" />](https://github.com/shuchkin/simplexlsxgen/stargazers) [<img src="https://img.shields.io/github/forks/shuchkin/simplexlsxgen" />](https://github.com/shuchkin/simplexlsxgen/network) [<img src="https://img.shields.io/github/issues/shuchkin/simplexlsxgen" />](https://github.com/shuchkin/simplexlsxgen/issues)
|
||||
|
||||
Export data to Excel XLSX file. PHP XLSX generator. No external tools and libraries.<br/>
|
||||
@ -40,15 +40,32 @@ $data = [
|
||||
['Integer', 123],
|
||||
['Float', 12.35],
|
||||
['Procent', '12%'],
|
||||
['Date','2020-05-20'],
|
||||
['Datetime', '2020-05-20 02:38:00'],
|
||||
['Date','2020-05-20'],
|
||||
['Time','02:38:00'],
|
||||
['String', 'See SimpleXLSXGen column autosize feature']
|
||||
];
|
||||
SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx');
|
||||
```
|
||||

|
||||
|
||||
### Debug
|
||||
```php
|
||||
ini_set('error_reporting', E_ALL );
|
||||
ini_set('display_errors', 1 );
|
||||
|
||||
$data = [
|
||||
['Debug', 123]
|
||||
]
|
||||
SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
|
||||
```
|
||||
|
||||
|
||||
## History
|
||||
v0.9.16 (2020-07-29) Fixed time detection in HH:MM:SS format
|
||||
v0.9.15 (2020-07-14) escape of shared strings for special chars in cells [#1](https://github.com/shuchkin/simplexlsxgen/issues/1) <br/>
|
||||
v0.9.14 (2020-05-31) fixed num2name A-Z,AA-AZ column names, thx Ertan Yusufoglu<br/>
|
||||
v0.9.13 (2020-05-21) if string more 160 chars, save as inlineStr<br/>
|
||||
v0.9.12 (2020-05-21) readme fixed<br/>
|
||||
v0.9.11 (2020-05-21) removed XML unimportant attributes<br/>
|
||||
v0.9.10 (2020-05-20) initial release
|
@ -117,7 +117,7 @@ class SimpleXLSXGen {
|
||||
return true;
|
||||
}
|
||||
public function download() {
|
||||
return $this->downloadAs( gmdate('YmdHi.xlsx') );
|
||||
return $this->downloadAs( gmdate('YmdHi') . '.xlsx' );
|
||||
}
|
||||
public function downloadAs( $filename ) {
|
||||
$fh = fopen('php://memory','wb');
|
||||
@ -181,7 +181,7 @@ class SimpleXLSXGen {
|
||||
}
|
||||
$COL[ $CUR_COL ] = max( mb_strlen( (string) $v ), $COL[ $CUR_COL ] );
|
||||
|
||||
$cname = $this->_num2name($CUR_COL).$CUR_ROW;
|
||||
$cname = $this->num2name($CUR_COL) . $CUR_ROW;
|
||||
|
||||
$ct = $cs = null;
|
||||
|
||||
@ -198,17 +198,20 @@ class SimpleXLSXGen {
|
||||
$cv = round( $m[1] / 100, 4 );
|
||||
$cs = 2; // [10] 0.00%
|
||||
} elseif ( preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d)$/', $v, $m ) ){
|
||||
$cv = $this->_date2excel($m[1],$m[2],$m[3]);
|
||||
$cv = $this->date2excel($m[1],$m[2],$m[3]);
|
||||
$cs = 3; // [14] mm-dd-yy
|
||||
} elseif ( preg_match('/^(\d\d):(\d\d):(\d\d)$/', $v, $m ) ){
|
||||
$cv = $this->_date2excel(0,0,0,$m[1],$m[2],$m[3]);
|
||||
$cv = $this->date2excel(0,0,0,$m[1],$m[2],$m[3]);
|
||||
$cs = 4; // [14] mm-dd-yy
|
||||
} elseif ( preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/', $v, $m ) ){
|
||||
$cv = $this->_date2excel($m[1],$m[2],$m[3],$m[4],$m[5],$m[6]);
|
||||
} elseif ( preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/', $v, $m ) ) {
|
||||
$cv = $this->date2excel( $m[1], $m[2], $m[3], $m[4], $m[5], $m[6] );
|
||||
$cs = 5; // [22] m/d/yy h:mm
|
||||
} elseif ( mb_strlen( $v ) > 160 ) {
|
||||
$ct = 'inlineStr';
|
||||
$cv = str_replace(['&','<','>'],['&','<','>'], $v);
|
||||
} else {
|
||||
$ct = 's'; // shared string
|
||||
$v = htmlentities($v, ENT_QUOTES);
|
||||
$v = str_replace(['&','<','>'],['&','<','>'], $v);
|
||||
$cv = array_search( $v, $SI, true );
|
||||
if ( $cv === false ) {
|
||||
$SI[] = $v;
|
||||
@ -221,7 +224,8 @@ class SimpleXLSXGen {
|
||||
continue;
|
||||
}
|
||||
|
||||
$row .= '<c r="' . $cname . '"'.($ct ? ' t="'.$ct.'"' : '').($cs ? ' s="'.$cs.'"' : '').'><v>' . $cv . "</v></c>\r\n";
|
||||
$row .= '<c r="' . $cname . '"'.($ct ? ' t="'.$ct.'"' : '').($cs ? ' s="'.$cs.'"' : '').'>'
|
||||
.($ct === 'inlineStr' ? '<is><t>'.$cv.'</t></is>' : '<v>' . $cv . '</v>')."</c>\r\n";
|
||||
}
|
||||
$ROWS[] = $row . "</row>\r\n";
|
||||
}
|
||||
@ -229,7 +233,7 @@ class SimpleXLSXGen {
|
||||
// $COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" bestFit="true" customWidth="true" />';
|
||||
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" />';
|
||||
}
|
||||
$REF = 'A1:'.$this->_num2name(count($COLS)).$CUR_ROW;
|
||||
$REF = 'A1:'.$this->num2name(count($COLS)) . $CUR_ROW;
|
||||
} else {
|
||||
$COLS[] = '<col min="1" max="1" bestFit="1" />';
|
||||
$ROWS[] = '<row r="1"><c r="A1" t="s"><v>0</v></c></row>';
|
||||
@ -343,17 +347,22 @@ class SimpleXLSXGen {
|
||||
|
||||
return true;
|
||||
}
|
||||
private function _num2name($num) {
|
||||
$numeric = ($num-1) % 26;
|
||||
public function num2name($num) {
|
||||
$numeric = ($num - 1) % 26;
|
||||
$letter = chr( 65 + $numeric );
|
||||
$num2 = (int) ( ($num-1) / 26 );
|
||||
if ( $num2 > 0 ) {
|
||||
return $this->_num2name( $num2 - 1 ) . $letter;
|
||||
return $this->num2name( $num2 ) . $letter;
|
||||
}
|
||||
|
||||
return $letter;
|
||||
}
|
||||
private function _date2excel($year, $month, $day, $hours=0, $minutes=0, $seconds=0) {
|
||||
public function date2excel($year, $month, $day, $hours=0, $minutes=0, $seconds=0) {
|
||||
$excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
|
||||
|
||||
if ( $year === 0 ) {
|
||||
return $excelTime;
|
||||
}
|
||||
|
||||
// self::CALENDAR_WINDOWS_1900
|
||||
$excel1900isLeapYear = True;
|
||||
if (((int)$year === 1900) && ($month <= 2)) { $excel1900isLeapYear = False; }
|
||||
@ -371,8 +380,6 @@ class SimpleXLSXGen {
|
||||
$decade = substr($year,2,2);
|
||||
$excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;
|
||||
|
||||
$excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
|
||||
|
||||
return (float) $excelDate + $excelTime;
|
||||
}
|
||||
}
|
||||
return (float) $excelDate + $excelTime;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user