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

23 Commits

Author SHA1 Message Date
a8093b9d8e Merge remote-tracking branch 'origin/master' 2020-10-04 16:08:32 +06:00
a9219575df disable type detection 2020-10-04 16:08:12 +06:00
476e083f29 Merge pull request #8 from jturbide/master
Fix invalid content
2020-08-28 00:26:29 +06:00
db598d4ee4 Update SimpleXLSXGen.php 2020-08-26 17:15:14 -04:00
a551f6485b Long Numbers format 2020-08-23 15:07:29 +06:00
aafe6abd5b fixed fast shared strings index 2020-08-22 13:54:34 +06:00
9b2c9d3e1c Fixed real numbers in 123.45 format detection, fast shared strings index 2020-08-21 21:57:02 +06:00
9824e5b25d Merge pull request #4 from fredriksundin/performance
Replace array_search to optimize large arrays
2020-08-21 21:40:15 +06:00
c3d64d13ef Fix strict compare bug 2020-08-19 23:09:33 +00:00
22cbd84f3c Add strict compare 2020-08-19 22:48:12 +00:00
291b508143 Replace array_search to optimize large arrays 2020-08-19 22:41:08 +00:00
5a4c04025a fixed time detection 2020-07-29 21:50:38 +06:00
982de475c9 Merge pull request #2 from boryn/patch-1
Extension '.xlsx' outside of gmdate()
2020-07-16 13:38:30 +06:00
e92d3a5284 Extension '.xlsx' outside of gmdate() 2020-07-15 23:16:48 +02:00
ca5da99160 debug example 2020-07-14 17:24:11 +06:00
8a38813b12 debug example 2020-07-14 17:19:58 +06:00
72b9f420d7 debug example 2020-07-14 15:24:51 +06:00
08e2ae6892 0.9.14 fixed num2name A-Z,AA-AZ 2020-05-31 08:35:35 +06:00
6a098dd7e4 0.9.13 if string more 160 chars, save as inlineStr 2020-05-20 23:36:47 +06:00
620388a3f6 0.9.12 readme fixed 2020-05-20 23:11:44 +06:00
0aad74dd81 0.9.11 removed XML unimportant attributes 2020-05-20 23:08:58 +06:00
02867ae14c 0.9.11 removed XML unimportant attributes 2020-05-20 23:08:29 +06:00
f74084cd35 0.9.11 removed XML unimportant attributes 2020-05-20 23:06:25 +06:00
2 changed files with 94 additions and 50 deletions

View File

