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

6 Commits

Author SHA1 Message Date
40eb704eb2 1.0.23 2022-02-01 02:50:32 +06:00
4005e8cbd1 Merge pull request #48 from appel/patch-1
added a ; in the Debug section.
2021-11-28 20:43:00 +06:00
9d82a43fe6 added a ; in the Debug section. 2021-11-28 08:48:15 -05:00
9a555eef9b 1.0.22 2021-10-29 03:37:04 +06:00
79ffbb5e30 1.0.21 2021-09-20 00:51:25 +06:00
868cf8edc0 1.0.21 2021-09-03 22:12:48 +06:00
3 changed files with 31 additions and 43 deletions

View File

@ -1,31 +1,33 @@
# Changelog # Changelog
## 1.0.20 (2021-07-29) ## 1.0.23 (2022-02-01)
* fixed dates if year < 1900 and time only cells, thx [fapth](https://github.com/shuchkin/simplexlsxgen/issues/51)
## 1.0.22 (2021-10-29)
* Escape \x00 and \x0B (vertical tab)
## 1.0.21 (2021-09-03)
* Fixed saveAs / downloadAs / etc methods more than once
## 1.0.20 (2021-07-29)
* Fixed sheet names duplicates (Page, Page (1), Page (2)...) * Fixed sheet names duplicates (Page, Page (1), Page (2)...)
## 1.0.19 (2021-07-28) ## 1.0.19 (2021-07-28)
* Fixed sheet names duplicates * Fixed sheet names duplicates
## 1.0.18 (2021-07-28) ## 1.0.18 (2021-07-28)
* Fixed email regex * Fixed email regex
## 1.0.17 (2021-07-28) ## 1.0.17 (2021-07-28)
* Fixed &quot; and &amp; in sheets names * Fixed &quot; and &amp; in sheets names
## 1.0.16 (2021-07-01) ## 1.0.16 (2021-07-01)
* Fixed &quot;&amp;&quot; in hyperlinks * Fixed &quot;&amp;&quot; in hyperlinks
## 1.0.15 (2021-06-22) ## 1.0.15 (2021-06-22)
* Fixed *mailto* hyperlinks detection * Fixed *mailto* hyperlinks detection
## 1.0.14 (2021-06-08) ## 1.0.14 (2021-06-08)
* Added *mailto* hyperlinks support (thx Howard Martin) * Added *mailto* hyperlinks support (thx Howard Martin)
```php ```php
SimpleXLSXGen::fromArray([ SimpleXLSXGen::fromArray([
@ -33,86 +35,65 @@ SimpleXLSXGen::fromArray([
])->saveAs('test.xlsx'); ])->saveAs('test.xlsx');
``` ```
## 1.0.13 (2021-05-29) ## 1.0.13 (2021-05-29)
* Fixed hyperlinks in several sheets * Fixed hyperlinks in several sheets
* Added [Opencollective donation link](https://opencollective.com/simplexlsx) * Added [Opencollective donation link](https://opencollective.com/simplexlsx)
## 1.0.12 (2021-05-19) ## 1.0.12 (2021-05-19)
* Fixed hyperlink regex * Fixed hyperlink regex
## 1.0.11 (2021-05-14) ## 1.0.11 (2021-05-14)
* Fixed 0.00% format, thx [marcrobledo](https://github.com/shuchkin/simplexlsxgen/pull/34), more examples in README.md * Fixed 0.00% format, thx [marcrobledo](https://github.com/shuchkin/simplexlsxgen/pull/34), more examples in README.md
## 1.0.10 (2021-05-03) ## 1.0.10 (2021-05-03)
Stable release Stable release
* Added hyperlinks and minimal formatting * Added hyperlinks and minimal formatting
## 0.9.25 (2021-02-26) ## 0.9.25 (2021-02-26)
* Added PHP Datetime object values in a cells * Added PHP Datetime object values in a cells
## 0.9.24 (2021-02-26) ## 0.9.24 (2021-02-26)
* Percent support * Percent support
## 0.9.23 (2021-01-25) ## 0.9.23 (2021-01-25)
* Fix local floats in XML * Fix local floats in XML
## 0.9.22 (2020-11-04) ## 0.9.22 (2020-11-04)
* Added multiple sheets support, thx [Savino59](https://github.com/Savino59), class ready for extend now * Added multiple sheets support, thx [Savino59](https://github.com/Savino59), class ready for extend now
## 0.9.21 (2020-10-17) ## 0.9.21 (2020-10-17)
* Updated images * Updated images
## 0.9.20 (2020-10-04) ## 0.9.20 (2020-10-04)
* Disable type detection if string started with chr(0) * Disable type detection if string started with chr(0)
## 0.9.19 (2020-08-23) ## 0.9.19 (2020-08-23)
* Numbers like SKU right aligned now * Numbers like SKU right aligned now
## 0.9.18 (2020-08-22) ## 0.9.18 (2020-08-22)
* Fixed fast shared strings index * Fixed fast shared strings index
## 0.9.17 (2020-08-21) ## 0.9.17 (2020-08-21)
* Fixed real numbers in 123.45 format detection, fast shared strings index (thx fredriksundin) * Fixed real numbers in 123.45 format detection, fast shared strings index (thx fredriksundin)
## 0.9.16 (2020-07-29) ## 0.9.16 (2020-07-29)
* Fixed time detection in HH:MM:SS format * Fixed time detection in HH:MM:SS format
## 0.9.15 (2020-07-14) ## 0.9.15 (2020-07-14)
* Escape of shared strings for special chars in cells [#1](https://github.com/shuchkin/simplexlsxgen/issues/1) * Escape of shared strings for special chars in cells [#1](https://github.com/shuchkin/simplexlsxgen/issues/1)
## 0.9.14 (2020-05-31) ## 0.9.14 (2020-05-31)
* Fixed num2name A-Z,AA-AZ column names, thx Ertan Yusufoglu * Fixed num2name A-Z,AA-AZ column names, thx Ertan Yusufoglu
## 0.9.13 (2020-05-21) ## 0.9.13 (2020-05-21)
* If string more 160 chars, save as inlineStr * If string more 160 chars, save as inlineStr
## 0.9.12 (2020-05-21) ## 0.9.12 (2020-05-21)
* Readme fixed * Readme fixed
## 0.9.11 (2020-05-21) ## 0.9.11 (2020-05-21)
* Removed XML unimportant attributes * Removed XML unimportant attributes
## 0.9.10 (2020-05-20) ## 0.9.10 (2020-05-20)
* Initial release * Initial release

View File

@ -99,6 +99,7 @@ ini_set('display_errors', 1 );
$data = [ $data = [
['Debug', 123] ['Debug', 123]
] ];
SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx'); SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
``` ```

View File

@ -110,8 +110,7 @@ class SimpleXLSXGen {
// <si><t>Простой шаблон</t></si><si><t>Будем делать генератор</t></si> // <si><t>Простой шаблон</t></si><si><t>Будем делать генератор</t></si>
} }
public static function fromArray( array $rows, $sheetName = null ) { public static function fromArray( array $rows, $sheetName = null ) {
$xlsx = new static(); return (new static())->addSheet( $rows, $sheetName );
return $xlsx->addSheet( $rows, $sheetName );
} }
public function addSheet( array $rows, $name = null ) { public function addSheet( array $rows, $name = null ) {
@ -122,11 +121,11 @@ class SimpleXLSXGen {
} else { } else {
$names = []; $names = [];
foreach( $this->sheets as $sh ) { foreach( $this->sheets as $sh ) {
$names[ strtoupper( $sh['name']) ] = 1; $names[ mb_strtoupper( $sh['name']) ] = 1;
} }
for( $i = 0; $i < 100; $i++ ) { for( $i = 0; $i < 100; $i++ ) {
$new_name = ($i === 0) ? $name : $name .' ('.$i.')'; $new_name = ($i === 0) ? $name : $name .' ('.$i.')';
$NEW_NAME = strtoupper( $new_name ); $NEW_NAME = mb_strtoupper( $new_name );
if ( !isset( $names[ $NEW_NAME ]) ) { if ( !isset( $names[ $NEW_NAME ]) ) {
$name = $new_name; $name = $new_name;
break; break;
@ -136,7 +135,7 @@ class SimpleXLSXGen {
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => []]; $this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => []];
if ( is_array( $rows ) && isset( $rows[0] ) && is_array($rows[0]) ) { if ( isset( $rows[0] ) && is_array($rows[0]) ) {
$this->sheets[$this->curSheet]['rows'] = $rows; $this->sheets[$this->curSheet]['rows'] = $rows;
} else { } else {
$this->sheets[$this->curSheet]['rows'] = []; $this->sheets[$this->curSheet]['rows'] = [];
@ -249,8 +248,8 @@ class SimpleXLSXGen {
$this->SI[] = 'No Data'; $this->SI[] = 'No Data';
} }
$si_cnt = count($this->SI); $si_cnt = count($this->SI);
$this->SI = '<si><t>'.implode("</t></si>\r\n<si><t>", $this->SI).'</t></si>'; $si = '<si><t>'.implode("</t></si>\r\n<si><t>", $this->SI).'</t></si>';
$template = str_replace(['{CNT}', '{STRINGS}'], [ $si_cnt, $this->SI ], $template ); $template = str_replace(['{CNT}', '{STRINGS}'], [ $si_cnt, $si ], $template );
$this->_writeEntry($fh, $cdrec, $cfilename, $template); $this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++; $entries++;
} elseif ( $cfilename === 'xl/worksheets/sheet1.xml' ) { } elseif ( $cfilename === 'xl/worksheets/sheet1.xml' ) {
@ -511,7 +510,7 @@ class SimpleXLSXGen {
$N = self::N_TIME; // time $N = self::N_TIME; // time
} elseif ( preg_match( '/^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/', $v, $m ) ) { } 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] ); $cv = $this->date2excel( $m[1], $m[2], $m[3], $m[4], $m[5], $m[6] );
$N = self::N_DATETIME; // [22] m/d/yy h:mm $N = ((int) $m[1] === 0) ? self::N_TIME : self::N_DATETIME; // [22] m/d/yy h:mm
} elseif ( preg_match( '/^(\d\d)\/(\d\d)\/(\d\d\d\d) (\d\d):(\d\d):(\d\d)$/', $v, $m ) ) { } elseif ( preg_match( '/^(\d\d)\/(\d\d)\/(\d\d\d\d) (\d\d):(\d\d):(\d\d)$/', $v, $m ) ) {
$cv = $this->date2excel( $m[3], $m[2], $m[1], $m[4], $m[5], $m[6] ); $cv = $this->date2excel( $m[3], $m[2], $m[1], $m[4], $m[5], $m[6] );
$N = self::N_DATETIME; // [22] m/d/yy h:mm $N = self::N_DATETIME; // [22] m/d/yy h:mm
@ -525,6 +524,10 @@ class SimpleXLSXGen {
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => 'mailto:' . $v, 'L' => '']; $this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => 'mailto:' . $v, 'L' => ''];
$F = self::F_HYPERLINK; // Hyperlink $F = self::F_HYPERLINK; // Hyperlink
} }
if ( ($N === self::N_DATE || $N === self::N_DATETIME) && $cv < 0 ) {
$cv = null;
$N = 0;
}
} }
if ( !$cv) { if ( !$cv) {
@ -625,15 +628,16 @@ class SimpleXLSXGen {
} }
public 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; $excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
if ( $year === 0 ) { if ( (int) $year === 0 ) {
return $excelTime; return $excelTime;
} }
// self::CALENDAR_WINDOWS_1900 // self::CALENDAR_WINDOWS_1900
$excel1900isLeapYear = True; $excel1900isLeapYear = True;
if (((int)$year === 1900) && ($month <= 2)) { $excel1900isLeapYear = False; } if (($year === 1900) && ($month <= 2)) { $excel1900isLeapYear = False; }
$myExcelBaseDate = 2415020; $myExcelBaseDate = 2415020;
// Julian base date Adjustment // Julian base date Adjustment
@ -643,9 +647,9 @@ class SimpleXLSXGen {
$month += 9; $month += 9;
--$year; --$year;
} }
// Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
$century = substr($year,0,2); $century = substr($year,0,2);
$decade = substr($year,2,2); $decade = substr($year,2,2);
// Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
$excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear; $excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;
return (float) $excelDate + $excelTime; return (float) $excelDate + $excelTime;
@ -659,6 +663,8 @@ class SimpleXLSXGen {
return $this; return $this;
} }
public function esc( $str ) { public function esc( $str ) {
return str_replace( ['&', '<', '>', "\x03"], ['&amp;', '&lt;', '&gt;', ''], $str ); // XML UTF-8: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// but we use fast version
return str_replace( ['&', '<', '>', "\x00","\x03","\x0B"], ['&amp;', '&lt;', '&gt;', '', '', ''], $str );
} }
} }