mirror of
https://github.com/shuchkin/simplexlsxgen.git
synced 2023-08-10 21:12:59 +03:00
Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
7f5c584772 | |||
5b58103e3b | |||
6d49a90fe7 | |||
c8a94d552a | |||
c9fa7da689 | |||
0d059fdf6e | |||
dcb5f8ad65 | |||
f970452779 | |||
6abd0286bb | |||
8bfbe078db | |||
6ece83f9ce | |||
4f375a4c30 | |||
41c6e7ca51 | |||
ef7b0fff02 | |||
860da7f493 |
118
CHANGELOG.md
Normal file
118
CHANGELOG.md
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 1.0.20 (2021-07-29)
|
||||||
|
|
||||||
|
* Fixed sheet names duplicates (Page, Page (1), Page (2)...)
|
||||||
|
|
||||||
|
## 1.0.19 (2021-07-28)
|
||||||
|
|
||||||
|
* Fixed sheet names duplicates
|
||||||
|
|
||||||
|
## 1.0.18 (2021-07-28)
|
||||||
|
|
||||||
|
* Fixed email regex
|
||||||
|
|
||||||
|
## 1.0.17 (2021-07-28)
|
||||||
|
|
||||||
|
* Fixed " and & in sheets names
|
||||||
|
|
||||||
|
## 1.0.16 (2021-07-01)
|
||||||
|
|
||||||
|
* Fixed "&" in hyperlinks
|
||||||
|
|
||||||
|
## 1.0.15 (2021-06-22)
|
||||||
|
|
||||||
|
* Fixed *mailto* hyperlinks detection
|
||||||
|
|
||||||
|
## 1.0.14 (2021-06-08)
|
||||||
|
|
||||||
|
* Added *mailto* hyperlinks support (thx Howard Martin)
|
||||||
|
```php
|
||||||
|
SimpleXLSXGen::fromArray([
|
||||||
|
['Mailto hyperlink', '<a href="mailto:sergey.shuchkin@gmail.com">Please email me</a>']
|
||||||
|
])->saveAs('test.xlsx');
|
||||||
|
```
|
||||||
|
## 1.0.13 (2021-05-29)
|
||||||
|
|
||||||
|
* Fixed hyperlinks in several sheets
|
||||||
|
* Added [Opencollective donation link](https://opencollective.com/simplexlsx)
|
||||||
|
|
||||||
|
## 1.0.12 (2021-05-19)
|
||||||
|
|
||||||
|
* Fixed hyperlink regex
|
||||||
|
|
||||||
|
## 1.0.11 (2021-05-14)
|
||||||
|
|
||||||
|
* Fixed 0.00% format, thx [marcrobledo](https://github.com/shuchkin/simplexlsxgen/pull/34), more examples in README.md
|
||||||
|
|
||||||
|
## 1.0.10 (2021-05-03)
|
||||||
|
|
||||||
|
Stable release
|
||||||
|
|
||||||
|
* Added hyperlinks and minimal formatting
|
||||||
|
|
||||||
|
## 0.9.25 (2021-02-26)
|
||||||
|
|
||||||
|
* Added PHP Datetime object values in a cells
|
||||||
|
|
||||||
|
## 0.9.24 (2021-02-26)
|
||||||
|
|
||||||
|
* Percent support
|
||||||
|
|
||||||
|
|
||||||
|
## 0.9.23 (2021-01-25)
|
||||||
|
|
||||||
|
* Fix local floats in XML
|
||||||
|
|
||||||
|
|
||||||
|
## 0.9.22 (2020-11-04)
|
||||||
|
|
||||||
|
* Added multiple sheets support, thx [Savino59](https://github.com/Savino59), class ready for extend now
|
||||||
|
|
||||||
|
## 0.9.21 (2020-10-17)
|
||||||
|
|
||||||
|
* Updated images
|
||||||
|
|
||||||
|
## 0.9.20 (2020-10-04)
|
||||||
|
|
||||||
|
* Disable type detection if string started with chr(0)
|
||||||
|
|
||||||
|
## 0.9.19 (2020-08-23)
|
||||||
|
|
||||||
|
* Numbers like SKU right aligned now
|
||||||
|
|
||||||
|
## 0.9.18 (2020-08-22)
|
||||||
|
|
||||||
|
* Fixed fast shared strings index
|
||||||
|
|
||||||
|
## 0.9.17 (2020-08-21)
|
||||||
|
|
||||||
|
* Fixed real numbers in 123.45 format detection, fast shared strings index (thx fredriksundin)
|
||||||
|
|
||||||
|
## 0.9.16 (2020-07-29)
|
||||||
|
|
||||||
|
* Fixed time detection in HH:MM:SS format
|
||||||
|
|
||||||
|
## 0.9.15 (2020-07-14)
|
||||||
|
|
||||||
|
* Escape of shared strings for special chars in cells [#1](https://github.com/shuchkin/simplexlsxgen/issues/1)
|
||||||
|
|
||||||
|
## 0.9.14 (2020-05-31)
|
||||||
|
|
||||||
|
* Fixed num2name A-Z,AA-AZ column names, thx Ertan Yusufoglu
|
||||||
|
|
||||||
|
## 0.9.13 (2020-05-21)
|
||||||
|
|
||||||
|
* If string more 160 chars, save as inlineStr
|
||||||
|
|
||||||
|
## 0.9.12 (2020-05-21)
|
||||||
|
|
||||||
|
* Readme fixed
|
||||||
|
|
||||||
|
## 0.9.11 (2020-05-21)
|
||||||
|
|
||||||
|
* Removed XML unimportant attributes
|
||||||
|
|
||||||
|
## 0.9.10 (2020-05-20)
|
||||||
|
|
||||||
|
* Initial release
|
64
README.md
64
README.md
@ -1,12 +1,12 @@
|
|||||||
# SimpleXLSXGen class 1.0.10 (Official)
|
# SimpleXLSXGen
|
||||||
[<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/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/>
|
||||||
(!) XLSX reader [here](https://github.com/shuchkin/simplexlsx).
|
(!) XLSX reader [here](https://github.com/shuchkin/simplexlsx).
|
||||||
|
|
||||||
**Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2021<br/>
|
**Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2021<br/>
|
||||||
|
|
||||||
*Hey, bro, please ★ the package for my motivation :)*
|
*Hey, bro, please ★ the package for my motivation :) and [donate](https://opencollective.com/simplexlsx) for more motivation!*
|
||||||
|
|
||||||
## Basic Usage
|
## Basic Usage
|
||||||
```php
|
```php
|
||||||
@ -46,22 +46,10 @@ $data = [
|
|||||||
['Hyperlink + Anchor', '<a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a>'],
|
['Hyperlink + Anchor', '<a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a>'],
|
||||||
['RAW string', "\0".'2020-10-04 16:02:00']
|
['RAW string', "\0".'2020-10-04 16:02:00']
|
||||||
];
|
];
|
||||||
SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx'); // or ->downloadAs('datatypes.xlsx');
|
SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx');
|
||||||
```
|
```
|
||||||

|

|
||||||
### Fluid examples
|
|
||||||
```php
|
|
||||||
SimpleXLSXGen::fromArray( $books )->downloadAs('table.xlsx'); // output to browser for download
|
|
||||||
SimpleXLSXGen::fromArray( $books )->addSheet( $books2 )->download(); // multiple sheets
|
|
||||||
(new SimpleXLSXGen)->addSheet( $books, 'Modern style')->save();
|
|
||||||
```
|
|
||||||
### Old school, multiple sheets
|
|
||||||
```php
|
|
||||||
$xlsx = new SimpleXLSXGen();
|
|
||||||
$xlsx->addSheet( $books, 'Catalog 2021' );
|
|
||||||
$xlsx->addSheet( $books2, 'Stephen King catalog');
|
|
||||||
$xlsx->downloadAs('books_2021.xlsx');
|
|
||||||
```
|
|
||||||
### Formatting
|
### Formatting
|
||||||
```php
|
```php
|
||||||
$data = [
|
$data = [
|
||||||
@ -84,7 +72,27 @@ SimpleXLSXGen::fromArray( $data )
|
|||||||
->saveAs('styles_and_tags.xlsx');
|
->saveAs('styles_and_tags.xlsx');
|
||||||
```
|
```
|
||||||

|

|
||||||
### Debug
|
|
||||||
|
### More examples
|
||||||
|
```php
|
||||||
|
// Fluid interface, output to browser for download
|
||||||
|
SimpleXLSXGen::fromArray( $books )->downloadAs('table.xlsx');
|
||||||
|
|
||||||
|
// Fluid interface, multiple sheets
|
||||||
|
SimpleXLSXGen::fromArray( $books )->addSheet( $books2 )->download();
|
||||||
|
|
||||||
|
// Alternative interface, sheet name, get xlsx content
|
||||||
|
$xlsx_cache = (string) (new SimpleXLSXGen)->addSheet( $books, 'Modern style');
|
||||||
|
|
||||||
|
// Classic interface
|
||||||
|
$xlsx = new SimpleXLSXGen();
|
||||||
|
$xlsx->addSheet( $books, 'Catalog 2021' );
|
||||||
|
$xlsx->addSheet( $books2, 'Stephen King catalog');
|
||||||
|
$xlsx->downloadAs('books_2021.xlsx');
|
||||||
|
exit();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Debug
|
||||||
```php
|
```php
|
||||||
ini_set('error_reporting', E_ALL );
|
ini_set('error_reporting', E_ALL );
|
||||||
ini_set('display_errors', 1 );
|
ini_set('display_errors', 1 );
|
||||||
@ -94,23 +102,3 @@ $data = [
|
|||||||
]
|
]
|
||||||
SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
|
SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## History
|
|
||||||
v1.0.10 (2021-05-03) + Hyperlinks, + Minimal formatting<br/>
|
|
||||||
v0.9.25 (2021-02-26) Added PHP Datetime object values in a cells<br/>
|
|
||||||
v0.9.24 (2021-02-26) * Percent<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.21 (2020-10-17) Updated images<br/>
|
|
||||||
v0.9.20 (2020-10-04) Disable type detection if string started with chr(0)<br/>
|
|
||||||
v0.9.19 (2020-08-23) Numbers like SKU right aligned now<br/>
|
|
||||||
v0.9.18 (2020-08-22) Fixed fast shared strings index<br/>
|
|
||||||
v0.9.17 (2020-08-21) Fixed real numbers in 123.45 format detection, fast shared strings index (thx fredriksundin)<br/>
|
|
||||||
v0.9.16 (2020-07-29) Fixed time detection in HH:MM:SS format<br/>
|
|
||||||
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
|
|
@ -115,9 +115,26 @@ class SimpleXLSXGen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function addSheet( array $rows, $name = null ) {
|
public function addSheet( array $rows, $name = null ) {
|
||||||
$this->curSheet++;
|
|
||||||
|
|
||||||
$this->sheets[$this->curSheet] = ['name' => $name ?: 'Sheet'.($this->curSheet+1), 'hyperlinks' => []];
|
$this->curSheet++;
|
||||||
|
if ( $name === null ) { // autogenerated sheet names
|
||||||
|
$name = 'Sheet'.($this->curSheet+1);
|
||||||
|
} else {
|
||||||
|
$names = [];
|
||||||
|
foreach( $this->sheets as $sh ) {
|
||||||
|
$names[ strtoupper( $sh['name']) ] = 1;
|
||||||
|
}
|
||||||
|
for( $i = 0; $i < 100; $i++ ) {
|
||||||
|
$new_name = ($i === 0) ? $name : $name .' ('.$i.')';
|
||||||
|
$NEW_NAME = strtoupper( $new_name );
|
||||||
|
if ( !isset( $names[ $NEW_NAME ]) ) {
|
||||||
|
$name = $new_name;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => []];
|
||||||
|
|
||||||
if ( is_array( $rows ) && isset( $rows[0] ) && is_array($rows[0]) ) {
|
if ( is_array( $rows ) && isset( $rows[0] ) && is_array($rows[0]) ) {
|
||||||
$this->sheets[$this->curSheet]['rows'] = $rows;
|
$this->sheets[$this->curSheet]['rows'] = $rows;
|
||||||
@ -217,7 +234,7 @@ class SimpleXLSXGen {
|
|||||||
} elseif ( $cfilename === 'xl/workbook.xml' ) {
|
} elseif ( $cfilename === 'xl/workbook.xml' ) {
|
||||||
$s = '';
|
$s = '';
|
||||||
foreach ( $this->sheets as $k => $v ) {
|
foreach ( $this->sheets as $k => $v ) {
|
||||||
$s .= '<sheet name="' . $v['name'] . '" sheetId="' . ( $k + 1) . '" state="visible" r:id="rId' . ( $k + 2) . '"/>';
|
$s .= '<sheet name="' . $this->esc( $v['name'] ) . '" sheetId="' . ( $k + 1) . '" state="visible" r:id="rId' . ( $k + 2) . '"/>';
|
||||||
}
|
}
|
||||||
$template = str_replace('{SHEETS}', $s, $template);
|
$template = str_replace('{SHEETS}', $s, $template);
|
||||||
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
|
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
|
||||||
@ -245,12 +262,12 @@ class SimpleXLSXGen {
|
|||||||
}
|
}
|
||||||
$xml = null;
|
$xml = null;
|
||||||
} elseif ( $cfilename === 'xl/worksheets/_rels/sheet1.xml.rels' ) {
|
} elseif ( $cfilename === 'xl/worksheets/_rels/sheet1.xml.rels' ) {
|
||||||
$RH = [];
|
|
||||||
foreach ( $this->sheets as $k => $v ) {
|
foreach ( $this->sheets as $k => $v ) {
|
||||||
if ( count($v['hyperlinks'])) {
|
if ( count($v['hyperlinks'])) {
|
||||||
|
$RH = [];
|
||||||
$filename = 'xl/worksheets/_rels/sheet' . ( $k + 1 ) . '.xml.rels';
|
$filename = 'xl/worksheets/_rels/sheet' . ( $k + 1 ) . '.xml.rels';
|
||||||
foreach ( $v['hyperlinks'] as $h ) {
|
foreach ( $v['hyperlinks'] as $h ) {
|
||||||
$RH[] = '<Relationship Id="' . $h['ID'] . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="' . $h['H'] . '" TargetMode="External"/>';
|
$RH[] = '<Relationship Id="' . $h['ID'] . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="' . $this->esc($h['H']) . '" TargetMode="External"/>';
|
||||||
}
|
}
|
||||||
$xml = str_replace( '{HYPERLINKS}', implode( "\r\n", $RH ), $template );
|
$xml = str_replace( '{HYPERLINKS}', implode( "\r\n", $RH ), $template );
|
||||||
$this->_writeEntry( $fh, $cdrec, $filename, $xml );
|
$this->_writeEntry( $fh, $cdrec, $filename, $xml );
|
||||||
@ -460,6 +477,10 @@ class SimpleXLSXGen {
|
|||||||
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
|
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
|
||||||
$F = self::F_HYPERLINK; // Hyperlink
|
$F = self::F_HYPERLINK; // Hyperlink
|
||||||
}
|
}
|
||||||
|
if ( preg_match( '/<a href="(mailto?:[^"]+)">(.*?)<\/a>/i', $v, $m ) ) {
|
||||||
|
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $m[1], 'L' => ''];
|
||||||
|
$F = self::F_HYPERLINK; // mailto hyperlink
|
||||||
|
}
|
||||||
$v = strip_tags( $v );
|
$v = strip_tags( $v );
|
||||||
} // tags
|
} // tags
|
||||||
$vl = mb_strlen( $v );
|
$vl = mb_strlen( $v );
|
||||||
@ -476,7 +497,7 @@ class SimpleXLSXGen {
|
|||||||
} elseif ( preg_match( '/^([-+]?\d+)%$/', $v, $m ) ) {
|
} elseif ( preg_match( '/^([-+]?\d+)%$/', $v, $m ) ) {
|
||||||
$cv = round( $m[1] / 100, 2 );
|
$cv = round( $m[1] / 100, 2 );
|
||||||
$N = self::N_PERCENT_INT; // [9] 0%
|
$N = self::N_PERCENT_INT; // [9] 0%
|
||||||
} elseif ( preg_match( '/^([-+]\d+\.\d+)%$/', $v, $m ) ) {
|
} elseif ( preg_match( '/^([-+]?\d+\.\d+)%$/', $v, $m ) ) {
|
||||||
$cv = round( $m[1] / 100, 4 );
|
$cv = round( $m[1] / 100, 4 );
|
||||||
$N = self::N_PRECENT_DEC; // [10] 0.00%
|
$N = self::N_PRECENT_DEC; // [10] 0.00%
|
||||||
} elseif ( preg_match( '/^(\d\d\d\d)-(\d\d)-(\d\d)$/', $v, $m ) ) {
|
} elseif ( preg_match( '/^(\d\d\d\d)-(\d\d)-(\d\d)$/', $v, $m ) ) {
|
||||||
@ -496,11 +517,11 @@ class SimpleXLSXGen {
|
|||||||
$N = self::N_DATETIME; // [22] m/d/yy h:mm
|
$N = self::N_DATETIME; // [22] m/d/yy h:mm
|
||||||
} elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ?
|
} elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ?
|
||||||
$A = self::A_RIGHT;
|
$A = self::A_RIGHT;
|
||||||
} elseif ( preg_match( '/https?:\/\/\S+/i', $v ) ) {
|
} elseif ( preg_match( '/^https?:\/\/\S+$/i', $v ) ) {
|
||||||
$h = explode( '#', $v );
|
$h = explode( '#', $v );
|
||||||
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
|
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
|
||||||
$F = self::F_HYPERLINK; // Hyperlink
|
$F = self::F_HYPERLINK; // Hyperlink
|
||||||
} elseif ( preg_match( "/([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $v ) ) {
|
} elseif ( preg_match( "/^[a-zA-Z0-9_\.\-]+@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/", $v ) ) {
|
||||||
$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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user