mirror of
https://github.com/shuchkin/simplexlsxgen.git
synced 2023-08-10 21:12:59 +03:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
c55d77c63e | |||
c3d10db967 | |||
1c206d06bc | |||
f0cbc32af9 | |||
121f1222c2 | |||
40eb704eb2 | |||
4005e8cbd1 | |||
9d82a43fe6 | |||
9a555eef9b | |||
79ffbb5e30 | |||
868cf8edc0 | |||
7f5c584772 | |||
5b58103e3b | |||
6d49a90fe7 | |||
c8a94d552a | |||
c9fa7da689 | |||
0d059fdf6e | |||
dcb5f8ad65 | |||
f970452779 | |||
6abd0286bb | |||
8bfbe078db | |||
6ece83f9ce |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
vendor*
|
||||
/vendor*
|
||||
/books.fw.png
|
||||
/datatypes.fw.png
|
||||
/styles.fw.png
|
||||
/.gitignore
|
||||
|
109
CHANGELOG.md
Normal file
109
CHANGELOG.md
Normal file
@ -0,0 +1,109 @@
|
||||
# Changelog
|
||||
|
||||
# 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
|
||||
* license fixed
|
||||
|
||||
## 1.1.10 (2022-02-05)
|
||||
* namespace added, use Shuchkin\SimpleXLSXGen
|
||||
|
||||
## 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)...)
|
||||
|
||||
## 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
|
59
README.md
59
README.md
@ -1,12 +1,14 @@
|
||||
# SimpleXLSXGen class 1.0.11 (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)
|
||||
# SimpleXLSXGen
|
||||
[<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/>
|
||||
(!) XLSX reader [here](https://github.com/shuchkin/simplexlsx).
|
||||
Export data to Excel XLSX file. PHP XLSX generator. No external tools and libraries.
|
||||
- XLSX reader [here](https://github.com/shuchkin/simplexlsx)
|
||||
- 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/>
|
||||
|
||||
*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
|
||||
```php
|
||||
@ -15,7 +17,7 @@ $books = [
|
||||
[618260307, 'The Hobbit', 'J. R. R. Tolkien', 'Houghton Mifflin', 'USA'],
|
||||
[908606664, 'Slinky Malinki', 'Lynley Dodd', 'Mallinson Rendel', 'NZ']
|
||||
];
|
||||
$xlsx = SimpleXLSXGen::fromArray( $books );
|
||||
$xlsx = Shuchkin\SimpleXLSXGen::fromArray( $books );
|
||||
$xlsx->saveAs('books.xlsx'); // or downloadAs('books.xlsx') or $xlsx_content = (string) $xlsx
|
||||
```
|
||||

|
||||
@ -46,7 +48,7 @@ $data = [
|
||||
['Hyperlink + Anchor', '<a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a>'],
|
||||
['RAW string', "\0".'2020-10-04 16:02:00']
|
||||
];
|
||||
SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx');
|
||||
Shuchkin\SimpleXLSXGen::fromArray( $data )->saveAs('datatypes.xlsx');
|
||||
```
|
||||

|
||||
|
||||
@ -64,11 +66,13 @@ $data = [
|
||||
['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>'],
|
||||
['<center>MERGE CELLS</center>']
|
||||
];
|
||||
SimpleXLSXGen::fromArray( $data )
|
||||
Shuchkin\SimpleXLSXGen::fromArray( $data )
|
||||
->setDefaultFont( 'Courier New' )
|
||||
->setDefaultFontSize( 14 )
|
||||
->mergeCells('A13:B13')
|
||||
->saveAs('styles_and_tags.xlsx');
|
||||
```
|
||||

|
||||
@ -76,15 +80,16 @@ SimpleXLSXGen::fromArray( $data )
|
||||
### More examples
|
||||
```php
|
||||
// Fluid interface, output to browser for download
|
||||
SimpleXLSXGen::fromArray( $books )->downloadAs('table.xlsx');
|
||||
Shuchkin\SimpleXLSXGen::fromArray( $books )->downloadAs('table.xlsx');
|
||||
|
||||
// Fluid interface, multiple sheets
|
||||
SimpleXLSXGen::fromArray( $books )->addSheet( $books2 )->download();
|
||||
Shuchkin\SimpleXLSXGen::fromArray( $books )->addSheet( $books2 )->download();
|
||||
|
||||
// Alternative interface, sheet name, get xlsx content
|
||||
$xlsx_cache = (string) (new SimpleXLSXGen)->addSheet( $books, 'Modern style');
|
||||
$xlsx_cache = (string) (new Shuchkin\SimpleXLSXGen)->addSheet( $books, 'Modern style');
|
||||
|
||||
// Classice interface
|
||||
// Classic interface
|
||||
use Shuchkin\SimpleXLSXGen
|
||||
$xlsx = new SimpleXLSXGen();
|
||||
$xlsx->addSheet( $books, 'Catalog 2021' );
|
||||
$xlsx->addSheet( $books2, 'Stephen King catalog');
|
||||
@ -92,34 +97,14 @@ $xlsx->downloadAs('books_2021.xlsx');
|
||||
exit();
|
||||
```
|
||||
|
||||
### Debug
|
||||
## Debug
|
||||
```php
|
||||
ini_set('error_reporting', E_ALL );
|
||||
ini_set('display_errors', 1 );
|
||||
|
||||
$data = [
|
||||
['Debug', 123]
|
||||
]
|
||||
SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
|
||||
];
|
||||
|
||||
Shuchkin\SimpleXLSXGen::fromArray( $data )->saveAs('debug.xlsx');
|
||||
```
|
||||
|
||||
|
||||
## History
|
||||
v1.0.11 (2021-05-14) Fixed 0.00% format, thx [marcrobledo](https://github.com/shuchkin/simplexlsxgen/pull/34), more examples in README.md<br/>
|
||||
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
|
@ -17,6 +17,7 @@
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.4",
|
||||
"ext-mbstring": "*",
|
||||
"ext-zlib": "*"
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Lukas Martinelli
|
||||
Copyright (c) 2020-2022 Sergey Shuchkin sergey.shuchkin@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
namespace Shuchkin;
|
||||
|
||||
/**
|
||||
* Class SimpleXLSXGen
|
||||
* Export data to MS Excel. PHP XLSX generator
|
||||
@ -34,11 +36,10 @@ class SimpleXLSXGen {
|
||||
const A_RIGHT = 2;
|
||||
const A_CENTER = 3;
|
||||
|
||||
|
||||
public function __construct() {
|
||||
$this->curSheet = -1;
|
||||
$this->defaultFont = 'Calibri';
|
||||
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [] ] ];
|
||||
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [] ] ];
|
||||
$this->SI = []; // sharedStrings index
|
||||
$this->SI_KEYS = []; // & keys
|
||||
$this->F = [ self::F_NORMAL ]; // fonts
|
||||
@ -71,7 +72,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>{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"?>
|
||||
@ -110,16 +111,37 @@ class SimpleXLSXGen {
|
||||
// <si><t>Простой шаблон</t></si><si><t>Будем делать генератор</t></si>
|
||||
}
|
||||
public static function fromArray( array $rows, $sheetName = null ) {
|
||||
$xlsx = new static();
|
||||
return $xlsx->addSheet( $rows, $sheetName );
|
||||
return (new static())->addSheet( $rows, $sheetName );
|
||||
}
|
||||
|
||||
public function addSheet( array $rows, $name = null ) {
|
||||
|
||||
$this->curSheet++;
|
||||
if ( $name === null ) { // autogenerated sheet names
|
||||
$name = 'Sheet'.($this->curSheet+1);
|
||||
} else {
|
||||
$name = mb_substr($name, 0, 31);
|
||||
$names = [];
|
||||
foreach( $this->sheets as $sh ) {
|
||||
$names[ mb_strtoupper( $sh['name']) ] = 1;
|
||||
}
|
||||
for( $i = 0; $i < 100; $i++ ) {
|
||||
$postfix = ' ('.$i.')';
|
||||
$new_name = ($i === 0) ? $name : $name . $postfix;
|
||||
if (mb_strlen($new_name) > 31) {
|
||||
$new_name = mb_substr($name,0, 31-mb_strlen($postfix)) . $postfix;
|
||||
}
|
||||
$NEW_NAME = mb_strtoupper( $new_name );
|
||||
if ( !isset( $names[ $NEW_NAME ]) ) {
|
||||
$name = $new_name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->sheets[$this->curSheet] = ['name' => $name ?: 'Sheet'.($this->curSheet+1), 'hyperlinks' => []];
|
||||
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => []];
|
||||
|
||||
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;
|
||||
} else {
|
||||
$this->sheets[$this->curSheet]['rows'] = [];
|
||||
@ -217,7 +239,7 @@ class SimpleXLSXGen {
|
||||
} elseif ( $cfilename === 'xl/workbook.xml' ) {
|
||||
$s = '';
|
||||
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);
|
||||
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
|
||||
@ -232,8 +254,8 @@ class SimpleXLSXGen {
|
||||
$this->SI[] = 'No Data';
|
||||
}
|
||||
$si_cnt = count($this->SI);
|
||||
$this->SI = '<si><t>'.implode("</t></si>\r\n<si><t>", $this->SI).'</t></si>';
|
||||
$template = str_replace(['{CNT}', '{STRINGS}'], [ $si_cnt, $this->SI ], $template );
|
||||
$si = '<si><t>'.implode("</t></si>\r\n<si><t>", $this->SI).'</t></si>';
|
||||
$template = str_replace(['{CNT}', '{STRINGS}'], [ $si_cnt, $si ], $template );
|
||||
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
|
||||
$entries++;
|
||||
} elseif ( $cfilename === 'xl/worksheets/sheet1.xml' ) {
|
||||
@ -245,12 +267,12 @@ class SimpleXLSXGen {
|
||||
}
|
||||
$xml = null;
|
||||
} elseif ( $cfilename === 'xl/worksheets/_rels/sheet1.xml.rels' ) {
|
||||
$RH = [];
|
||||
foreach ( $this->sheets as $k => $v ) {
|
||||
if ( count($v['hyperlinks'])) {
|
||||
$RH = [];
|
||||
$filename = 'xl/worksheets/_rels/sheet' . ( $k + 1 ) . '.xml.rels';
|
||||
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 );
|
||||
$this->_writeEntry( $fh, $cdrec, $filename, $xml );
|
||||
@ -418,12 +440,13 @@ class SimpleXLSXGen {
|
||||
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;
|
||||
|
||||
@ -460,6 +483,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] : ''];
|
||||
$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 );
|
||||
} // tags
|
||||
$vl = mb_strlen( $v );
|
||||
@ -490,20 +517,24 @@ class SimpleXLSXGen {
|
||||
$N = self::N_TIME; // time
|
||||
} 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] );
|
||||
$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 ) ) {
|
||||
$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;
|
||||
} elseif ( preg_match( '/https?:\/\/\S+/i', $v ) ) {
|
||||
} 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
|
||||
} 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' => ''];
|
||||
$F = self::F_HYPERLINK; // Hyperlink
|
||||
}
|
||||
if ( ($N === self::N_DATE || $N === self::N_DATETIME) && $cv < 0 ) {
|
||||
$cv = null;
|
||||
$N = 0;
|
||||
}
|
||||
}
|
||||
if ( !$cv) {
|
||||
|
||||
@ -532,9 +563,9 @@ class SimpleXLSXGen {
|
||||
} elseif ( is_float( $v ) ) {
|
||||
$vl = mb_strlen( (string) $v );
|
||||
$cv = $v;
|
||||
} elseif ( $v instanceof DateTime ) {
|
||||
} 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;
|
||||
@ -563,8 +594,8 @@ class SimpleXLSXGen {
|
||||
}
|
||||
}
|
||||
|
||||
$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";
|
||||
}
|
||||
@ -572,11 +603,22 @@ class SimpleXLSXGen {
|
||||
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" />';
|
||||
}
|
||||
$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';
|
||||
}
|
||||
|
||||
$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>';
|
||||
@ -588,8 +630,8 @@ class SimpleXLSXGen {
|
||||
//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}','{MERGECELLS}','{HYPERLINKS}'],
|
||||
[ $REF, implode("\r\n", $COLS), implode("\r\n",$ROWS), implode("\r\n", $MERGECELLS), implode("\r\n", $HYPERLINKS) ],
|
||||
$template );
|
||||
}
|
||||
|
||||
@ -604,15 +646,16 @@ class SimpleXLSXGen {
|
||||
}
|
||||
|
||||
public function date2excel($year, $month, $day, $hours=0, $minutes=0, $seconds=0) {
|
||||
|
||||
$excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
|
||||
|
||||
if ( $year === 0 ) {
|
||||
if ( (int) $year === 0 ) {
|
||||
return $excelTime;
|
||||
}
|
||||
|
||||
// self::CALENDAR_WINDOWS_1900
|
||||
$excel1900isLeapYear = True;
|
||||
if (((int)$year === 1900) && ($month <= 2)) { $excel1900isLeapYear = False; }
|
||||
if (($year === 1900) && ($month <= 2)) { $excel1900isLeapYear = False; }
|
||||
$myExcelBaseDate = 2415020;
|
||||
|
||||
// Julian base date Adjustment
|
||||
@ -622,9 +665,9 @@ class SimpleXLSXGen {
|
||||
$month += 9;
|
||||
--$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);
|
||||
$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;
|
||||
|
||||
return (float) $excelDate + $excelTime;
|
||||
@ -637,7 +680,13 @@ class SimpleXLSXGen {
|
||||
$this->defaultFontSize = $size;
|
||||
return $this;
|
||||
}
|
||||
public function mergeCells( $range ) {
|
||||
$this->sheets[$this->curSheet]['mergecells'][] = $range;
|
||||
return $this;
|
||||
}
|
||||
public function esc( $str ) {
|
||||
return str_replace( ['&', '<', '>', "\x03"], ['&', '<', '>', ''], $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: 57 KiB |
Reference in New Issue
Block a user