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

3 Commits

Author SHA1 Message Date
aff0fa243d 1.2.16 2022-08-12 18:57:41 +06:00
82b0db1f8e 1.2.16 2022-08-06 23:13:06 +06:00
aef6ee4935 1.2.15 2022-07-28 18:26:11 +06:00
3 changed files with 55 additions and 14 deletions

View File

@ -1,4 +1,11 @@
# Changelog
## 1.2.16 (2022-08-12)
* added `autoFilter( $range )`
```php
$xlsx->autoFilter('A2:B10');
```
* fixed `0%` bug
## 1.2.15 (2022-07-05)
* added wrap words in long strings `<wraptext>long long line</wraptext>`

View File

@ -104,6 +104,10 @@ $xlsx->addSheet( $books, 'Catalog 2021' );
$xlsx->addSheet( $books2, 'Stephen King catalog');
$xlsx->downloadAs('books_2021.xlsx');
exit();
// Autofilter
$xlsx->autoFilter('A1:B10');
```
### JS array to Excel (AJAX)
```php
@ -131,17 +135,29 @@ function array2excel() {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
var file = new Blob([this.response], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
} else {
alert("Error: " + this.status + " " + this.statusText);
request.onload = function () {
if (this.status === 200) {
var file = new Blob([this.response], {type: this.getResponseHeader('Content-Type')});
var fileURL = URL.createObjectURL(file);
var filename = "", m;
var disposition = this.getResponseHeader('Content-Disposition');
if (disposition && (m = /"([^"]+)"/.exec(disposition)) !== null) {
filename = m[1];
}
var a = document.createElement("a");
if (typeof a.download === 'undefined') {
window.location = fileURL;
} else {
a.href = fileURL;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
alert("Error: " + this.status + " " + this.statusText);
}
}
request.open('POST', "array2excel.php");
request.responseType = "blob";
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

View File

@ -55,7 +55,7 @@ class SimpleXLSXGen {
public function __construct() {
$this->curSheet = -1;
$this->defaultFont = 'Calibri';
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [] ] ];
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '' ] ];
$this->SI = []; // sharedStrings index
$this->SI_KEYS = []; // & keys
$this->XF = [ // styles
@ -90,7 +90,7 @@ class SimpleXLSXGen {
'xl/worksheets/sheet1.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
><dimension ref="{REF}"/>{COLS}<sheetData>{ROWS}</sheetData>{MERGECELLS}{HYPERLINKS}</worksheet>',
><dimension ref="{REF}"/>{COLS}<sheetData>{ROWS}</sheetData>{AUTOFILTER}{MERGECELLS}{HYPERLINKS}</worksheet>',
'xl/worksheets/_rels/sheet1.xml.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{HYPERLINKS}</Relationships>',
'xl/sharedStrings.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@ -157,7 +157,7 @@ class SimpleXLSXGen {
}
}
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => []];
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '' ];
if ( isset( $rows[0] ) && is_array($rows[0]) ) {
$this->sheets[$this->curSheet]['rows'] = $rows;
@ -620,7 +620,7 @@ class SimpleXLSXGen {
$N = 0;
}
}
if ( !$cv) {
if ( $cv === null ) {
$v = $this->esc( $v );
@ -697,6 +697,11 @@ class SimpleXLSXGen {
$REF = 'A1:A1';
}
$AUTOFILTER = '';
if ($this->sheets[$idx]['autofilter']) {
$AUTOFILTER = '<autoFilter ref="'.$this->sheets[$idx]['autofilter'].'" />';
}
$MERGECELLS = [];
if ( count($this->sheets[$idx]['mergecells']) ) {
$MERGECELLS[] = '';
@ -715,11 +720,18 @@ class SimpleXLSXGen {
}
$HYPERLINKS[] = '</hyperlinks>';
}
//restore locale
setlocale(LC_NUMERIC, $_loc);
return str_replace(['{REF}','{COLS}','{ROWS}','{MERGECELLS}','{HYPERLINKS}'],
[ $REF, implode("\r\n", $COLS), implode("\r\n",$ROWS), implode("\r\n", $MERGECELLS), implode("\r\n", $HYPERLINKS) ],
return str_replace(['{REF}','{COLS}','{ROWS}','{AUTOFILTER}','{MERGECELLS}','{HYPERLINKS}'],
[ $REF,
implode("\r\n", $COLS),
implode("\r\n",$ROWS),
$AUTOFILTER,
implode("\r\n", $MERGECELLS),
implode("\r\n", $HYPERLINKS)
],
$template );
}
@ -768,6 +780,12 @@ class SimpleXLSXGen {
$this->defaultFontSize = $size;
return $this;
}
public function autoFilter( $range ) {
$this->sheets[$this->curSheet]['autofilter'] = $range;
return $this;
}
public function mergeCells( $range ) {
$this->sheets[$this->curSheet]['mergecells'][] = $range;
return $this;