mirror of
https://github.com/shuchkin/simplexlsxgen.git
synced 2023-08-10 21:12:59 +03:00
Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
aff0fa243d | |||
82b0db1f8e | |||
aef6ee4935 | |||
053574d1e1 | |||
f74163d311 | |||
0bf38275d7 | |||
459b1666e9 | |||
b63c66f8e9 | |||
1b5701a07f | |||
199e5691e7 | |||
c54b314328 | |||
6db113f753 | |||
ff3f016358 | |||
bcd3586731 | |||
402c6b9417 | |||
5840fd5285 | |||
4543922330 | |||
d49f8996df | |||
5309e6a002 | |||
ab28bc319d | |||
e145bd5777 | |||
2ee7237016 | |||
a0a932e2f5 | |||
fc884044ac | |||
d84c842d38 | |||
0dafc8bf9e | |||
82f19a9f35 | |||
139602c340 | |||
ac34d8f527 | |||
c55d77c63e | |||
c3d10db967 |
28
CHANGELOG.md
28
CHANGELOG.md
@ -1,4 +1,32 @@
|
||||
# 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>`
|
||||
|
||||
## 1.2.14 (2022-06-10)
|
||||
* added example [JS array to Excel (AJAX)](https://github.com/shuchkin/simplexlsxgen#js-array-to-excel-ajax)
|
||||
|
||||
## 1.2.13 (2022-06-01)
|
||||
* setColWidth(num_col_started_1, size_in_chars) - set column width
|
||||
|
||||
## 1.2.12 (2022-05-17)
|
||||
* Vertical align (tags top,middle,bottom) `<bottom>12345</bottom>`
|
||||
|
||||
## 1.2.11 (2022-05-01)
|
||||
* Row height `<style height="50">Custom row height 50</style>`
|
||||
|
||||
|
||||
## 1.2.10 (2022-04-24)
|
||||
* Added colors `<style color="#FFFF00" bgcolor="#00FF00">Yellow text on blue background</style>`, thx [mrjemson](https://github.com/mrjemson)
|
||||
|
||||
## 1.1.12 (2022-03-15)
|
||||
* Added `$xlsx->mergeCells('A1:C1')`
|
||||
|
||||
## 1.1.11 (2022-02-05)
|
||||
* sheet name maximum length is 31 chars, mb_substr used now
|
||||
|
81
README.md
81
README.md
@ -6,7 +6,7 @@ Export data to Excel XLSX file. PHP XLSX generator. No external tools and librar
|
||||
- XLS reader [here](https://github.com/shuchkin/simplexls)
|
||||
- CSV reader/writer [here](https://github.com/shuchkin/simplecsv)
|
||||
|
||||
**Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2021<br/>
|
||||
**Sergey Shuchkin** <sergey.shuchkin@gmail.com> 2020-2022<br/>
|
||||
|
||||
*Hey, bro, please ★ the package for my motivation :) and [donate](https://opencollective.com/simplexlsx) for more motivation!*
|
||||
|
||||
@ -63,14 +63,25 @@ $data = [
|
||||
['Bold + Italic', '<b><i>12345.67</i></b>'],
|
||||
['Hyperlink', 'https://github.com/shuchkin/simplexlsxgen'],
|
||||
['Italic + Hyperlink + Anchor', '<i><a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a></i>'],
|
||||
['Green', '<style color="#00FF00">12345.67</style>'],
|
||||
['Bold Red Text', '<b><style color="#FF0000">12345.67</style></b>'],
|
||||
['Blue Text and Yellow Fill', '<style bgcolor="#FFFF00" color="#0000FF">12345.67</style>'],
|
||||
['Left', '<left>12345.67</left>'],
|
||||
['Center', '<center>12345.67</center>'],
|
||||
['Right', '<right>Right Text</right>'],
|
||||
['Center + Bold', '<center><b>Name</b></center>']
|
||||
['Center + Bold', '<center><b>Name</b></center>'],
|
||||
['Row height', '<style height="50">Row Height = 50</style>'],
|
||||
['Top', '<style height="50"><top>Top</top></style>'],
|
||||
['Middle + Center', '<style height="50"><middle><center>Middle + Center</center></middle></style>'],
|
||||
['Bottom + Right', '<style height="50"><bottom><right>Bottom + Right</right></bottom></style>'],
|
||||
['<center>MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS</center>', null],
|
||||
['<top>Word wrap</top>', "<wraptext>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book</wraptext>"]
|
||||
];
|
||||
Shuchkin\SimpleXLSXGen::fromArray( $data )
|
||||
SimpleXLSXGen::fromArray( $data )
|
||||
->setDefaultFont( 'Courier New' )
|
||||
->setDefaultFontSize( 14 )
|
||||
->setColWidth(1, 35) // 1 - num column, 35 - size in chars
|
||||
->mergeCells('A20:B20')
|
||||
->saveAs('styles_and_tags.xlsx');
|
||||
```
|
||||

|
||||
@ -93,6 +104,70 @@ $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
|
||||
<?php // array2excel.php
|
||||
if (isset($_POST['array2excel'])) {
|
||||
require __DIR__.'/simplexlsxgen/src/SimpleXLSXGen.php';
|
||||
$data = json_decode($_POST['array2excel'], false);
|
||||
\Shuchkin\SimpleXLSXGen::fromArray($data)->downloadAs('file.xlsx');
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>JS array to Excel</title>
|
||||
</head>
|
||||
<script>
|
||||
|
||||
function array2excel() {
|
||||
var books = [
|
||||
["ISBN", "title", "author", "publisher", "ctry"],
|
||||
[618260307, "The Hobbit", "J. R. R. Tolkien", "Houghton Mifflin", "USA"],
|
||||
[908606664, "Slinky Malinki", "Lynley Dodd", "Mallinson Rendel", "NZ"]
|
||||
];
|
||||
var json = JSON.stringify(books);
|
||||
|
||||
var request = new XMLHttpRequest();
|
||||
|
||||
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");
|
||||
request.send("array2excel=" + encodeURIComponent(json));
|
||||
}
|
||||
</script>
|
||||
<body>
|
||||
<input type="button" onclick="array2excel()" value="array2excel" />
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## Debug
|
||||
|
@ -1,4 +1,9 @@
|
||||
<?php
|
||||
<?php /** @noinspection ReturnTypeCanBeDeclaredInspection */
|
||||
/** @noinspection PhpMissingReturnTypeInspection */
|
||||
/** @noinspection NullCoalescingOperatorCanBeUsedInspection */
|
||||
|
||||
/** @noinspection PhpIssetCanBeReplacedWithCoalesceInspection */
|
||||
|
||||
namespace Shuchkin;
|
||||
|
||||
/**
|
||||
@ -14,7 +19,6 @@ class SimpleXLSXGen {
|
||||
protected $defaultFontSize;
|
||||
protected $sheets;
|
||||
protected $template;
|
||||
protected $F, $F_KEYS; // fonts
|
||||
protected $XF, $XF_KEYS; // cellXfs
|
||||
protected $SI, $SI_KEYS; // shared strings
|
||||
const N_NORMAL = 0; // General
|
||||
@ -31,21 +35,35 @@ class SimpleXLSXGen {
|
||||
const F_ITALIC = 4;
|
||||
const F_UNDERLINE = 8;
|
||||
const F_STRIKE = 16;
|
||||
const F_COLOR = 32;
|
||||
const FL_NONE = 0; // none
|
||||
const FL_SOLID = 1; // solid
|
||||
const FL_MEDIUM_GRAY = 2; // mediumGray
|
||||
const FL_DARK_GRAY = 4; // darkGray
|
||||
const FL_LIGHT_GRAY = 8; // lightGray
|
||||
const FL_GRAY_125 = 16; // gray125
|
||||
const FL_COLOR = 32;
|
||||
const A_DEFAULT = 0;
|
||||
const A_LEFT = 1;
|
||||
const A_RIGHT = 2;
|
||||
const A_CENTER = 3;
|
||||
const A_CENTER = 4;
|
||||
const A_TOP = 8;
|
||||
const A_MIDDLE = 16;
|
||||
const A_BOTTOM = 32;
|
||||
const A_WRAPTEXT = 64;
|
||||
|
||||
public function __construct() {
|
||||
$this->curSheet = -1;
|
||||
$this->defaultFont = 'Calibri';
|
||||
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [] ] ];
|
||||
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '' ] ];
|
||||
$this->SI = []; // sharedStrings index
|
||||
$this->SI_KEYS = []; // & keys
|
||||
$this->F = [ self::F_NORMAL ]; // fonts
|
||||
$this->F_KEYS = [0]; // & keys
|
||||
$this->XF = [ [self::N_NORMAL, self::F_NORMAL, self::A_DEFAULT] ]; // styles
|
||||
$this->XF_KEYS = ['N0F0A0' => 0 ]; // & keys
|
||||
$this->XF = [ // styles
|
||||
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_NONE, 0, 0],
|
||||
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_GRAY_125, 0, 0], // hack
|
||||
];
|
||||
$this->XF_KEYS['0-0-0-0-0-0'] = 0; // & keys
|
||||
$this->XF_KEYS['0-0-0-16-0-0'] = 1;
|
||||
|
||||
$this->template = [
|
||||
'_rels/.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
@ -72,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>{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"?>
|
||||
@ -80,7 +98,7 @@ class SimpleXLSXGen {
|
||||
'xl/styles.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
||||
{FONTS}
|
||||
<fills count="1"><fill><patternFill patternType="none"/></fill></fills>
|
||||
{FILLS}
|
||||
<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>
|
||||
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" /></cellStyleXfs>
|
||||
{XF}
|
||||
@ -139,7 +157,7 @@ class SimpleXLSXGen {
|
||||
}
|
||||
}
|
||||
|
||||
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => []];
|
||||
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '' ];
|
||||
|
||||
if ( isset( $rows[0] ) && is_array($rows[0]) ) {
|
||||
$this->sheets[$this->curSheet]['rows'] = $rows;
|
||||
@ -294,30 +312,68 @@ class SimpleXLSXGen {
|
||||
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
|
||||
$entries++;
|
||||
} elseif ( $cfilename === 'xl/styles.xml' ) {
|
||||
$FONTS = ['<fonts count="'.count($this->F).'">'];
|
||||
foreach ( $this->F as $f ) {
|
||||
$FONTS[] = '<font><name val="'.$this->defaultFont.'"/><family val="2"/>'
|
||||
. ( $this->defaultFontSize ? '<sz val="'.$this->defaultFontSize.'"/>' : '' )
|
||||
.( $f & self::F_BOLD ? '<b/>' : '')
|
||||
.( $f & self::F_ITALIC ? '<i/>' : '')
|
||||
.( $f & self::F_UNDERLINE ? '<u/>' : '')
|
||||
.( $f & self::F_STRIKE ? '<strike/>' : '')
|
||||
.( $f & self::F_HYPERLINK ? '<color rgb="FF0563C1"/><u/>' : '')
|
||||
.'</font>';
|
||||
}
|
||||
$FONTS[] = '</fonts>';
|
||||
$XF = ['<cellXfs count="'.count($this->XF).'">'];
|
||||
$XF = $FONTS = $F_KEYS = $FILLS = $FL_KEYS = [];
|
||||
// print_r( $this->XF );
|
||||
foreach( $this->XF as $xf ) {
|
||||
$align = ($xf[2] === self::A_LEFT ? ' applyAlignment="1"><alignment horizontal="left"/>' : '')
|
||||
.($xf[2] === self::A_RIGHT ? ' applyAlignment="1"><alignment horizontal="right"/>' : '')
|
||||
.($xf[2] === self::A_CENTER ? ' applyAlignment="1"><alignment horizontal="center"/>' : '');
|
||||
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$xf[1].'" fillId="0" borderId="0" xfId="0"'
|
||||
// 0 - num fmt, 1 - align, 2 - font, 3 - fill, 4 - font color, 5 - bgcolor
|
||||
// fonts
|
||||
$F_KEY = $xf[2].'-'.$xf[4];
|
||||
if ( isset($F_KEYS[ $F_KEY ]) ) {
|
||||
$F_ID = $F_KEYS[ $F_KEY ];
|
||||
} else {
|
||||
$F_ID = $F_KEYS[ $F_KEY ] = count( $FONTS );
|
||||
|
||||
$FONTS[] = '<font><name val="'.$this->defaultFont.'"/><family val="2"/>'
|
||||
. ( $this->defaultFontSize ? '<sz val="'.$this->defaultFontSize.'"/>' : '' )
|
||||
.( $xf[2] & self::F_BOLD ? '<b/>' : '')
|
||||
.( $xf[2] & self::F_ITALIC ? '<i/>' : '')
|
||||
.( $xf[2] & self::F_UNDERLINE ? '<u/>' : '')
|
||||
.( $xf[2] & self::F_STRIKE ? '<strike/>' : '')
|
||||
.( $xf[2] & self::F_HYPERLINK ? '<u/>' : '')
|
||||
.( $xf[2] & self::F_COLOR ? '<color rgb="'.$xf[4].'"/>' : '')
|
||||
.'</font>';
|
||||
}
|
||||
// fills
|
||||
$FL_KEY = $xf[3].'-'.$xf[5];
|
||||
if (isset($FL_KEYS[$FL_KEY])) {
|
||||
$FL_ID = $FL_KEYS[ $FL_KEY ];
|
||||
} else {
|
||||
$FL_ID = $FL_KEYS[ $FL_KEY ] = count($FILLS);
|
||||
$FILLS[] = '<fill><patternFill patternType="'
|
||||
.( $xf[3] === 0 ? 'none' : '')
|
||||
.( $xf[3] & self::FL_SOLID ? 'solid' : '')
|
||||
.( $xf[3] & self::FL_MEDIUM_GRAY ? 'mediumGray' : '')
|
||||
.( $xf[3] & self::FL_DARK_GRAY ? 'darkGray' : '')
|
||||
.( $xf[3] & self::FL_LIGHT_GRAY ? 'lightGray' : '')
|
||||
.( $xf[3] & self::FL_GRAY_125 ? 'gray125' : '')
|
||||
.'"'
|
||||
.( $xf[3] & self::FL_COLOR ? '><fgColor rgb="'.$xf[5].'"/><bgColor indexed="64"/></patternFill>' : ' />')
|
||||
.'</fill>';
|
||||
}
|
||||
$align = ($xf[1] & self::A_LEFT ? ' horizontal="left"' : '')
|
||||
.($xf[1] & self::A_RIGHT ? ' horizontal="right"' : '')
|
||||
.($xf[1] & self::A_CENTER ? ' horizontal="center"' : '')
|
||||
.($xf[1] & self::A_TOP ? ' vertical="top"' : '')
|
||||
.($xf[1] & self::A_MIDDLE ? ' vertical="center"' : '')
|
||||
.($xf[1] & self::A_BOTTOM ? ' vertical="bottom"' : '')
|
||||
.($xf[1] & self::A_WRAPTEXT ? ' wrapText="1"' : '');
|
||||
|
||||
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$F_ID.'" fillId="'.$FL_ID.'" borderId="0" xfId="0"'
|
||||
.($xf[0] > 0 ? ' applyNumberFormat="1"' : '')
|
||||
.($align ? $align . '</xf>' : '/>');
|
||||
.($F_ID > 0 ? ' applyFont="1"' : '')
|
||||
.($FL_ID > 0 ? ' applyFill="1"' : '')
|
||||
.($align ? ' applyAlignment="1"><alignment'.$align . '/></xf>' : '/>');
|
||||
|
||||
}
|
||||
// wrap collections
|
||||
array_unshift( $XF, '<cellXfs count="'.count($XF).'">');
|
||||
$XF[] = '</cellXfs>';
|
||||
$template = str_replace(['{FONTS}','{XF}'], [implode("\r\n", $FONTS), implode("\r\n", $XF)], $template);
|
||||
array_unshift($FONTS, '<fonts count="'.count($FONTS).'">');
|
||||
$FONTS[] = '</fonts>';
|
||||
array_unshift($FILLS, '<fills count="'.count($FILLS).'">');
|
||||
$FILLS[] = '</fills>';
|
||||
|
||||
$template = str_replace(['{FONTS}','{XF}','{FILLS}'], [implode("\r\n", $FONTS), implode("\r\n", $XF), implode("\r\n", $FILLS)], $template);
|
||||
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
|
||||
$entries++;
|
||||
} else {
|
||||
@ -433,21 +489,23 @@ class SimpleXLSXGen {
|
||||
$COL = [];
|
||||
foreach( $this->sheets[$idx]['rows'] as $r ) {
|
||||
$CUR_ROW++;
|
||||
$row = '<row r="'.$CUR_ROW.'">';
|
||||
$row = '';
|
||||
$CUR_COL = 0;
|
||||
$RH = 0; // row height
|
||||
foreach( $r as $v ) {
|
||||
$CUR_COL++;
|
||||
if ( !isset($COL[ $CUR_COL ])) {
|
||||
$COL[ $CUR_COL ] = 0;
|
||||
}
|
||||
$cname = $this->num2name($CUR_COL) . $CUR_ROW;
|
||||
|
||||
if ( $v === null || $v === '' ) {
|
||||
$row .= '<c r="'.$cname.'"/>';
|
||||
continue;
|
||||
}
|
||||
|
||||
$cname = $this->num2name($CUR_COL) . $CUR_ROW;
|
||||
|
||||
$ct = $cv = null;
|
||||
$N = $F = $A = 0;
|
||||
$N = $A = $F = $FL = $C = $B = 0;
|
||||
|
||||
if ( is_string($v) ) {
|
||||
|
||||
@ -468,6 +526,21 @@ class SimpleXLSXGen {
|
||||
if ( strpos( $v, '<s>' ) !== false ) {
|
||||
$F += self::F_STRIKE;
|
||||
}
|
||||
if ( preg_match('/<style([^>]+)>/', $v, $m ) ) {
|
||||
|
||||
if ( preg_match('/ color="([^"]+)"/', $m[1], $m2) ) {
|
||||
|
||||
$F += self::F_COLOR;
|
||||
$C = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#'));
|
||||
}
|
||||
if ( preg_match('/ bgcolor="([^"]+)"/', $m[1], $m2) ) {
|
||||
$FL += self::FL_COLOR;
|
||||
$B = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#'));
|
||||
}
|
||||
if ( preg_match('/ height="([^"]+)"/', $m[1], $m2) ) {
|
||||
$RH = $m2[1];
|
||||
}
|
||||
}
|
||||
if ( strpos( $v, '<left>' ) !== false ) {
|
||||
$A += self::A_LEFT;
|
||||
}
|
||||
@ -477,14 +550,26 @@ class SimpleXLSXGen {
|
||||
if ( strpos( $v, '<right>' ) !== false ) {
|
||||
$A += self::A_RIGHT;
|
||||
}
|
||||
if ( strpos( $v, '<top>' ) !== false ) {
|
||||
$A += self::A_TOP;
|
||||
}
|
||||
if ( strpos( $v, '<middle>' ) !== false ) {
|
||||
$A += self::A_MIDDLE;
|
||||
}
|
||||
if ( strpos( $v, '<bottom>' ) !== false ) {
|
||||
$A += self::A_BOTTOM;
|
||||
}
|
||||
if ( strpos( $v, '<wraptext>' ) !== false ) {
|
||||
$A += self::A_WRAPTEXT;
|
||||
}
|
||||
if ( preg_match( '/<a href="(https?:\/\/[^"]+)">(.*?)<\/a>/i', $v, $m ) ) {
|
||||
$h = explode( '#', $m[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
|
||||
$F += self::F_HYPERLINK; // mailto hyperlink
|
||||
}
|
||||
$v = strip_tags( $v );
|
||||
} // tags
|
||||
@ -521,21 +606,21 @@ class SimpleXLSXGen {
|
||||
$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
|
||||
} elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ?
|
||||
$A = self::A_RIGHT;
|
||||
$A += ($A & (self::A_LEFT | self::A_CENTER)) ? 0 : self::A_RIGHT;
|
||||
} elseif ( preg_match( '/^https?:\/\/\S+$/i', $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] : ''];
|
||||
$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\-]*\.)+[a-zA-Z]{2,}$/", $v ) ) {
|
||||
$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 === null ) {
|
||||
|
||||
$v = $this->esc( $v );
|
||||
|
||||
@ -564,7 +649,7 @@ class SimpleXLSXGen {
|
||||
$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') );
|
||||
$cv = $this->date2excel($v->format('Y'), $v->format('m'), $v->format('d'), $v->format('H'), $v->format('i'), $v->format('s'));
|
||||
$N = self::N_DATETIME; // [22] m/d/yy h:mm
|
||||
} else {
|
||||
continue;
|
||||
@ -573,40 +658,60 @@ class SimpleXLSXGen {
|
||||
$COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] );
|
||||
|
||||
$cs = 0;
|
||||
if ( $N + $F + $A > 0 ) {
|
||||
|
||||
if ( isset($this->F_KEYS[ $F ] ) ) {
|
||||
$cf = $this->F_KEYS[ $F ];
|
||||
} else {
|
||||
$cf = count($this->F);
|
||||
$this->F_KEYS[$F] = $cf;
|
||||
$this->F[] = $F;
|
||||
if ( $N + $A + $F + $FL > 0 ) {
|
||||
|
||||
if ( $FL === self::FL_COLOR ) {
|
||||
$FL += self::FL_SOLID;
|
||||
}
|
||||
$NFA = 'N' . $N . 'F' . $cf . 'A' . $A;
|
||||
if ( isset( $this->XF_KEYS[ $NFA ] ) ) {
|
||||
$cs = $this->XF_KEYS[ $NFA ];
|
||||
if ( ($F & self::F_HYPERLINK) && !($F & self::F_COLOR)) {
|
||||
$F += self::F_COLOR;
|
||||
$C = 'FF0563C1';
|
||||
}
|
||||
|
||||
$XF_KEY = $N . '-' . $A . '-' . $F. '-'. $FL . '-' . $C . '-' . $B;
|
||||
// echo $cname .'='.$XF_KEY.PHP_EOL;
|
||||
if ( isset( $this->XF_KEYS[ $XF_KEY ] ) ) {
|
||||
$cs = $this->XF_KEYS[ $XF_KEY ];
|
||||
}
|
||||
if ( $cs === 0 ) {
|
||||
$cs = count( $this->XF );
|
||||
$this->XF_KEYS[ $NFA ] = $cs;
|
||||
$this->XF[] = [$N, $cf, $A];
|
||||
$this->XF_KEYS[ $XF_KEY ] = $cs;
|
||||
$this->XF[] = [$N, $A, $F, $FL, $C, $B];
|
||||
}
|
||||
}
|
||||
|
||||
$row .= '<c r="' . $cname . '"'.($ct ? ' t="'.$ct.'"' : '').($cs ? ' s="'.$cs.'"' : '').'>'
|
||||
.($ct === 'inlineStr' ? '<is><t>'.$cv.'</t></is>' : '<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";
|
||||
$ROWS[] = '<row r="'.$CUR_ROW.'"'.($RH ? ' customHeight="1" ht="'.$RH.'"' : '').'>'.$row . "</row>";
|
||||
}
|
||||
foreach ( $COL as $k => $max ) {
|
||||
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" />';
|
||||
$w = isset($this->sheets[$idx]['colwidth'][$k]) ? $this->sheets[$idx]['colwidth'][$k] : min( $max+1, 60);
|
||||
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.$w.'" />';
|
||||
}
|
||||
$COLS[] = '</cols>';
|
||||
$REF = 'A1:'.$this->num2name(count($COLS)) . $CUR_ROW;
|
||||
$REF = 'A1:'.$this->num2name(count($COL)) . $CUR_ROW;
|
||||
} else {
|
||||
$ROWS[] = '<row r="1"><c r="A1" t="s"><v>0</v></c></row>';
|
||||
$REF = 'A1:A1';
|
||||
}
|
||||
|
||||
$AUTOFILTER = '';
|
||||
if ($this->sheets[$idx]['autofilter']) {
|
||||
$AUTOFILTER = '<autoFilter ref="'.$this->sheets[$idx]['autofilter'].'" />';
|
||||
}
|
||||
|
||||
$MERGECELLS = [];
|
||||
if ( count($this->sheets[$idx]['mergecells']) ) {
|
||||
$MERGECELLS[] = '';
|
||||
$MERGECELLS[] = '<mergeCells count="'.count($this->sheets[$idx]['mergecells']).'">';
|
||||
foreach( $this->sheets[$idx]['mergecells'] as $m ) {
|
||||
$MERGECELLS[] = '<mergeCell ref="'.$m.'"/>';
|
||||
}
|
||||
$MERGECELLS[] = '</mergeCells>';
|
||||
}
|
||||
|
||||
$HYPERLINKS = [];
|
||||
if ( count( $this->sheets[$idx]['hyperlinks']) ) {
|
||||
$HYPERLINKS[] = '<hyperlinks>';
|
||||
@ -615,11 +720,18 @@ class SimpleXLSXGen {
|
||||
}
|
||||
$HYPERLINKS[] = '</hyperlinks>';
|
||||
}
|
||||
|
||||
//restore locale
|
||||
setlocale(LC_NUMERIC, $_loc);
|
||||
|
||||
return str_replace(['{REF}','{COLS}','{ROWS}','{HYPERLINKS}'],
|
||||
[ $REF, implode("\r\n", $COLS), implode("\r\n",$ROWS), 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 );
|
||||
}
|
||||
|
||||
@ -668,9 +780,23 @@ 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;
|
||||
}
|
||||
public function setColWidth($col, $width) {
|
||||
$this->sheets[$this->curSheet]['colwidth'][$col] = $width;
|
||||
return $this;
|
||||
}
|
||||
public function esc( $str ) {
|
||||
// XML UTF-8: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||
// but we use fast version
|
||||
return str_replace( ['&', '<', '>', "\x00","\x03","\x0B"], ['&', '<', '>', '', '', ''], $str );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
styles.png
BIN
styles.png
Binary file not shown.
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 40 KiB |
Reference in New Issue
Block a user