@ -1,4 +1,4 @@
# SimpleXLSXGen class 0.9.10 (Official)
# SimpleXLSXGen class 0.9.20 (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)
Export data to Excel XLSX file. PHP XLSX generator. No external tools and libraries.<br/>
@ -40,13 +40,37 @@ $data = [
['Integer', 123],
['Float', 12.35],
['Procent', '12%'],
['Date','2020-05-20'],
['Datetime', '2020-05-20 02:38:00'],
['String', 'See SimpleXLSXGen column autosize feature']
['Date','2020-05-20'],
['Time','02:38:00'],
['String', 'See SimpleXLSXGen column autosize feature'],
['Disable Type Detection', "\0".'2020-10-04 16:02:00']
];
SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx');
```
![XLSX screenshot](datatypes.png)
### Debug
```php
ini_set('error_reporting', E_ALL );
ini_set('display_errors', 1 );
$data = [
['Debug', 123]
]
SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
```
## History
v0.9.10 (2020-05-20) initial relese
V0.9.20 (2020-10-04) Disable type detection if string started \0
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

View File

@ -52,29 +52,26 @@ class SimpleXLSXGen {
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="{CNT}" uniqueCount="{CNT}">{STRINGS}</sst>',
'xl/styles.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<fonts count="1"><font><name val="Calibri"/><family val="2"/></font></fonts>
<fonts count="2"><font><name val="Calibri"/><family val="2"/></font><font><name val="Calibri"/><family val="2"/><b/></font></fonts>
<fills count="1"><fill><patternFill patternType="none"/></fill></fills>
<borders count="1"><border diagonalUp="false" diagonalDown="false"><left/><right/><top/><bottom/><diagonal/></border></borders>
<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" /></cellStyleXfs>
<cellXfs count="6">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="9" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="10" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="14" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="20" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="22" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="1" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
<xf numFmtId="9" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
<xf numFmtId="10" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
<xf numFmtId="14" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
<xf numFmtId="20" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
<xf numFmtId="22" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" applyAlignment="1"><alignment horizontal="right"/></xf>
</cellXfs>
<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>
</styleSheet>',
'xl/workbook.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="'.__CLASS__.'"/>
<workbookPr backupFile="false" showObjects="all" date1904="false"/>
<bookViews>
<workbookView showHorizontalScroll="true" showVerticalScroll="true" showSheetTabs="true" xWindow="0" yWindow="0" windowWidth="16384" windowHeight="8192" tabRatio="205" firstSheet="0" activeTab="0"/>
</bookViews>
<sheets><sheet name="Sheet1" sheetId="1" state="visible" r:id="rId2"/></sheets>
<calcPr iterateCount="100" refMode="A1" iterate="false" iterateDelta="0.001"/>
</workbook>'
];
// <col min="1" max="1" width="22.1796875" bestFit="1" customWidth="1"/>
@ -122,7 +119,7 @@ class SimpleXLSXGen {
return true;
}
public function download() {
return $this->downloadAs( gmdate('YmdHi.xlsx') );
return $this->downloadAs( gmdate('YmdHi') . '.xlsx' );
}
public function downloadAs( $filename ) {
$fh = fopen('php://memory','wb');
@ -167,6 +164,7 @@ class SimpleXLSXGen {
}
$SI = [];
$SI_KEYS = [];
$COLS = [];
$ROWS = [];
if ( count($this->rows) ) {
@ -176,7 +174,7 @@ class SimpleXLSXGen {
$CUR_ROW++;
$row = '<row r="'.$CUR_ROW.'">';
$CUR_COL = 0;
foreach( $r as $k => $v ) {
foreach( $r as $v ) {
$CUR_COL++;
if ( !isset($COL[ $CUR_COL ])) {
$COL[ $CUR_COL ] = 0;
@ -184,40 +182,58 @@ class SimpleXLSXGen {
if ( $v === null || $v === '' ) {
continue;
}
$COL[ $CUR_COL ] = max( mb_strlen( (string) $v ), $COL[ $CUR_COL ] );
$vl = mb_strlen( (string) $v );
$cname = $this->_num2name($CUR_COL).$CUR_ROW;
$COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] );
$cname = $this->num2name($CUR_COL) . $CUR_ROW;
$ct = $cs = null;
if ( is_string($v) ) {
if ( preg_match( '/^[-+]?\d{1,18}$/', $v ) ) {
$cv = ltrim($v,'+');
} elseif ( preg_match('/^[-+]?\d+\.?\d*$/', $v ) ) {
if ( $v === '0' || preg_match( '/^[-+]?[1-9]\d{0,14}$/', $v ) ) { // Integer as General
$cv = ltrim( $v, '+' );
if ( $vl > 10 ) {
$cs = 1; // [1] 0
}
} elseif ( preg_match('/^[-+]?(0|[1-9]\d*)\.\d+$/', $v ) ) {
$cv = ltrim($v,'+');
} elseif ( preg_match('/^([-+]?\d+)%$/', $v, $m) ) {
$cv = round( $m[1] / 100, 2);
$cs = 1; // [9] 0%
} elseif ( preg_match('/^([-+]\d+\.\d*)%$/', $v, $m) ) {
$cs = 2; // [9] 0%
} elseif ( preg_match('/^([-+]\d+\.\d+)%$/', $v, $m) ) {
$cv = round( $m[1] / 100, 4 );
$cs = 2; // [10] 0.00%
$cs = 3; // [10] 0.00%
} elseif ( preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d)$/', $v, $m ) ){
$cv = $this->_date2excel($m[1],$m[2],$m[3]);
$cs = 3; // [14] mm-dd-yy
} elseif ( preg_match('/^(\d\d):(\d\d):(\d\d)$/', $v, $m ) ){
$cv = $this->_date2excel(0,0,0,$m[1],$m[2],$m[3]);
$cv = $this->date2excel($m[1],$m[2],$m[3]);
$cs = 4; // [14] mm-dd-yy
} 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]);
$cs = 5; // [22] m/d/yy h:mm
} elseif ( preg_match('/^(\d\d):(\d\d):(\d\d)$/', $v, $m ) ){
$cv = $this->date2excel(0,0,0,$m[1],$m[2],$m[3]);
$cs = 5; // [14] mm-dd-yy
} 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] );
$cs = 6; // [22] m/d/yy h:mm
} elseif ( mb_strlen( $v ) > 160 ) {
$ct = 'inlineStr';
$cv = str_replace(['&','<','>',"\x03"],['&amp;','&lt;','&gt;',''], $v);
} else {
if ( preg_match('/^[0-9+-.]+$/', $v ) ) { // Long ?
$cs = 7; // Align Right
}
$v = ltrim($v,"\0"); // disabled type detection
$ct = 's'; // shared string
$v = htmlentities($v, ENT_QUOTES);
$cv = array_search( $v, $SI, true );
$v = str_replace(['&','<','>',"\x03"],['&amp;','&lt;','&gt;',''], $v);
$cv = false;
$skey = '~'.$v;
if ( isset($SI_KEYS[ $skey ]) ) {
$cv = $SI_KEYS[ $skey ];
}
if ( $cv === false ) {
$SI[] = $v;
$cv = count( $SI ) - 1;
$SI_KEYS[$skey] = $cv;
}
}
} elseif ( is_int( $v ) || is_float( $v ) ) {
@ -226,17 +242,18 @@ class SimpleXLSXGen {
continue;
}
$row .= '<c r="' . $cname . '"'.($ct ? ' t="'.$ct.'"' : '').($cs ? ' s="'.$cs.'"' : '').'><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";
}
foreach ( $COL as $k => $max ) {
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max, 60).'" bestFit="1" customWidth="1" />';
// $COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" bestFit="true" customWidth="true" />';
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" />';
}
$REF = 'A1:'.$this->_num2name(count($COLS)).$CUR_ROW;
$REF = 'A1:'.$this->num2name(count($COLS)) . $CUR_ROW;
} else {
// $COLS[] = '<col min="1" max="7" width="7.18" bestFit="1" customWidth="1" />';
$COLS[] = '<col min="1" max="7" bestFit="1" />';
$COLS[] = '<col min="1" max="1" bestFit="1" />';
$ROWS[] = '<row r="1"><c r="A1" t="s"><v>0</v></c></row>';
$REF = 'A1:A1';
$SI[] = 'No Data';
@ -348,17 +365,22 @@ class SimpleXLSXGen {
return true;
}
private function _num2name($num) {
$numeric = ($num-1) % 26;
public function num2name($num) {
$numeric = ($num - 1) % 26;
$letter = chr( 65 + $numeric );
$num2 = (int) ( ($num-1) / 26 );
if ( $num2 > 0 ) {
return $this->_num2name( $num2 - 1 ) . $letter;
return $this->num2name( $num2 ) . $letter;
}
return $letter;
}
private 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;
if ( $year === 0 ) {
return $excelTime;
}
// self::CALENDAR_WINDOWS_1900
$excel1900isLeapYear = True;
if (((int)$year === 1900) && ($month <= 2)) { $excel1900isLeapYear = False; }
@ -376,8 +398,6 @@ class SimpleXLSXGen {
$decade = substr($year,2,2);
$excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;
$excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
return (float) $excelDate + $excelTime;
}
}
return (float) $excelDate + $excelTime;
}
}