1
0
mirror of https://github.com/shuchkin/simplexlsxgen.git synced 2023-08-10 21:12:59 +03:00

Added PHP Datetime object values in a cells

This commit is contained in:
Sergey Shuchkin 2021-02-26 21:39:13 +06:00
parent 21db9eca96
commit d1ccfc6f17
3 changed files with 14 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# SimpleXLSXGen class 0.9.24 (Official) # SimpleXLSXGen class 0.9.25 (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) [<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/> Export data to Excel XLSX file. PHP XLSX generator. No external tools and libraries.<br/>
@ -40,8 +40,9 @@ $data = [
['Datetime', '2020-05-20 02:38:00'], ['Datetime', '2020-05-20 02:38:00'],
['Date','2020-05-20'], ['Date','2020-05-20'],
['Time','02:38:00'], ['Time','02:38:00'],
['Datetime PHP', new DateTime('2021-02-06 21:07:00')],
['String', 'Long UTF-8 String in autoresized column'], ['String', 'Long UTF-8 String in autoresized column'],
['Disable Type Detection', "\0".'2020-10-04 16:02:00'] ['RAW string', "\0".'2020-10-04 16:02:00']
]; ];
SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx'); SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx');
``` ```
@ -72,6 +73,7 @@ SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
## History ## History
v0.9.25 (2021-02-26) Added PHP Datetime object values in a cells
v0.9.24 (2021-02-26) * Percent<br/> v0.9.24 (2021-02-26) * Percent<br/>
v0.9.23 (2021-01-25) Fix local floats in XML<br/> v0.9.23 (2021-01-25) Fix local floats in XML<br/>
v0.9.22 (2020-11-04) Added multiple sheets support, thx [Savino59](https://github.com/Savino59), class ready for extend now<br/> v0.9.22 (2020-11-04) Added multiple sheets support, thx [Savino59](https://github.com/Savino59), class ready for extend now<br/>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -351,9 +351,6 @@ class SimpleXLSXGen {
if ( $v === null || $v === '' ) { if ( $v === null || $v === '' ) {
continue; continue;
} }
$vl = mb_strlen( (string) $v );
$COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] );
$cname = $this->num2name($CUR_COL) . $CUR_ROW; $cname = $this->num2name($CUR_COL) . $CUR_ROW;
@ -361,6 +358,8 @@ class SimpleXLSXGen {
if ( is_string($v) ) { if ( is_string($v) ) {
$vl = mb_strlen( $v );
if ( $v === '0' || preg_match( '/^[-+]?[1-9]\d{0,14}$/', $v ) ) { // Integer as General if ( $v === '0' || preg_match( '/^[-+]?[1-9]\d{0,14}$/', $v ) ) { // Integer as General
$cv = ltrim( $v, '+' ); $cv = ltrim( $v, '+' );
if ( $vl > 10 ) { if ( $vl > 10 ) {
@ -412,13 +411,21 @@ class SimpleXLSXGen {
} }
} }
} elseif ( is_int( $v ) ) { } elseif ( is_int( $v ) ) {
$vl = mb_strlen( (string) $v );
$cv = $v; $cv = $v;
} elseif ( is_float( $v ) ) { } elseif ( is_float( $v ) ) {
$vl = mb_strlen( (string) $v );
$cv = $v; $cv = $v;
} elseif ( $v instanceof DateTime ) {
$vl = 16;
$cv = $this->date2excel( $v->format('Y'), $v->format('m'), $v->format('d'), $v->format('H'), $v->format('i'), $v->format('s') );
$cs = 6; // [22] m/d/yy h:mm
} else { } else {
continue; continue;
} }
$COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] );
$row .= '<c r="' . $cname . '"'.($ct ? ' t="'.$ct.'"' : '').($cs ? ' s="'.$cs.'"' : '').'>' $row .= '<c r="' . $cname . '"'.($ct ? ' t="'.$ct.'"' : '').($cs ? ' s="'.$cs.'"' : '').'>'
.($ct === 'inlineStr' ? '<is><t>'.$cv.'</t></is>' : '<v>' . $cv . '</v>')."</c>\r\n"; .($ct === 'inlineStr' ? '<is><t>'.$cv.'</t></is>' : '<v>' . $cv . '</v>')."</c>\r\n";
} }